Linter that matches Go file naming patterns to their expected build tags.
Jump to Installation and usage
Match file naming patterns to build tags and make sure these files always have the expected build tags in the // +build
instruction.
Example: files named foo.go
must include the bar
build tag.
File: foo.go
// +build bar
package foo
Example: files ending with _suffix.go
must include the bar
build tag.
File: a_suffix.go
// +build bar
package foo
File: b_suffix.go
// +build bar
package foo
Built on top of the buildtag
linter, it supports its Go files features.
- Run it as a standalone command
- Integrate it as a part of a runner using the provided
analysis.Analyzer
Let's say you name your integration tests *_integration_test.go
and you run them as part of your CI pipeline.
You might forget to add the // +build integration
instruction on a newly created file, or you might remove it inadvertently,
and it can have some consequences, such as never running during your pipeline.
As a consequence, you think your code works when it doesn't, because it is not tested (but you believe it is).
This linter can help with such issues and let you know when you forgot to add the expected build tags.
Install with Go install
GO111MODULE=on go get github.com/aziule/filebuildtag/cmd/filebuildtag
Install and build from source
- Clone the repo
- Build the executable
make build
Usage
// All files named "foo.go" must have the "bar" tag
filebuildtag --filetags foo.go:bar ./...
// All files ending with "_integration_test.go" must have the "integration" tag
filebuildtag --filetags *_integration_test.go:integration ./...
// Both of the above
filebuildtag --filetags foo.go:bar,*_integration_test.go:integration ./...
// Only check that the `// +build` instructions are correct (no args to pass)
filebuildtag ./...
Note: files naming patterns are matched using Go's filepath.Match
method. Therefore, you can use any of its supported patterns.
See File patterns for more information and examples.
This linter exposes an Analyzer
(accessible via filebuildtag.Analyzer
), which is defined as
a golang.org/x/tools/go/analysis/analysis.Analyzer
struct.
Most of the linters runners expect linters to be defined like so, therefore you should not have much trouble integrating it following the linters runner's doc.
From the official filepath.Match
doc, file patterns syntax is the following:
pattern:
{ term }
term:
'*' matches any sequence of non-Separator characters
'?' matches any single non-Separator character
'[' [ '^' ] { character-range } ']'
character class (must be non-empty)
c matches character c (c != '*', '?', '\\', '[')
'\\' c matches character c
character-range:
c matches character c (c != '\\', '-', ']')
'\\' c matches character c
lo '-' hi matches character c for lo <= c <= hi
file 👇 pattern 👉 | foo.go | *.go | ba?.go | *_test.go |
---|---|---|---|---|
foo.go | ✅ | ✅ | 🚫 | 🚫 |
bar.go | 🚫 | ✅ | ✅ | 🚫 |
baz.go | 🚫 | ✅ | ✅ | 🚫 |
a_test.go | 🚫 | ✅ | 🚫 | ✅ |
something | 🚫 | 🚫 | 🚫 | 🚫 |
- Support for folder name matching (
/pkg/**/foo.go
,/pkg/foo/*.go
, etc.).
A bug to report? A feature to add? Please feel free to open an issue or to propose pull requests!
Some of the code was copied from Go's buildtag
linter and adapted to match the needs of the filebuildtag
linter.
Those files have the mandatory copyright header and their license can be found in LICENSE.google
.
You can also find the original code here.