Problem running go install with "-ldflags" via "os/exec"

408 views
Skip to first unread message

Martin Bertschler

unread,
Sep 3, 2015, 5:59:23 PM9/3/15
to golang-nuts

Hello Gophers,


I try to write an automatic builder that embeds the build time and version in the program with the -ldflags -X option, but I have a problem running go install with the arguments.


My program:

package main

import "fmt"

var (
    BuildTime    string
    BuildVersion string
)

func main() {
    fmt.Println("Hello, World :)")
    fmt.Println("Build time:", BuildTime)
    fmt.Println("Build version:", BuildVersion)
}

My builder:

package main

import (
    "fmt"
    "os"
    "os/exec"
    "path/filepath"
    "time"
)

var version = "v1.2.3"

func main() {
    Cmd("go", "install", "program")
    Cmd("program")

    err := os.Remove(filepath.Join(os.ExpandEnv("$GOPATH"), "bin", "program"))
    if err != nil {
        fmt.Println("Remove error:", err)
    }
    Cmd("go", "install", "-ldflags",
        "'-X \"main.BuildTime="+time.Now().Format(time.UnixDate)+
            "\" -X \"main.BuildVersion="+version+"\"'",
        "program")
    Cmd("program")
}

func Cmd(args ...string) {
    fmt.Println("Running:", args)
    cmd := exec.Command(args[0], args[1:]...)
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    err := cmd.Run()
    if err != nil {
        fmt.Println("Error:", err, args)
    }
}

But when I run my builder I only get the following output (emphasis mine):

Running: [go install program]
Running: [program]
Hello, World :)
Build time: 
Build version: 
Running: [go install -ldflags '-X "main.BuildTime=Thu Sep  3 23:06:44 CEST 2015" -X "main.BuildVersion=v1.2.3"' program]
# program
flag provided but not defined: -X "main.BuildTime
usage: link [options] main.o
  flags and options...
Error: exit status 2 [go install -ldflags '-X "main.BuildTime=Thu Sep  3 23:06:44 CEST 2015" -X "main.BuildVersion=v1.2.3"' program]
Running: [program]
Error: exec: "program": executable file not found in $PATH [program]

I can run this command without problems on my terminal go install -ldflags '-X "main.BuildTime=Thu Sep 3 23:06:44 CEST 2015" -X "main.BuildVersion=v1.2.3"' program but I obviously have some problems in the argument list in my builder.


Does anyone have an idea how this could be fixed?


Thank you
-Martin

James Bardin

unread,
Sep 3, 2015, 7:16:21 PM9/3/15
to golang-nuts
Try removing the extra single quotes around the -ldflags argument.
Message has been deleted

Martin Bertschler

unread,
Sep 3, 2015, 8:24:11 PM9/3/15
to golang-nuts
I could have sworn that I already tried that, but that fixed my program. Thank you James!

But then the command failed in my shell:
$ go install -ldflags -"main.BuildTime=Fri Sep  4 02:09:39 CEST 2015" -"main.BuildVersion=v1.2.3" exp/program
can
't load package: package main.BuildTime=Fri Sep  4 02:09:39 CEST 2015: cannot find package "main.BuildTime=Fri Sep  4 02:09:39 CEST 2015" in any of:

Okay so I just learned something new about the shell... "Inside single quotes everything is preserved without exception. You can't even escape single quotes."
So what happens now as far as I can understand is by using the single quotes (that I got from here: Issue 12338 ) the shell interprets this quoted block as a single argument, but because I already put the ldflags into one element of my args slice instead of multiple ones, the single quotes are unnecessary and make the program fail.

Does that sound plausible?

Thanks again for the quick help :)

-Martin

(Google groups just showed my message twice, so I deleted one but then both messages were gone...)
Reply all
Reply to author
Forward
0 new messages