1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

fix(golangBuild): order of arguments (#3645)

* fix: order of go build arguments

As per https://pkg.go.dev/cmd/go#hdr-Compile_packages_and_dependencies 
The `go build [-o output] [build flags] [packages] ` -ldflags shall be put before packages.

The build may fail this way
```
running command: go build -trimpath -o foo-linux.amd64 ./cmd/main.go -ldflags '-linkmode=external'
named files must be .go files: -ldflags

```
This commit is contained in:
Maximilian Braun 2022-03-18 12:03:45 +01:00 committed by GitHub
parent 2a4052d13c
commit 040a2c36ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 2 deletions

View File

@ -446,10 +446,10 @@ func runGolangBuildPerArchitecture(config *golangBuildOptions, utils golangBuild
buildOptions = append(buildOptions, "-o", binaryName)
}
buildOptions = append(buildOptions, config.BuildFlags...)
buildOptions = append(buildOptions, config.Packages...)
if len(ldflags) > 0 {
buildOptions = append(buildOptions, "-ldflags", ldflags)
}
buildOptions = append(buildOptions, config.Packages...)
if err := utils.RunExecutable("go", buildOptions...); err != nil {
log.Entry().Debugf("buildOptions: %v", buildOptions)

View File

@ -98,6 +98,7 @@ func TestRunGolangBuild(t *testing.T) {
config := golangBuildOptions{
RunTests: true,
LdflagsTemplate: "test",
Packages: []string{"package/foo"},
TargetArchitectures: []string{"linux,amd64"},
}
utils := newGolangBuildTestsUtils()
@ -110,7 +111,7 @@ func TestRunGolangBuild(t *testing.T) {
assert.Equal(t, "gotestsum", utils.ExecMockRunner.Calls[1].Exec)
assert.Equal(t, []string{"--junitfile", "TEST-go.xml", "--", fmt.Sprintf("-coverprofile=%v", coverageFile), "./..."}, utils.ExecMockRunner.Calls[1].Params)
assert.Equal(t, "go", utils.ExecMockRunner.Calls[2].Exec)
assert.Equal(t, []string{"build", "-trimpath", "-ldflags", "test"}, utils.ExecMockRunner.Calls[2].Params)
assert.Equal(t, []string{"build", "-trimpath", "-ldflags", "test", "package/foo"}, utils.ExecMockRunner.Calls[2].Params)
})
t.Run("success - tests with coverage", func(t *testing.T) {