Unexpected side effect of -trimpath for go version -m

Hi all,

We wanted to put a (not very) secret into our executable at build time using -ldflags -X. But then we realised that the ldflags are printed out if you just use go version -m yourexe.

When we add the -trimpath flag to the build the ldflags disappear from go version which is super but this isn’t the documented behaviour of this flag.

Is this expected or a bug in the tools and/or might this feature disappear in future go version?

Thanks for your help.

E.g.

package main
import "fmt"
var ASecret = ""

func main() {
   fmt.Println(ASecret)
}
jonrichards@UNKNOWN Downloads % go build -ldflags="-X main.ASecret=123"  main.go
jonrichards@UNKNOWN Downloads % ./main 
123
jonrichards@UNKNOWN Downloads % go version -m main                              
main: go1.23.3
	path	command-line-arguments
	build	-buildmode=exe
	build	-compiler=gc
	build	-ldflags="-X main.ASecret=123"
	build	CGO_ENABLED=1
	build	CGO_CFLAGS=
	build	CGO_CPPFLAGS=
	build	CGO_CXXFLAGS=
	build	CGO_LDFLAGS=
	build	GOARCH=arm64
	build	GOOS=darwin
	build	GOARM64=v8.0
jonrichards@UNKNOWN Downloads % go build -trimpath -ldflags="-X main.ASecret=123"  main.go
jonrichards@UNKNOWN Downloads % go version -m main                                        
main: go1.23.3
	path	command-line-arguments
	build	-buildmode=exe
	build	-compiler=gc
	build	-trimpath=true
	build	CGO_ENABLED=1
	build	GOARCH=arm64
	build	GOOS=darwin
	build	GOARM64=v8.0

Hello there, it’s a bug and it is tracked here

1 Like

So it is. Thank you for your prompt response.