diff --git a/cmd/shfmt/main.go b/cmd/shfmt/main.go index c13424f4..51fcbbc0 100644 --- a/cmd/shfmt/main.go +++ b/cmd/shfmt/main.go @@ -37,11 +37,12 @@ var ( versionFlag = &multiFlag[bool]{"", "version", false} list = &multiFlag[bool]{"l", "list", false} - write = &multiFlag[bool]{"w", "write", false} - simplify = &multiFlag[bool]{"s", "simplify", false} - minify = &multiFlag[bool]{"mn", "minify", false} - find = &multiFlag[bool]{"f", "find", false} - diff = &multiFlag[bool]{"d", "diff", false} + write = &multiFlag[bool]{"w", "write", false} + simplify = &multiFlag[bool]{"s", "simplify", false} + minify = &multiFlag[bool]{"mn", "minify", false} + find = &multiFlag[bool]{"f", "find", false} + diff = &multiFlag[bool]{"d", "diff", false} + applyIgnore = &multiFlag[bool]{"", "apply-ignore", false} lang = &multiFlag[syntax.LangVariant]{"ln", "language-dialect", syntax.LangAuto} posix = &multiFlag[bool]{"p", "posix", false} @@ -73,7 +74,7 @@ var ( version = "(devel)" // to match the default from runtime/debug allFlags = []any{ - versionFlag, list, write, simplify, minify, find, diff, + versionFlag, list, write, simplify, minify, find, diff, applyIgnore, lang, posix, filename, indent, binNext, caseIndent, spaceRedirs, keepPadding, funcNext, toJSON, fromJSON, } @@ -138,6 +139,7 @@ directory, all shell scripts found under that directory will be used. -d, --diff error with a diff when the formatting differs -s, --simplify simplify the code -mn, --minify minify the code to reduce its size (implies -s) + --apply-ignore always apply EditorConfig ignore rules Parser options: @@ -252,12 +254,12 @@ For more information, see 'man shfmt' and https://github.com/mvdan/sh. } status := 0 for _, path := range flag.Args() { - if info, err := os.Stat(path); err == nil && !info.IsDir() && !find.val { - // When given paths to files directly, always format - // them, no matter their extension or shebang. + if info, err := os.Stat(path); err == nil && !info.IsDir() && !applyIgnore.val && !find.val { + // When given paths to files directly, always format them, + // no matter their extension or shebang. // - // The only exception is the -f flag; in that case, we - // do want to report whether the file is a shell script. + // One exception is --apply-ignore, which explicitly changes this behavior. + // Another is --find, whose logic depends on walkPath being called. if err := formatPath(path, false); err != nil { if err != errChangedWithDiff { fmt.Fprintln(os.Stderr, err) @@ -295,6 +297,16 @@ func formatStdin(name string) error { if write.val { return fmt.Errorf("-w cannot be used on standard input") } + if applyIgnore.val { + // Mimic the logic from walkPath to apply the ignore rules. + props, err := ecQuery.Find(name, []string{"shell"}) + if err != nil { + return err + } + if props.Get("ignore") == "true" { + return nil + } + } src, err := io.ReadAll(in) if err != nil { return err @@ -319,28 +331,31 @@ func walkPath(path string, entry fs.DirEntry) error { if entry.IsDir() && vcsDir.MatchString(entry.Name()) { return filepath.SkipDir } - if useEditorConfig { - // We don't know the language variant at this point yet, as we are walking directories - // and we first want to tell if we should skip a path entirely. - // TODO: Should the call to Find with the language name check "ignore" too, then? - // Otherwise a [[bash]] section with ignore=true is effectively never used. - props, err := ecQuery.Find(path, []string{"shell"}) - if err != nil { - return err - } - if props.Get("ignore") == "true" { - if entry.IsDir() { - return filepath.SkipDir - } else { - return nil - } + // We don't know the language variant at this point yet, as we are walking directories + // and we first want to tell if we should skip a path entirely. + // + // TODO: Should the call to Find with the language name check "ignore" too, then? + // Otherwise a [[bash]] section with ignore=true is effectively never used. + // + // TODO: Should there be a way to explicitly turn off ignore rules when walking? + // Perhaps swapping the default to --apply-ignore=auto and allowing --apply-ignore=false? + // I don't imagine it's a particularly uesful scenario for now. + props, err := ecQuery.Find(path, []string{"shell"}) + if err != nil { + return err + } + if props.Get("ignore") == "true" { + if entry.IsDir() { + return filepath.SkipDir + } else { + return nil } } conf := fileutil.CouldBeScript2(entry) if conf == fileutil.ConfNotScript { return nil } - err := formatPath(path, conf == fileutil.ConfIfShebang) + err = formatPath(path, conf == fileutil.ConfIfShebang) if err != nil && !os.IsNotExist(err) { return err } diff --git a/cmd/shfmt/shfmt.1.scd b/cmd/shfmt/shfmt.1.scd index 58456b2d..84dd2dec 100644 --- a/cmd/shfmt/shfmt.1.scd +++ b/cmd/shfmt/shfmt.1.scd @@ -16,7 +16,7 @@ directory, all shell scripts found under that directory will be used. If any EditorConfig files are found, they will be used to apply formatting options. If any parser or printer flags are given to the tool, no EditorConfig -files will be used. A default like *-i=0* can be used for this purpose. +formatting options will be used. A default like *-i=0* can be used for this purpose. shfmt's default shell formatting was chosen to be consistent, common, and predictable. Some aspects of the format can be configured via printer flags. @@ -47,6 +47,13 @@ predictable. Some aspects of the format can be configured via printer flags. *-mn*, *--minify* Minify the code to reduce its size (implies *-s*). +*--apply-ignore* + Always apply EditorConfig ignore rules. + + When formatting files directly, ignore rules are skipped without this flag. + Should be useful to any tools or editors which format stdin or a single file. + When printing results to stdout, an ignored file results in no output at all. + ## Parser flags *-ln*, *--language-dialect* @@ -132,9 +139,9 @@ keep_padding = true function_next_line = true # Ignore the entire "third_party" directory when calling shfmt on directories, -# such as "shfmt -l -w .". -# When formatting regular files explicitly, like "shfmt -w third_party/foo.sh", -# the files are always formatted regardless of this setting. +# such as "shfmt -l -w .". When formatting files directly, +# like "shfmt -w third_party/foo.sh" or "shfmt --filename=third_party/foo.sh", +# the ignore logic is applied only when the --apply-ignore flag is given. [third_party/**] ignore = true ``` diff --git a/cmd/shfmt/testdata/script/editorconfig.txtar b/cmd/shfmt/testdata/script/editorconfig.txtar index f98d1a91..99037c13 100644 --- a/cmd/shfmt/testdata/script/editorconfig.txtar +++ b/cmd/shfmt/testdata/script/editorconfig.txtar @@ -47,12 +47,50 @@ exec shfmt -l otherknobs ! stdout . ! stderr . -# Ignore directories when walking, if they match ignore=true. +# Files found by walking directories are skipped if they match ignore=true properties. exec shfmt -l ignored stdout 'regular\.sh' ! stdout 'ignored\.sh' ! stderr . +# Formatting files directly does not obey ignore=true properties by default. +# Test the various modes in which shfmt can run. +! exec shfmt -l input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh +stdout -count=1 'input\.sh$' +stdout -count=1 'ignored\.sh$' +stderr -count=1 'ignored\.sh.* must be followed by' +! exec shfmt -d input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh +stdout -count=1 'input\.sh$' +stdout -count=1 'ignored\.sh$' +stderr -count=1 'ignored\.sh.* must be followed by' +! exec shfmt input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh +stdout -count=1 'indented' +stdout -count=1 'echo foo' +stderr -count=1 'ignored\.sh.* must be followed by' +stdin ignored/1_lone_ignored.sh +exec shfmt --filename=ignored/1_lone_ignored.sh +stdout -count=1 'echo foo' +! stderr . + +# Formatting files directly obeys ignore=true when --apply-ignore is given. +# Test the same modes that the earlier section does. +exec shfmt --apply-ignore -l input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh +stdout -count=1 'input\.sh$' +! stdout 'ignored\.sh' +! stderr . +! exec shfmt --apply-ignore -d input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh +stdout -count=1 'input\.sh$' +! stdout 'ignored\.sh' +! stderr . +exec shfmt --apply-ignore input.sh ignored/1_lone_ignored.sh ignored/third_party/bad_syntax_ignored.sh +stdout -count=1 'indented' +! stdout 'echo foo' +! stderr . +stdin ignored/1_lone_ignored.sh +exec shfmt --apply-ignore --filename=ignored/1_lone_ignored.sh +! stdout . +! stderr . + # Check EditorConfig [[language]] sections, used primarily for extension-less strings with shebangs. exec shfmt -d shebang ! stdout . @@ -153,9 +191,9 @@ ignore = true [2_dir_ignored] ignore = true --- ignored/third_party/ignored.sh -- +-- ignored/third_party/bad_syntax_ignored.sh -- bad (syntax --- ignored/1_lone_ignored.sh +-- ignored/1_lone_ignored.sh -- echo foo -- ignored/2_dir_ignored/ignored.sh -- echo foo