I’ve been trying to implement Build Constraints while trying to build a modified version of grafana. All my attempts at setting the “-tags” build argument have been unsuccessful.
In the code below, package 1 will be for a production build, and package 2 is for development (much more logging is included.)
Looks correct to me (apart from the ldflags argument lacking quotes). Maybe something is different in what you have or what you do than you are showing here?
(That is: please show your real code and real error.)
go build -i --tags DEVTEST -ldflags -w -X main.version=4.7.0-pre1 -X main.commit=91f60bb -X main.buildstamp=1507907560 -o ./bin/grafana-server ./pkg/cmd/grafana-server
# github.com/grafana/grafana/pkg/middleware
pkg/middleware/middlewaredev.go:28:6: Context redeclared in this block
previous declaration at pkg/middleware/middleware.go:28:6
pkg/middleware/middlewaredev.go:40:26: GetContextHandler redeclared in this block
previous declaration at pkg/middleware/middleware.go:40:26
pkg/middleware/middlewaredev.go:160:49: initContextWithAnonymousUser redeclared in this block
previous declaration at pkg/middleware/middleware.go:160:49
pkg/middleware/middlewaredev.go:181:66: initContextWithUserSessionCookie redeclared in this block
previous declaration at pkg/middleware/middleware.go:181:66
pkg/middleware/middlewaredev.go:204:42: initContextWithApiKey redeclared in this block
previous declaration at pkg/middleware/middleware.go:204:42
pkg/middleware/middlewaredev.go:240:58: initContextWithBasicAuth redeclared in this block
previous declaration at pkg/middleware/middleware.go:240:58
pkg/middleware/middlewaredev.go:283:6: (*Context).Handle redeclared in this block
previous declaration at pkg/middleware/middleware.go:283:6
pkg/middleware/middlewaredev.go:296:6: (*Context).JsonOK redeclared in this block
previous declaration at pkg/middleware/middleware.go:296:6
pkg/middleware/middlewaredev.go:302:6: (*Context).IsApiRequest redeclared in this block
previous declaration at pkg/middleware/middleware.go:302:6
pkg/middleware/middlewaredev.go:342:34: AddDefaultResponseHeaders redeclared in this block
previous declaration at pkg/middleware/middleware.go:342:34
pkg/middleware/middlewaredev.go:302:6: too many errors
So I couldn’t see anything wrong, and even pulled down your repo to double check, and got the same issue as you despite everything looking fine. Then I noticed:
The BOM is an extra two byte binary marker at the start of the file. It makes the compiler not see the build tag comment. Re-save as regular UTF-8 without BOM and you’re good.