Skip to content

Commit

Permalink
feat: warn when it looks like you're using ignore instead of exclude …
Browse files Browse the repository at this point in the history
…directive (#1955)

**What type of PR is this?**

Feature

**What package or component does this PR mostly affect?**

all

**What does this PR do? Why is it needed?**

`ignore` and `exclude` are somewhat confusing directive names, and are
easily mixed up. Show a warning when using the `gazelle:ignore`
directive with an argument.

I've seen many places where people do something like `# gazelle:ignore
testdata` and accidentally disable gazelle for the entire directory and
subdirectories. This can be seen in [many public github
repos](https://github.com/search?q=%2F%23+gazelle%3Aignore+.%2F+language%3AStarlark&type=code&l=Starlark).

**Other notes for review**

Add a new generated test directory to assert that the warning is shown
correctly.
  • Loading branch information
alex-torok authored Oct 11, 2024
1 parent 8bbeae7 commit 186dedd
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 0 deletions.
Empty file.
Empty file.
1 change: 1 addition & 0 deletions tests/warns_on_invalid_directive_use/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Gazelle warns on invalid directive use
Empty file.
2 changes: 2 additions & 0 deletions tests/warns_on_invalid_directive_use/arguments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-lang
nolang
1 change: 1 addition & 0 deletions tests/warns_on_invalid_directive_use/expectedExitCode.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
1 change: 1 addition & 0 deletions tests/warns_on_invalid_directive_use/expectedStderr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gazelle: the ignore directive does not take any arguments. Did you mean to use gazelle:exclude instead? in //ignore_directive '# gazelle:ignore *.go'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# gazelle:ignore *.go

# valid use, but trailing whitespace should not warn
# gazelle:ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# gazelle:ignore *.go

# valid use, but trailing whitespace should not warn
# gazelle:ignore
3 changes: 3 additions & 0 deletions walk/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func (cr *Configurer) Configure(c *config.Config, rel string, f *rule.File) {
}
wcCopy.follow = append(wcCopy.follow, path.Join(rel, d.Value))
case "ignore":
if d.Value != "" {
log.Printf("the ignore directive does not take any arguments. Did you mean to use gazelle:exclude instead? in //%s '# gazelle:ignore %s'", f.Pkg, d.Value)
}
wcCopy.ignore = true
}
}
Expand Down

0 comments on commit 186dedd

Please sign in to comment.