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
go get -ldflags
update the binary, but -ldflags
not working
#42482
Comments
Show the actual code you are running |
This program is a tool built upon cobra. The most relevant code is as following: private repository: ${private-git-host}/gitcode, when I run gitcode has a subcmd package cmd
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"${private-git-host|/git-sdk-go"
"github.com/Masterminds/semver"
"github.com/spf13/cobra"
)
// updateCmd represents the update command
var updateCmd = &cobra.Command{
Use: "update",
Short: "update this tool",
Long: `update this tool.`,
RunE: func(cmd *cobra.Command, args []string) error {
// load 1st-page tag list
rsp, err := tgitClient.TagService().ListTag(context.Background(), &tgit.ListTagReq{
ProjectID: tgit.ProjectID{Path: "content_platform_center/gitcode"},
Page: 1,
PerPage: 10,
})
if err != nil {
return err
}
// get the newest tag version and commit
var (
tagVersion = "v0.0.0"
tagCommit = "xxxxxxxx"
)
for _, tag := range *rsp {
old, err := semver.NewVersion(tagVersion)
if err != nil {
return err
}
v, err := semver.NewVersion(tag.Name)
if err != nil {
return err
}
if v.GreaterThan(old) {
tagVersion = tag.Name
tagCommit = tag.Commit.ShortID
}
}
// build artifact version: ${tag number}-${commit hash}
buildver := fmt.Sprintf(
`-ldflags="-X {private-git-host}/gitcode/config.VERSION=%s-%s"`,
tagVersion, tagCommit)
// install with linker options, updating the config.VERSION
updateArgs := []string{"go", "get", buildver, "${private-git-host}/gitcode@" + tagVersion}
// run `go get` to update this tool
command := exec.Command(updateArgs[0], updateArgs[1:]...)
fmt.Println(strings.Join(updateCmd.Args, " "))
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Stdin = os.Stdin
err = command.Run()
if err != nil {
return err
}
return nil
},
}
func init() {
rootCmd.AddCommand(updateCmd)
} |
2 issues:
buildver := fmt.Sprintf(
`-ldflags=-X main.VERSION=%s-%s`,
tagVersion, tagCommit) |
@seankhliao Thanks.
After change I am still a little confused, here. I'll appreciate If you explain in details.
Actually, we could update any package not just main package, |
|
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
No, I didn't test it.
I just want to know is there a way to work around this problem.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
github.com/xxx/cmd
, and tagged v1.0.0cmd update
, it will create an exec.Command to rungo get -ldflags="-X github.com/xxx/cmd.VERSION=${tagno}" github.com/xxx/cmd@${tagno}
. The ${tagno} is got by query taglist, which seems OK.cmd update
, indeed, it download the new tag and build, but the-ldflags
seems not working, but when I run thego get -ldflags...
command manually, it works.In this occasion,
cmd
updatecmd
itself, maybe it's a special case. I want to know is this a known problem?What did you expect to see?
I want to see
-ldflags
works, that means cmd.VERSION is set to ${tagno}.What did you see instead?
I see cmd.VERSION is not set.
The text was updated successfully, but these errors were encountered: