`go test` does not honor -ldflags

2,090 views
Skip to first unread message

singh....@gmail.com

unread,
Oct 3, 2016, 7:30:32 PM10/3/16
to golang-nuts

Please see the code of main.go and main_test.go pasted at the end. Then consider the output of these commands. I would have expected the `go test` command's output to contain the value 'hello'. From `go help test` output, the `go test` command is supposed to honor all build flags, but clearly it isn't.

$ go version
go version go1.7.1 darwin/amd64

$ pwd
/tmp/ldflags_test

$ go run -ldflags="-X main.MyVar=hello" main.go
MyVar's value is hello

$ go build -ldflags="-X main.MyVar=hello" && ./ldflags_test
MyVar's value is hello

$ go test -ldflags="-X main.MyVar=hello"
MyVar is empty
exit status 1
FAIL    _/tmp/ldflags_test    0.016s

$ cat main.go
package main

import (
    "fmt"
    "os"
)

var MyVar = ""

func main() {
    if MyVar == "" {
        fmt.Println("MyVar is empty")
        os.Exit(1)
    } else {
        fmt.Println("MyVar's value is " + MyVar)
    }
}

$ cat main_test.go
package main

import (
    "testing"
)

func TestMain(t *testing.T) {
    main()
}

Ian Lance Taylor

unread,
Oct 3, 2016, 8:09:41 PM10/3/16
to singh....@gmail.com, golang-nuts
On Mon, Oct 3, 2016 at 4:08 PM, <singh....@gmail.com> wrote:
>
> Please see the code of main.go and main_test.go pasted at the end. Then
> consider the output of these commands. I would have expected the `go test`
> command's output to contain the value 'hello'. From `go help test` output,
> the `go test` command is supposed to honor all build flags, but clearly it
> isn't.

I don't think that's the problem. If you use the -x option with `go
test`, you should see that the -ldflags option is indeed being passed
to `go tool link`. I think the problem is that `go test` renames the
package being tested, so it no longer has the name `main`. That means
that your `-X` option is not using the right variable name.

I'm not sure whether this should be considered a bug or not.

Ian

Art Mellor

unread,
Oct 4, 2016, 9:03:39 AM10/4/16
to golang-nuts
From a user experience standpoint, I think the "Principle of least astonishment" would argue it is more of a bug than not
https://en.wikipedia.org/wiki/Principle_of_least_astonishment

Ian Lance Taylor

unread,
Oct 4, 2016, 9:36:23 AM10/4/16
to Art Mellor, golang-nuts
I can't argue with that.

The problem is that the definition of -X relies critically on knowing
the exact package name of the variable being set, which is to say the
path used to import that package. That works well enough for every
package other than "main". So one resolution of this problem is to
say that -X does not work reliably with the main package. Although
unfortunately I feel confident that many people do use -X with main,
so we can hardly make it stop working.

Ian

Art Mellor

unread,
Oct 4, 2016, 9:47:30 AM10/4/16
to golang-nuts, art-goo...@dontsharemyemail.com
Perhaps documenting the behavior is the best quick fix. Not knowing how 'main' gets altered - is it something that is predictable? If so, I don't think it is too terrible to document that so you can alter your command line appropriately.

I personally use the feature to pass in build numbers and version, etc. but I (without knowing of this problem) had done it in a separate version package. I imagine it would be common to use it the same way with the main package.


marcu...@grabtaxi.com

unread,
Mar 10, 2017, 9:28:21 AM3/10/17
to golang-nuts
Actually, you can do 

$ go test -v -ldflags="-X import/path/to/main.test.MyVar=hello"


Grab is hiring. Learn more at https://grab.careers

By communicating with Grab Inc and/or its subsidiaries, associate companies and jointly controlled entities (“Grab Group”), you are deemed to have consented to processing of your personal data as set out in the Privacy Notice which can be viewed at https://grab.com/privacy/

This email contains confidential information and is only for the intended recipient(s). If you are not the intended recipient(s), please do not disseminate, distribute or copy this email and notify Grab Group immediately if you have received this by mistake and delete this email from your system. Email transmission cannot be guaranteed to be secure or error-free as any information therein could be intercepted, corrupted, lost, destroyed, delayed or incomplete, or contain viruses. Grab Group do not accept liability for any errors or omissions in the contents of this email arises as a result of email transmission. All intellectual property rights in this email and attachments therein shall remain vested in Grab Group, unless otherwise provided by law.

marcu...@grabtaxi.com

unread,
Mar 12, 2017, 11:57:41 PM3/12/17
to golang-nuts
My apologies, I did not realise my work signature would automatically be added.

chioy...@gmail.com

unread,
Jun 30, 2017, 11:12:32 PM6/30/17
to golang-nuts, art-goo...@dontsharemyemail.com
Do you know where is this issue documented? I'm facing the same problem now and tried to find more information. Thanks!
Reply all
Reply to author
Forward
0 new messages