Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/shfmt: add --apply-ignore flag for tools and editors #1051

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 42 additions & 27 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
15 changes: 11 additions & 4 deletions cmd/shfmt/shfmt.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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* <str>
Expand Down Expand Up @@ -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
```
Expand Down
44 changes: 41 additions & 3 deletions cmd/shfmt/testdata/script/editorconfig.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down Expand Up @@ -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
Expand Down