Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: -ldflags=-X=VAL does not work #29719

Closed
greenpau opened this issue Jan 13, 2019 · 5 comments
Closed

cmd/compile: -ldflags=-X=VAL does not work #29719

greenpau opened this issue Jan 13, 2019 · 5 comments

Comments

@greenpau
Copy link

@greenpau greenpau commented Jan 13, 2019

What version of Go are you using (go version)?

$ go version
go version go1.10.5 linux/amd64

Does this issue reproduce with the latest release?

The same result is with 1.10.7.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN="/home/greenpau/dev/go/bin"
GOCACHE="/home/greenpau/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/greenpau/dev/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build378269758=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I run a build with the following ldflags:

https://github.com/greenpau/network_exporter/blob/master/Makefile#L19-L34

What did you expect to see?

My code depends on external llibrary github.com/prometheus/common. As part of the build, I set the values for the following variables https://github.com/prometheus/common/blob/master/version/info.go#L27-L34.

During the startup of the application, I expect seeing version, branch, revision, etc. information.

Version: 1.0.0, Branch: master, Revision: 786432c-dirty
Build on 2019-01-13 by greenpau

What did you see instead?

Instead I see that ldflags didn't work.

$ make qtest
network-exporter
INFO[0000] Starting network-exporter (version=, branch=, revision=)  source="main.go:74"
INFO[0000] Build context (go=go1.10.5, user=, date=)     source="main.go:75"
INFO[0000] Listening on 0.0.0.0:9533                     source="main.go:101"

$ make qtest
network-exporter
INFO[0000] Starting network-exporter (version=, branch=, revision=)  source="main.go:74"
INFO[0000] Build context (go=go1.10.7, user=, date=)     source="main.go:75"

@kindermoumoute
Copy link

@kindermoumoute kindermoumoute commented Jan 14, 2019

Same issue with Go 1.11.4

go env

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/username/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/username/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.11.4/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.11.4/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/username/work/project/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/xz/m_txgcw11956v_f03r_xz4880000gn/T/go-build167362921=/tmp/go-build -gno-record-gcc-switches -fno-common"

@agnivade
Copy link
Contributor

@agnivade agnivade commented Jan 24, 2019

I am unable to reproduce this -

$make qtest
network-exporter 1.0.0, commit: 563169a
INFO[0000] Starting network-exporter (version=1.0.0, branch=master, revision=563169a)  source="main.go:74"
INFO[0000] Build context (go=go1.11.2, user=agniva, date=2019-01-24)  source="main.go:75"
$PATH=~/sdk/go1.12beta1/bin:$PATH make qtest
network-exporter 1.0.0, commit: 563169a
INFO[0000] Starting network-exporter (version=1.0.0, branch=master, revision=563169a)  source="main.go:74"
INFO[0000] Build context (go=go1.12beta1, user=agniva, date=2019-01-24)  source="main.go:75"

Are you sure this is not a bash/shell issue ?

@greenpau
Copy link
Author

@greenpau greenpau commented Jan 25, 2019

Are you sure this is not a bash/shell issue ?

@agnivade , pretty sure.

@agnivade
Copy link
Contributor

@agnivade agnivade commented Jan 25, 2019

Then I am not sure what is happening here. Could you make a stripped down main.go which just imports github.com/prometheus/common/version and just do version.Print("hello") from main.go ?

package main

import (
	"fmt"

	"github.com/prometheus/common/version"
)

func main() {
	fmt.Println(version.Print("hello"))
}
go run -ldflags "-s -w -X github.com/prometheus/common/version.Version=blabla" bidirec.go 
hello, version blabla (branch: , revision: )
  build user:       
  build date:       
  go version:       go1.11.2
@gopherbot
Copy link

@gopherbot gopherbot commented Mar 1, 2019

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@gopherbot gopherbot closed this Mar 1, 2019
@golang golang locked and limited conversation to collaborators Feb 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants