Skip to content

Commit

Permalink
builder: go: Allow equal signs in env vars (#1232)
Browse files Browse the repository at this point in the history
If .slsa-goreleaser.yml contains an env var with multiple '=' signs:
```
env:
  - CGO_ENABLED=1
  - CGO_CFLAGS=-mmacosx-version-min=11.0
```

the 'build dry project' GH workflow step fails with "invalid environment
variable: CGO_CFLAGS=-mmacosx-version-min=11.0"
This is caused by the way the env vars are parsed in GoReleaserConfig:setEnvs
which only allow for a single '=' sign.

This commit fixes this by splitting the env string at the first equal sign,
first part is the env var name and won't contain '=' signs, second part
is its value and can contain any number of '='.

This also adds unit tests for this situation, and for env vars with no
values (`CGO_CFLAGS=`).

This fixes #1231

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
  • Loading branch information
cfergeau committed Nov 16, 2022
1 parent eb79b8b commit 1936957
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 3 deletions.
6 changes: 3 additions & 3 deletions internal/builders/go/pkg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ func validateVersion(cf *goReleaserConfigFile) error {
func (r *GoReleaserConfig) setEnvs(cf *goReleaserConfigFile) error {
m := make(map[string]string)
for _, e := range cf.Env {
es := strings.Split(e, "=")
if len(es) != 2 {
name, value, present := strings.Cut(e, "=")
if !present {
return fmt.Errorf("%w: %s", ErrorInvalidEnvironmentVariable, e)
}
m[es[0]] = es[1]
m[name] = value
}

if len(m) > 0 {
Expand Down
26 changes: 26 additions & 0 deletions internal/builders/go/pkg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ func Test_ConfigFromFile(t *testing.T) {
Main: asPointer("./relative/main.go"),
},
},
{
name: "valid env var with no value",
path: "./testdata/releaser-valid-envs-no-value.yml",
config: GoReleaserConfig{
Goos: "linux", Goarch: "amd64",
Flags: []string{"-trimpath", "-tags=netgo"},
Ldflags: []string{"{{ .Env.VERSION_LDFLAGS }}"},
Binary: "binary-{{ .OS }}-{{ .Arch }}",
Env: map[string]string{
"GO111MODULE": "on", "CGO_ENABLED": "0", "CGO_CFLAGS": "",
},
},
},
{
name: "valid env var with multiple = signs",
path: "./testdata/releaser-valid-envs-multiple-equal-signs.yml",
config: GoReleaserConfig{
Goos: "linux", Goarch: "amd64",
Flags: []string{"-trimpath", "-tags=netgo"},
Ldflags: []string{"{{ .Env.VERSION_LDFLAGS }}"},
Binary: "binary-{{ .OS }}-{{ .Arch }}",
Env: map[string]string{
"GO111MODULE": "on", "CGO_ENABLED": "0", "CGO_CFLAGS": "a=b=c",
},
},
},
{
name: "missing version",
path: "./testdata/releaser-noversion.yml",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 1
env:
- GO111MODULE=on
# https://stackoverflow.com/a/62821358/19407
- CGO_ENABLED=0
- CGO_CFLAGS=a=b=c

flags:
- -trimpath
- -tags=netgo

goos: linux
goarch: amd64
binary: binary-{{ .OS }}-{{ .Arch }}
ldflags:
- "{{ .Env.VERSION_LDFLAGS }}"
16 changes: 16 additions & 0 deletions internal/builders/go/pkg/testdata/releaser-valid-envs-no-value.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: 1
env:
- GO111MODULE=on
# https://stackoverflow.com/a/62821358/19407
- CGO_ENABLED=0
- CGO_CFLAGS=

flags:
- -trimpath
- -tags=netgo

goos: linux
goarch: amd64
binary: binary-{{ .OS }}-{{ .Arch }}
ldflags:
- "{{ .Env.VERSION_LDFLAGS }}"

0 comments on commit 1936957

Please sign in to comment.