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

fix: make sure USER_WORKING_DIR works corrently with includes #1309

Merged
merged 1 commit into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
- Fix a missing a line break on log when using `--watch` mode (#1285, #1297 by
@FilipSolich).
- Fix `defer` on JSON Schema (#1288 by @calvinmclean and @andreynering).
- Fix bug in usage of special variables like `{{.USER_WORKING_DIR}}` in
combination with `includes` (#1046, #1205, #1250, #1293, #1312, #1274 by
@andarto, #1309 by @andreynering).

## v3.28.0 - 2023-07-24

Expand Down
28 changes: 27 additions & 1 deletion internal/filepathext/filepathext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,43 @@ package filepathext
import (
"os"
"path/filepath"
"strings"
)

// SmartJoin joins two paths, but only if the second is not already an
// absolute path.
func SmartJoin(a, b string) string {
if filepath.IsAbs(b) {
if IsAbs(b) {
return b
}
return filepath.Join(a, b)
}

func IsAbs(path string) bool {
// NOTE(@andreynering): If the path contains any if the special
// variables that we know are absolute, return true.
if isSpecialDir(path) {
return true
}

return filepath.IsAbs(path)
}

var knownAbsDirs = []string{
".ROOT_DIR",
".TASKFILE_DIR",
".USER_WORKING_DIR",
}

func isSpecialDir(dir string) bool {
for _, d := range knownAbsDirs {
if strings.Contains(dir, d) {
return true
}
}
return false
}

// TryAbsToRel tries to convert an absolute path to relative based on the
// process working directory. If it can't, it returns the absolute path.
func TryAbsToRel(abs string) string {
Expand Down
12 changes: 8 additions & 4 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,17 @@ func (e *Executor) setupCompiler() error {
Logger: e.Logger,
}
} else {
userWorkingDir, err := os.Getwd()
if err != nil {
return err
if e.UserWorkingDir == "" {
var err error
e.UserWorkingDir, err = os.Getwd()
if err != nil {
return err
}
}

e.Compiler = &compilerv3.CompilerV3{
Dir: e.Dir,
UserWorkingDir: userWorkingDir,
UserWorkingDir: e.UserWorkingDir,
TaskfileEnv: e.Taskfile.Env,
TaskfileVars: e.Taskfile.Vars,
Logger: e.Logger,
Expand Down
11 changes: 6 additions & 5 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ type Executor struct {
Stdout io.Writer
Stderr io.Writer

Logger *logger.Logger
Compiler compiler.Compiler
Output output.Output
OutputStyle taskfile.Output
TaskSorter sort.TaskSorter
Logger *logger.Logger
Compiler compiler.Compiler
Output output.Output
OutputStyle taskfile.Output
TaskSorter sort.TaskSorter
UserWorkingDir string

taskvars *taskfile.Vars
fuzzyModel *fuzzy.Model
Expand Down
20 changes: 20 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,26 @@ func TestUserWorkingDirectory(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%s\n", wd), buff.String())
}

func TestUserWorkingDirectoryWithIncluded(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)

wd = filepathext.SmartJoin(wd, "testdata/user_working_dir_with_includes/somedir")

var buff bytes.Buffer
e := task.Executor{
UserWorkingDir: wd,
Dir: "testdata/user_working_dir_with_includes",
Stdout: &buff,
Stderr: &buff,
}

require.NoError(t, err)
require.NoError(t, e.Setup())
require.NoError(t, e.Run(context.Background(), taskfile.Call{Task: "included:echo"}))
assert.Equal(t, fmt.Sprintf("%s\n", wd), buff.String())
}

func TestPlatforms(t *testing.T) {
var buff bytes.Buffer
e := task.Executor{
Expand Down
2 changes: 1 addition & 1 deletion taskfile/included_taskfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (it *IncludedTaskfile) resolvePath(path string) (string, error) {
return "", err
}

if filepath.IsAbs(path) {
if filepathext.IsAbs(path) {
return path, nil
}

Expand Down
5 changes: 5 additions & 0 deletions testdata/user_working_dir_with_includes/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
version: '3'

includes:
included:
taskfile: ./included/Taskfile.yml
8 changes: 8 additions & 0 deletions testdata/user_working_dir_with_includes/included/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: '3'

tasks:
echo:
dir: '{{.USER_WORKING_DIR}}'
cmds:
- pwd
silent: true
Empty file.