forc-fmt 0.71.2

A `forc` plugin for running the Sway code formatter.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! A `forc` plugin for running the Sway code formatter.

use anyhow::{bail, Result};
use clap::Parser;
use forc_pkg::{
    manifest::{GenericManifestFile, ManifestFile},
    WorkspaceManifestFile,
};
use forc_tracing::{init_tracing_subscriber, println_error, println_green, println_red};
use forc_util::fs_locking::is_file_dirty;
use prettydiff::{basic::DiffOp, diff_lines};
use std::{
    collections::HashMap,
    default::Default,
    fs,
    path::{Path, PathBuf},
};
use sway_features::ExperimentalFeatures;
use sway_utils::{constants, find_parent_manifest_dir, get_sway_files, is_sway_file};
use swayfmt::Formatter;
use taplo::formatter as taplo_fmt;
use tracing::{debug, info};

forc_util::cli_examples! {
    crate::App {
        [ Run the formatter in check mode on the current directory => "forc fmt --check"]
        [ Run the formatter in check mode on the current directory with short format => "forc fmt -c"]
        [ Run formatter against a given file => "forc fmt --file {path}/src/main.sw"]
        [ Run formatter against a given file with short format => "forc fmt -f {path}/src/main.sw"]
        [ Run formatter against a given dir => "forc fmt --path {path}"]
        [ Run formatter against a given dir with short format => "forc fmt -p {path}"]
    }
}

#[derive(Debug, Parser)]
#[clap(
    name = "forc-fmt",
    about = "Forc plugin for running the Sway code formatter",
    after_help = help(),
    version
)]
pub struct App {
    /// Run in 'check' mode.
    ///
    /// - Exits with `0` if input is formatted correctly.
    /// - Exits with `1` and prints a diff if formatting is required.
    #[clap(short, long)]
    pub check: bool,
    /// Path to the project.
    ///
    /// If not specified, current working directory will be used.
    #[clap(short, long)]
    pub path: Option<String>,
    #[clap(short, long)]
    /// Formats a single .sw file with the default settings.
    /// If not specified, current working directory will be formatted using a Forc.toml
    /// configuration.
    pub file: Option<String>,

    #[command(flatten)]
    experimental: sway_features::CliFields,
}

fn main() {
    init_tracing_subscriber(Default::default());
    if let Err(err) = run() {
        println_error("Formatting skipped due to error.");
        println_error(&format!("{err}"));
        std::process::exit(1);
    }
}

fn run() -> Result<()> {
    let app = App::parse();

    let dir = match app.path.as_ref() {
        Some(path) => PathBuf::from(path),
        None => std::env::current_dir()?,
    };

    let experimental = ExperimentalFeatures::new(
        &HashMap::default(),
        &app.experimental.experimental,
        &app.experimental.no_experimental,
    )
    .map_err(|err| anyhow::anyhow!("{err}"))?;

    let mut formatter = Formatter::from_dir(&dir, experimental)?;
    if let Some(f) = app.file.as_ref() {
        let file_path = &PathBuf::from(f);

        if is_sway_file(file_path) {
            format_file(&app, file_path.to_path_buf(), &mut formatter)?;
            return Ok(());
        }

        bail!(
            "Provided file '{}' is not a valid Sway file",
            file_path.display()
        );
    };

    let manifest_file = forc_pkg::manifest::ManifestFile::from_dir(&dir)?;
    match manifest_file {
        ManifestFile::Workspace(ws) => {
            format_workspace_at_dir(&app, &ws, &dir, experimental)?;
        }
        ManifestFile::Package(_) => {
            format_pkg_at_dir(&app, &dir, &mut formatter)?;
        }
    }
    Ok(())
}

/// Recursively get a Vec<PathBuf> of subdirectories that contains a Forc.toml.
fn get_sway_dirs(workspace_dir: PathBuf) -> Vec<PathBuf> {
    let mut dirs_to_format = vec![];
    let mut dirs_to_search = vec![workspace_dir];

    while let Some(next_dir) = dirs_to_search.pop() {
        if let Ok(read_dir) = fs::read_dir(next_dir) {
            for entry in read_dir.filter_map(|res| res.ok()) {
                let path = entry.path();

                if path.is_dir() {
                    dirs_to_search.push(path.clone());
                    if path.join(constants::MANIFEST_FILE_NAME).exists() {
                        dirs_to_format.push(path);
                    }
                }
            }
        }
    }

    dirs_to_format
}

/// Format a file, given its path.
/// Returns:
/// - Ok(true) if executed successfully and formatted,
/// - Ok(false) if executed successfully and not formatted,
/// - Err if it fails to execute at all.
fn format_file(app: &App, file: PathBuf, formatter: &mut Formatter) -> Result<bool> {
    let file = file.canonicalize()?;
    if is_file_dirty(&file) {
        bail!(
            "The below file is open in an editor and contains unsaved changes.\n       \
             Please save it before formatting.\n       \
             {}",
            file.display()
        );
    }
    if let Ok(file_content) = fs::read_to_string(&file) {
        let mut edited = false;
        match Formatter::format(formatter, file_content.as_str().into()) {
            Ok(formatted_content) => {
                if app.check {
                    if *file_content != formatted_content {
                        info!("File was edited by formatter: \n{:?}\n", file);
                        display_file_diff(&file_content, &formatted_content)?;
                        edited = true;
                    }
                } else {
                    write_file_formatted(&file, &formatted_content)?;
                }

                return Ok(edited);
            }
            Err(err) => {
                // TODO: Support formatting for incomplete/invalid sway code.
                // https://github.com/FuelLabs/sway/issues/5012
                debug!("{}", err);
                if let Some(file) = file.to_str() {
                    bail!("Failed to compile {}\n{}", file, err);
                } else {
                    bail!("Failed to compile.\n{}", err);
                }
            }
        }
    }

    bail!("Could not read file: {:?}", file)
}

/// Format the workspace at the given directory.
fn format_workspace_at_dir(
    app: &App,
    workspace: &WorkspaceManifestFile,
    dir: &Path,
    experimental: ExperimentalFeatures,
) -> Result<()> {
    let mut contains_edits = false;
    let mut formatter = Formatter::from_dir(dir, experimental)?;
    let mut members = vec![];

    for member_path in workspace.member_paths()? {
        members.push(member_path)
    }

    // Format files at the root - we do not want to start calling format_pkg_at_dir() here,
    // since this would mean we format twice on each subdirectory.
    if let Ok(read_dir) = fs::read_dir(dir) {
        for entry in read_dir.filter_map(|res| res.ok()) {
            let path = entry.path();
            if is_sway_file(&path) {
                format_file(app, path, &mut formatter)?;
            }
        }
    }

    // Format subdirectories. We do not call format on members directly here, since
    // in workspaces, it is perfectly valid to have subdirectories containing Sway files,
    // yet not be a member of the workspace.
    for sub_dir in get_sway_dirs(dir.to_path_buf()) {
        if sub_dir.join(constants::MANIFEST_FILE_NAME).exists() {
            // Here, we cannot simply call Formatter::from_dir() and rely on defaults
            // if there is no swayfmt.toml in the sub directory because we still want
            // to use the swayfmt.toml at the workspace root (if any).
            // In order of priority: member > workspace > default.
            formatter = Formatter::from_dir(&sub_dir, experimental)?;
        }
        format_pkg_at_dir(app, &sub_dir, &mut formatter)?;
    }

    let manifest_file = dir.join(constants::MANIFEST_FILE_NAME);

    // Finally, format the root manifest using taplo formatter
    contains_edits |= format_manifest(app, manifest_file)?;

    if app.check && contains_edits {
        // One or more files are not formatted, exit with error
        bail!("Files contain formatting violations.");
    }

    Ok(())
}

/// Format the given manifest at a path.
/// Returns:
/// - Ok(true) if executed successfully and formatted,
/// - Ok(false) if executed successfully and not formatted,
/// - Err if it fails to execute at all.
fn format_manifest(app: &App, manifest_file: PathBuf) -> Result<bool> {
    if let Ok(manifest_content) = fs::read_to_string(&manifest_file) {
        let mut edited = false;
        // TODO: Alphabetize tables excluding the project table when https://github.com/tamasfe/taplo/issues/763 is supported
        let formatted_content = taplo_fmt::format(&manifest_content, taplo_fmt::Options::default());
        if !app.check {
            write_file_formatted(&manifest_file, &formatted_content)?;
        } else if formatted_content != manifest_content {
            edited = true;
            println_error(&format!(
                "Improperly formatted manifest file: {}",
                manifest_file.display()
            ));
            display_file_diff(&manifest_content, &formatted_content)?;
        } else {
            info!(
                "Manifest Forc.toml formatted correctly: {}",
                manifest_file.display()
            )
        }

        return Ok(edited);
    };

    bail!("failed to format manifest: {:?}", manifest_file)
}

/// Format the package at the given directory.
fn format_pkg_at_dir(app: &App, dir: &Path, formatter: &mut Formatter) -> Result<()> {
    match find_parent_manifest_dir(dir) {
        Some(path) => {
            let manifest_path = path.clone();
            let manifest_file = manifest_path.join(constants::MANIFEST_FILE_NAME);
            let files = get_sway_files(path);
            let mut contains_edits = false;

            for file in files {
                contains_edits |= format_file(app, file, formatter)?;
            }
            // format manifest using taplo formatter
            contains_edits |= format_manifest(app, manifest_file)?;

            if app.check && contains_edits {
                // One or more files are not formatted, exit with error
                bail!("Files contain formatting violations.");
            }

            Ok(())
        }
        _ => bail!("Manifest file does not exist"),
    }
}

fn display_file_diff(file_content: &str, formatted_content: &str) -> Result<()> {
    let changeset = diff_lines(file_content, formatted_content);
    let mut count_of_updates = 0;
    for diff in changeset.diff() {
        // max 100 updates
        if count_of_updates >= 100 {
            break;
        }
        match diff {
            DiffOp::Equal(old) => {
                for o in old {
                    info!("{}", o)
                }
            }
            DiffOp::Insert(new) => {
                count_of_updates += 1;
                for n in new {
                    println_green(&format!("+{n}"));
                }
            }
            DiffOp::Remove(old) => {
                count_of_updates += 1;
                for o in old {
                    println_red(&format!("-{o}"));
                }
            }
            DiffOp::Replace(old, new) => {
                count_of_updates += 1;
                for o in old {
                    println_red(&format!("-{o}"));
                }
                for n in new {
                    println_green(&format!("+{n}"));
                }
            }
        }
    }
    Ok(())
}

fn write_file_formatted(file: &Path, formatted_content: &str) -> Result<()> {
    fs::write(file, formatted_content)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::taplo_fmt;
    use std::default::Default;

    #[test]
    fn test_forc_indentation() {
        let correct_forc_manifest = r#"
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "Fuel example project"


[dependencies]
std = { git = "https://github.com/FuelLabs/sway-lib-std", version = "v0.0.1" }
"#;
        let taplo_alphabetize = taplo_fmt::Options {
            reorder_keys: true,
            ..Default::default()
        };
        let formatted_content = taplo_fmt::format(correct_forc_manifest, taplo_alphabetize.clone());
        assert_eq!(formatted_content, correct_forc_manifest);
        let indented_forc_manifest = r#"
        [project]
    authors = ["Fuel Labs <contact@fuel.sh>"]
                    license = "Apache-2.0"
    name = "Fuel example project"


    [dependencies]
                    std = { git = "https://github.com/FuelLabs/sway-lib-std", version = "v0.0.1" }
"#;
        let formatted_content =
            taplo_fmt::format(indented_forc_manifest, taplo_alphabetize.clone());
        assert_eq!(formatted_content, correct_forc_manifest);
        let whitespace_forc_manifest = r#"
[project]
 authors=["Fuel Labs <contact@fuel.sh>"]
license   =                                   "Apache-2.0"
name = "Fuel example project"


[dependencies]
std         =     {   git     =  "https://github.com/FuelLabs/sway-lib-std"  , version = "v0.0.1"           }
"#;
        let formatted_content = taplo_fmt::format(whitespace_forc_manifest, taplo_alphabetize);
        assert_eq!(formatted_content, correct_forc_manifest);
    }

    #[test]
    fn test_forc_alphabetization() {
        let correct_forc_manifest = r#"
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "Fuel example project"


[dependencies]
std = { git = "https://github.com/FuelLabs/sway-lib-std", version = "v0.0.1" }
"#;
        let taplo_alphabetize = taplo_fmt::Options {
            reorder_keys: true,
            ..Default::default()
        };
        let formatted_content = taplo_fmt::format(correct_forc_manifest, taplo_alphabetize.clone());
        assert_eq!(formatted_content, correct_forc_manifest);
        let disordered_forc_manifest = r#"
[project]
name = "Fuel example project"
license = "Apache-2.0"
authors = ["Fuel Labs <contact@fuel.sh>"]


[dependencies]
std = { git = "https://github.com/FuelLabs/sway-lib-std", version = "v0.0.1" }
    "#;
        let formatted_content = taplo_fmt::format(disordered_forc_manifest, taplo_alphabetize);
        assert_eq!(formatted_content, correct_forc_manifest);
    }
}