Skip to content

Commit

Permalink
feat: experiment taskfile envs take precedence over os envs
Browse files Browse the repository at this point in the history
  • Loading branch information
vmaerten committed May 5, 2024
1 parent cd259a7 commit e80aa9a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 7 deletions.
9 changes: 5 additions & 4 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import (
"fmt"
"os"

"github.com/go-task/task/v3/internal/experiments"
"github.com/go-task/task/v3/taskfile/ast"
)

func Get(t *ast.Task) []string {
if t.Env == nil {
return nil
}

environ := os.Environ()

for k, v := range t.Env.ToCacheMap() {
str, isString := v.(string)
if !isString {
continue
}

if _, alreadySet := os.LookupEnv(k); alreadySet {
continue
if !experiments.EnvPrecedence.Enabled {
if _, alreadySet := os.LookupEnv(k); alreadySet {
continue
}
}

environ = append(environ, fmt.Sprintf("%s=%s", k, str))
Expand Down
3 changes: 3 additions & 0 deletions internal/experiments/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
RemoteTaskfiles Experiment
AnyVariables Experiment
MapVariables Experiment
EnvPrecedence Experiment
)

func init() {
Expand All @@ -37,6 +38,7 @@ func init() {
RemoteTaskfiles = New("REMOTE_TASKFILES")
AnyVariables = New("ANY_VARIABLES", "1", "2")
MapVariables = New("MAP_VARIABLES", "1", "2")
EnvPrecedence = New("ENV_PRECEDENCE")
}

func New(xName string, enabledValues ...string) Experiment {
Expand Down Expand Up @@ -104,5 +106,6 @@ func List(l *logger.Logger) error {
printExperiment(w, l, GentleForce)
printExperiment(w, l, RemoteTaskfiles)
printExperiment(w, l, MapVariables)
printExperiment(w, l, EnvPrecedence)
return w.Flush()
}
21 changes: 18 additions & 3 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ func (fct fileContentTest) Run(t *testing.T) {
for f := range fct.Files {
_ = os.Remove(filepathext.SmartJoin(fct.Dir, f))
}

t.Setenv("TASK_X_ENV_PRECEDENCE", "1")
e := &task.Executor{
Dir: fct.Dir,
TempDir: filepathext.SmartJoin(fct.Dir, ".task"),
Entrypoint: fct.Entrypoint,
Stdout: io.Discard,
Stderr: io.Discard,
}

require.NoError(t, e.Setup(), "e.Setup()")
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: fct.Target}), "e.Run(target)")

Expand Down Expand Up @@ -96,16 +97,30 @@ func TestEmptyTask(t *testing.T) {
}

func TestEnv(t *testing.T) {
t.Setenv("QUX", "from_env")
tt := fileContentTest{
Dir: "testdata/env",
Target: "default",
TrimSpace: false,
Files: map[string]string{
"local.txt": "GOOS='linux' GOARCH='amd64' CGO_ENABLED='0'\n",
"global.txt": "FOO='foo' BAR='overriden' BAZ='baz'\n",
"local.txt": "GOOS='linux' GOARCH='amd64' CGO_ENABLED='0'\n",
"global.txt": "FOO='foo' BAR='overriden' BAZ='baz'\n",
"not-overriden.txt": "QUX='from_env'\n",
},
}
tt.Run(t)

t.Setenv("TASK_X_ENV_PRECEDENCE", "1")

ttt := fileContentTest{
Dir: "testdata/env",
Target: "overriden",
TrimSpace: false,
Files: map[string]string{
"overriden.txt": "QUX='from_taskfile'\n",
},
}
ttt.Run(t)
}

func TestVars(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions testdata/env/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ env:
FOO: foo
BAR: bar
BAZ: "{{.BAZ}}"
QUX: from_taskfile

tasks:
default:
cmds:
- task: local
- task: global
- task: not-overriden

local:
vars:
Expand All @@ -31,3 +33,11 @@ tasks:
BAR: overriden
cmds:
- echo "FOO='$FOO' BAR='$BAR' BAZ='$BAZ'" > global.txt

not-overriden:
cmds:
- echo "QUX='$QUX'" > not-overriden.txt

overriden:
cmds:
- echo "QUX='$QUX'" > overriden.txt
65 changes: 65 additions & 0 deletions website/docs/experiments/env_precedence.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
draft: false # Hide in production
slug: '/experiments/env-precedence'
---

# Env Precedence (#1038)

:::caution

All experimental features are subject to breaking changes and/or removal _at any
time_. We strongly recommend that you do not use these features in a production
environment. They are intended for testing and feedback only.

:::

:::warning

This experiment breaks the following functionality:

- environment variable will take precedence over OS environment variables

:::

:::info

To enable this experiment, set the environment variable: `TASK_X_ENV_PRECEDENCE=1`.
Check out [our guide to enabling experiments ][enabling-experiments] for more
information.

:::

Before this experiment, the OS variable took precedence over the task environment variable. This experiment changes the precedence to make the task environment variable take precedence over the OS variable.

Consider the following example:

```yml
version: '3'

tasks:
default:
env:
KEY: 'other'
cmds:
- echo "$KEY"
```
Running `KEY=some task` before this experiment, the output would be `some`, but after this experiment, the output would be `other`.

If you still want to get the OS variable, you can use the template function env like follow : `{{env.OS_VAR}}`.

```yml
version: '3'

tasks:
default:
env:
KEY: 'other'
cmds:
- echo "$KEY"
- echo {{env "KEY"}}
```
Running `KEY=some task`, the output would be `other` and `some`.

{/* prettier-ignore-start */}
[enabling-experiments]: ./experiments.mdx#enabling-experiments
{/* prettier-ignore-end */}

0 comments on commit e80aa9a

Please sign in to comment.