Skip to content

apply-ignore ignores dotfiles #1310

Closed
Closed

Description

@ferrarimarco

Hi, thanks for supporting this tool.

I'm experiencing a surprising behavior when adding the --apply-ignore option and invoking shfmt to format scripts with a file name that starts with a dot (.).

Example:

shfmt --apply-ignore --diff .script.sh

shfmt will ignore .script.sh.

I think this happens in the CouldBeScript2 function:

sh/fileutil/file.go

Lines 67 to 84 in 2315483

// CouldBeScript2 reports how likely a directory entry is to be a shell script.
// It discards directories and other non-regular files like symbolic links,
// filenames beginning with '.', and files with non-shell extensions.
func CouldBeScript2(entry fs.DirEntry) ScriptConfidence {
name := entry.Name()
switch {
case name[0] == '.':
return ConfNotScript // '.' prefix (hidden file)
case !entry.Type().IsRegular():
return ConfNotScript // dir, symlink, named pipes, etc
case extRe.MatchString(name):
return ConfIsScript // shell extension
case strings.IndexByte(name, '.') > 0:
return ConfNotScript // non-shell extension
default:
return ConfIfShebang // no extension; read and look for a shebang
}
}

specifically with the lines:

sh/fileutil/file.go

Lines 73 to 74 in 2315483

case name[0] == '.':
return ConfNotScript // '.' prefix (hidden file)

I don't have the full context that led to this implementation, but it makes it impossible to check the formatting of Bash and ZSH dotfiles, for example. Also, I didn't find any mention to it in the documentation, that only refers to EditorConfig files being considered when adding the --apply-ignore option:

sh/cmd/shfmt/shfmt.1.scd

Lines 47 to 52 in 2315483

*--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.

Would it be possible to make ignoring dotfiles configurable?

Thanks!

Activity

  1. carhartl commented on Apr 1, 2026

    @carhartl

    Possibly the files recently having been added here: 33b521e (thanks for that ❤️) would also need to yield ConfIsScript 🤔

  2. mvdan commented on Apr 1, 2026

    @mvdan
    Owner

    Thanks for reporting; this is an oversight in the logic. I had split it into two at some point in the past, but this led to splitting the logic for --apply-ignore, and I then made the logic slightly different in the two branches.

    I'm joining the logic again. This leads to much more inline code, but it's less duplication and chances for bugs like this one.

  3. ferrarimarco commented on Apr 1, 2026

    @ferrarimarco
    Author

    Thanks!

  4. added a commit that references this issue on Apr 2, 2026
    f299f47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址