Repository: goreleaser/goreleaser
Pull request #3591 (2022-11-24 19:08:59)

fix: validate env templates
ignore invalid environment variables (e.g. no key, no value, or no equal sign)

codecov[bot]: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/3591?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) Report
> Merging [#3591](https://codecov.io/gh/goreleaser/goreleaser/pull/3591?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (ad52cb2) into [main](https://codecov.io/gh/goreleaser/goreleaser/commit/9abc613ad1e205d6eab4ed977e15bf4e48c8abee?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) (9abc613) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##             main    #3591   +/-   ##
=======================================
  Coverage   83.87%   83.87%           
=======================================
  Files         117      117           
  Lines        9750     9751    +1     
=======================================
+ Hits         8178     8179    +1     
  Misses       1268     1268           
  Partials      304      304           
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/3591?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser) | Coverage Δ | |
|---|---|---|
| [internal/tmpl/tmpl.go](https://codecov.io/gh/goreleaser/goreleaser/pull/3591/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser#diff-aW50ZXJuYWwvdG1wbC90bXBsLmdv) | `97.84% <100.00%> (+0.01%)` | :arrow_up: |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=goreleaser)

caarlos0: not really sure this is needed btw

Patch:
diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go
index e6371a95..1fda1603 100644
--- a/internal/tmpl/tmpl.go
+++ b/internal/tmpl/tmpl.go
@@ -119,7 +119,10 @@ func New(ctx *context.Context) *Template {
 func (t *Template) WithEnvS(envs []string) *Template {
 	result := map[string]string{}
 	for _, env := range envs {
-		k, v, _ := strings.Cut(env, "=")
+		k, v, ok := strings.Cut(env, "=")
+		if !ok || k == "" {
+			continue
+		}
 		result[k] = v
 	}
 	return t.WithEnv(result)
diff --git a/www/docs/customization/build.md b/www/docs/customization/build.md
index 472cf50c..c3dbbcad 100644
--- a/www/docs/customization/build.md
+++ b/www/docs/customization/build.md
@@ -71,6 +71,8 @@ builds:
 
     # Custom environment variables to be set during the builds.
     #
+    # Invalid environment variables will be ignored.
+    #
     # Default: `os.Environ()` merged with what you set the root `env` section.
     env:
       - CGO_ENABLED=0


Test patch:
diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go
index 14f8bb16..bccb35ec 100644
--- a/internal/tmpl/tmpl_test.go
+++ b/internal/tmpl/tmpl_test.go
@@ -159,12 +159,21 @@ func TestWithEnv(t *testing.T) {
 		"FOO": "BAR",
 	}
 	ctx.Git.CurrentTag = "v1.2.3"
-	out, err := New(ctx).WithEnvS([]string{
+	tpl := New(ctx).WithEnvS([]string{
 		"FOO=foo",
 		"BAR=bar",
-	}).Apply("{{ .Env.FOO }}-{{ .Env.BAR }}")
+		"NOVAL=",
+		"=NOKEY",
+		"=",
+		"NOTHING",
+	})
+	out, err := tpl.Apply("{{ .Env.FOO }}-{{ .Env.BAR }}")
 	require.NoError(t, err)
 	require.Equal(t, "foo-bar", out)
+
+	out, err = tpl.Apply(`{{ range $idx, $key := .Env }}{{ $idx }},{{ end }}`)
+	require.NoError(t, err)
+	require.Equal(t, "BAR,FOO,NOVAL,", out)
 }
 
 func TestFuncMap(t *testing.T) {