bazel-contrib / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.2k stars 380 forks source link

Add directive to skip generating a specific rule #57

Open jayconrod opened 6 years ago

jayconrod commented 6 years ago

Migrated from bazelbuild/rules_go#788

For example, you could prevent Gazelle from generating a test for in a certain package using a directive like this:

# gazelle:skip go_default_test
dancompton commented 5 years ago

@jayconrod Does this still apply? I'm currently attempting to rewrite the AST of many jsonnet manifests using a mix of tooling from go-jsonnet and now-defunct k8s.
The tests break when combined into one and it's impossible to construct proper tests using a single jsonnet file. I'm seeing this:

gazelle: /home/dc/work/src/github.com/zenreach/service-manifests/tools/refactor/query/BUILD.bazel: unknown directive: gazelle:skiprule

PS: if you have any tips on mass-refactoring jsonnet code that is are miserable, please include them in your response.

jayconrod commented 5 years ago

This directive hasn't been implemented. Is this a go_test that ends up broken or something else?

Sorry I don't know enough about jsonnet to advise. Good luck though.

dancompton commented 5 years ago

Consider yourself lucky. Perhaps cel-spec could make a fine replacement? Will that be supported in rules_go?

jayconrod commented 5 years ago

rules_go just supports Go and proto. I avoid adding support for too many other tools, since it adds dependencies for users.

I'm sure CEL rules could be written for Bazel, but I'm not sure if anyone is already planning to do so.

dragonsinth commented 4 years ago

@jayconrod did anything like this ever land? This would be SUPER useful in go_repository rules. I have a number of third party deps whose tests "fan out" into problematic deps; if I could write things like:

        build_directives = ["gazelle:exclude testing", "gazelle:skiprule go_default_test"],

I could prune a bunch of things out of our build tree.

dragonsinth commented 4 years ago

^^ btw I have a working patch for this if there is interest!

jayconrod commented 4 years ago

@dragonsinth No this is still open. Feel free to send a patch. I changed the directive name in the first comment from skiprule to skip.

For the example you gave, # gazelle:skip would only skip generating a particular target, not all targets with a matching name. So I wonder if this might work better:

build_directives = ["gazelle:exclude **_test.go"],

37 is a related issue: probably would be good to disable generation of all test targets in go_repository.

dragonsinth commented 4 years ago

@dragonsinth No this is still open. Feel free to send a patch. I changed the directive name in the first comment from skiprule to skip.

For the example you gave, # gazelle:skip would only skip generating a particular target, not all targets with a matching name. So I wonder if this might work better:

build_directives = ["gazelle:exclude **_test.go"],

Ah, that actually works just fine! Or rather in our particular case:

        build_directives = ["gazelle:exclude testing", "gazelle:exclude **/**_test.go"],

I didn't realize exclude would work on the file level!

So this solves my problem, and would allow me to unfork our skiprule implementation. But if you want a skip feature, happy to push you a PR. (It doesn't have test coverage, tho.)

dragonsinth commented 4 years ago

Actually the patch I used is pretty small... just gonna drop it here:

diff --git a/language/go/config.go b/language/go/config.go
index 98126b141c..ee425a39be 100644
--- a/language/go/config.go
+++ b/language/go/config.go
@@ -115,6 +115,9 @@ type goConfig struct {
    // attribute.
    repoNamingConvention map[string]namingConvention

+   // A list of rule names to skip generating rules for.
+   skipRule map[string]bool
+
    // submodules is a list of modules which have the current module's path
    // as a prefix of their own path. This affects visibility attributes
    // in internal packages.
@@ -335,6 +338,7 @@ func (*goLang) KnownDirectives() []string {
        "go_visibility",
        "importmap_prefix",
        "prefix",
+       "skiprule",
    }
 }

@@ -570,6 +574,13 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.`

            case "prefix":
                setPrefix(d.Value)
+
+           case "skiprule":
+               list := splitValue(d.Value)
+               gc.skipRule = map[string]bool{}
+               for _, e := range list {
+                   gc.skipRule[e] = true
+               }
            }
        }

diff --git a/language/go/generate.go b/language/go/generate.go
index 2892948d38..7812878230 100644
--- a/language/go/generate.go
+++ b/language/go/generate.go
@@ -278,7 +278,11 @@ func (gl *goLang) GenerateRules(args language.GenerateArgs) language.GenerateRes
            g.generateTest(pkg, libName))
    }

+   gc := getGoConfig(c)
    for _, r := range rules {
+       if gc.skipRule[r.Name()] {
+           continue
+       }
        if r.IsEmpty(goKinds[r.Kind()]) {
            res.Empty = append(res.Empty, r)
        } else {
GabrielGhe commented 2 years ago

Is there a directive that we can use to skip a whole directory?