Build Constraints question

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.)

Version:

$ go version
go version go1.9.1 linux/amd64

Build:

go build -tags DEVTEST -ldflags -w -X main.version=4.7.0-pre1 main.commit=91f60bb -o ./bin/grafana-server ./pkg/cmd/grafana-server

package 1:

// +build !DEVTEST

package middleware

<package code here>

package 2:

// +build DEVTEST

package middleware

<package code here>

All my attempts have ended with all items in the package shown as redeclared, implying the build contraints were not applied.

Any insights?

Drew

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
# 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

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.

1 Like

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).

1 Like