Skip to content

sh.Run and sh.Output sh.OutCmd fail on a go build command but running it directly on local machine works #304

@varunturlapati

Description

@varunturlapati

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:

go: finding github.myorg.com/myrepo/heyo/version.BuildSha=4accbfe latest
go: finding github.myorg.com/myrepo/heyo/version.ReleaseType=localdev' latest
go: finding github.myorg.com/myrepo/heyo/version.BuildVersion=local latest
can't load package: package github.myorg.com/myrepo/heyo/version.BuildDate=20200505: no matching versions for query "latest"
can't load package: package -X: malformed module path "-X": leading dash
can't load package: package github.myorg.com/myrepo/heyo/version.BuildSha=4accbfe: no matching versions for query "latest"
can't load package: package github.myorg.com/myrepo/heyo/version.BuildVersion=local: no matching versions for query "latest"
can't load package: package github.myorg.com/myrepo/heyo/version.ReleaseType=localdev': no matching versions for query "latest"
can't load package: package -os: malformed module path "-os": leading dash
can't load package: package darwin: malformed module path "darwin": missing dot in first path element
can't load package: package -arch: malformed module path "-arch": leading dash
can't load package: package amd64: malformed module path "amd64": missing dot in first path element
can't load package: package -output: malformed module path "-output": leading dash
can't load package: package targets/ocs_heyo_darwin_amd64: malformed module path "targets/ocs_heyo_darwin_amd64": missing dot in first path element

Error: running "gox -ldflags '-X github.myorg.com/myrepo/heyo/version.BuildDate=20200505 -X github.myorg.com/myrepo/heyo/version.BuildSha=4accbfe -X github.myorg.com/myrepo/heyo/version.BuildVersion=local -X github.myorg.com/myrepo/heyo/version.ReleaseType=localdev' -os darwin -arch amd64 -output targets/ocs_heyo_darwin_amd64 github.myorg.com/myrepo/heyo/cmd" failed with exit code 1

Then as an experiment I simply copied the whole command that it says it ran in the last line.

gox -ldflags '-X github.myorg.com/myrepo/heyo/version.BuildDate=20200505 -X github.myorg.com/myrepo/heyo/version.BuildSha=4accbfe -X github.myorg.com/myrepo/heyo/version.BuildVersion=local -X github.myorg.com/myrepo/heyo/version.ReleaseType=localdev' -os darwin -arch amd64 -output targets/ocs_heyo_darwin_amd64 github.myorg.com/myrepo/heyo/cmd

And this worked perfectly. Got the build and it reflected all the build attrs correctly.

Build function is:

func build(b *BuildOpts) error {
	if b == nil {
		fmt.Println("No BuildOpts specified. Will use defaults.")
		b = DefaultBuildOpts
	}
	if _, ok := Os2BuildArgsMap[b.Goos]; !ok {
		return fmt.Errorf("your local machine os %s is not supported in builds. Add it in constants.go or see why the OS is not"+
			"recognized", b.Goos)
	}
	var ldPrefixedArgs = strings.Split(PrepareLdFlagsFromBuildOpts(b), " ")
	ldPrefixedArgs = append(ldPrefixedArgs, Os2BuildArgsMap[b.Goos]...)
	fmt.Printf("%d --> %v", len(ldPrefixedArgs), ldPrefixedArgs)
	//os.Exit(1)
	out, err := sh.Output(Gox, ldPrefixedArgs...) // TODO looping to create all in a BuildAll target
	if err != nil {
		fmt.Println(out)
		return err
	}
	return nil
}
func PrepareLdFlagsFromBuildOpts(b *BuildOpts) string {
    var flags = []string{
        fmt.Sprintf("-X github.myorg.com/myrepo/heyo/version.BuildDate=%v", b.Timestamp),
        fmt.Sprintf("-X github.myorg.com/myrepo/heyo/version.BuildSha=%v", b.Sha),
        fmt.Sprintf("-X github.myorg.com/myrepo/heyo/version.BuildVersion=%v", b.Version),
        fmt.Sprintf("-X github.myorg.com/myrepo/heyo/version.ReleaseType=%v", b.Reltype),
    }
    return fmt.Sprintf("-ldflags '%s'", strings.Join(flags, " "))
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions