Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment definitions in task files should have precedence over predefined environment variables #1053

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ tasks:

### Task

:::info
Custom environment variables will **overwrite** any predefined environment variables from your shell.
:::info

You can use `env` to set custom environment variables for a specific task:

```yaml
Expand Down
4 changes: 0 additions & 4 deletions internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ func Get(t *taskfile.Task) []string {
continue
}

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

environ = append(environ, fmt.Sprintf("%s=%s", k, str))
}

Expand Down
28 changes: 28 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"

"github.com/Masterminds/semver/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/go-task/task/v3"
"github.com/go-task/task/v3/internal/filepathext"
Expand Down Expand Up @@ -1832,3 +1834,29 @@ func TestBashShellOptsCommandLevel(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "globstar\ton\n", buff.String())
}

func TestOverwriteEnvironment(t *testing.T) {
binary := buildTask(t)

cmd := exec.Command(binary)
cmd.Dir = "testdata/env_override"
cmd.Env = []string{"PREDEFINED=initial"}
require.NoError(t, cmd.Run())

output, err := os.ReadFile("testdata/env_override/output.txt")
require.NoError(t, err)
require.Equal(t, "PREDEFINED='overwritten'", string(output))
}

func buildTask(t *testing.T) string {
temp := t.TempDir()

binary := filepath.Join(temp, "task")
command := exec.Command("go", "build", "-o", binary, "cmd/task/task.go")
var output bytes.Buffer
command.Stdout = &output
command.Stderr = &output
require.NoError(t, command.Run(), &output)

return binary
}
1 change: 1 addition & 0 deletions testdata/env_override/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.txt
10 changes: 10 additions & 0 deletions testdata/env_override/Taskfile.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'

silent: true

tasks:
default:
env:
PREDEFINED: 'overwritten'
cmds:
- echo -n "PREDEFINED='$PREDEFINED'" > output.txt