Open varunturlapati opened 4 years ago
formatting ldflags with the correct quoting is super annoying and hard. Now add that to the fact that os/exec
and thus sh
are not actual shells (despite the misleading name for sh)... and things get tricky.
The trick is that the value for -ldflags
has to exist in a single string passed as an argument to sh.Output/Run. You can't split it up by spaces.
You can see how I format it here: https://github.com/magefile/mage/blob/master/magefile.go#L55
Just ran to the same issue when inspecting the exec: message. It is super annoying that os/exec
is not an actual shell!!!! This bascially means all shell keywords like time
and exit
would have to be implemented in the golang way...
I ran into the same problem and I can't figure it out how to fix it. Here is my Build
function:
args := []string{
`-arch="` + SupportedArch + `"`,
`-osarch="` + UnsupportedOsArch + `"`,
`-os="` + SupportedOs + `"`,
`-output="` + path.Join(PkgDir, "{{.OS}}_{{.Arch}}", ProgramName) + `"`,
`-ldflags="` + ldFlags + `"`,
mainFilePath,
}
return sh.RunWithV(env, "gox", args...)
The error:
No valid platforms to build for. If you specified a value
for the 'os', 'arch', or 'osarch' flags, make sure you're
using a valid value.
Error: running "gox -arch="amd64 arm64" -osarch="!windows/arm64" -os="darwin linux windows" -output="pkg/{{.OS}}_{{.Arch}}/template-golang-project" -ldflags="-s -w" cmd/template-golang-project/main.go" failed with exit code 1
Then if I ran the same manually, everything works fine.
gox -arch="amd64 arm64" -osarch="!windows/arm64" -os="darwin linux windows" -output="pkg/{{.OS}}_{{.Arch}}/template-golang-project" -ldflags="-s -w" cmd/template-golang-project/main.go
Any ideas are welcome. :)
I setup my mage target to build locally and it worked fine. Then I wanted to introduce ldflags to get build info embedded as well. The build target errors out.
Expected: Build succeeds
Actual:
Then as an experiment I simply copied the whole command that it says it ran in the last line.
And this worked perfectly. Got the build and it reflected all the build attrs correctly.
Build function is: