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.)
$ go version
go version go1.9.1 linux/amd64
go build -tags DEVTEST -ldflags -w -X main.version=4.7.0-pre1 main.commit=91f60bb -o ./bin/grafana-server ./pkg/cmd/grafana-server
// +build !DEVTEST
<package code here>
// +build DEVTEST
<package code here>
All my attempts have ended with all items in the package shown as redeclared, implying the build contraints were not applied.
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.)
Ok, here is the actual console output:
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
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
Source code has been placed on github at:
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.
You sir, are VERY observant!! Your suggestion worked!
I owe you more than a beer, instead I’ll offer 1 mole of thank you’s (6.022 x 10E23).