mirror of
https://github.com/go-task/task.git
synced 2025-07-17 01:43:07 +02:00
fix: make sure USER_WORKING_DIR works corrently with includes (#1309)
Closes #1046 Closes #1205 Closes #1250 Closes #1293 Closes #1274 Closes #1309 Closes #1312 Co-authored-by: Marcus Spading <ms@fragmentum.net>
This commit is contained in:
@ -6,6 +6,9 @@
|
|||||||
- Fix a missing a line break on log when using `--watch` mode (#1285, #1297 by
|
- Fix a missing a line break on log when using `--watch` mode (#1285, #1297 by
|
||||||
@FilipSolich).
|
@FilipSolich).
|
||||||
- Fix `defer` on JSON Schema (#1288 by @calvinmclean and @andreynering).
|
- 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
|
## v3.28.0 - 2023-07-24
|
||||||
|
|
||||||
|
@ -3,17 +3,43 @@ package filepathext
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SmartJoin joins two paths, but only if the second is not already an
|
// SmartJoin joins two paths, but only if the second is not already an
|
||||||
// absolute path.
|
// absolute path.
|
||||||
func SmartJoin(a, b string) string {
|
func SmartJoin(a, b string) string {
|
||||||
if filepath.IsAbs(b) {
|
if IsAbs(b) {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
return filepath.Join(a, 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
|
// TryAbsToRel tries to convert an absolute path to relative based on the
|
||||||
// process working directory. If it can't, it returns the absolute path.
|
// process working directory. If it can't, it returns the absolute path.
|
||||||
func TryAbsToRel(abs string) string {
|
func TryAbsToRel(abs string) string {
|
||||||
|
8
setup.go
8
setup.go
@ -175,13 +175,17 @@ func (e *Executor) setupCompiler() error {
|
|||||||
Logger: e.Logger,
|
Logger: e.Logger,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
userWorkingDir, err := os.Getwd()
|
if e.UserWorkingDir == "" {
|
||||||
|
var err error
|
||||||
|
e.UserWorkingDir, err = os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
e.Compiler = &compilerv3.CompilerV3{
|
e.Compiler = &compilerv3.CompilerV3{
|
||||||
Dir: e.Dir,
|
Dir: e.Dir,
|
||||||
UserWorkingDir: userWorkingDir,
|
UserWorkingDir: e.UserWorkingDir,
|
||||||
TaskfileEnv: e.Taskfile.Env,
|
TaskfileEnv: e.Taskfile.Env,
|
||||||
TaskfileVars: e.Taskfile.Vars,
|
TaskfileVars: e.Taskfile.Vars,
|
||||||
Logger: e.Logger,
|
Logger: e.Logger,
|
||||||
|
1
task.go
1
task.go
@ -72,6 +72,7 @@ type Executor struct {
|
|||||||
Output output.Output
|
Output output.Output
|
||||||
OutputStyle taskfile.Output
|
OutputStyle taskfile.Output
|
||||||
TaskSorter sort.TaskSorter
|
TaskSorter sort.TaskSorter
|
||||||
|
UserWorkingDir string
|
||||||
|
|
||||||
taskvars *taskfile.Vars
|
taskvars *taskfile.Vars
|
||||||
fuzzyModel *fuzzy.Model
|
fuzzyModel *fuzzy.Model
|
||||||
|
20
task_test.go
20
task_test.go
@ -1940,6 +1940,26 @@ func TestUserWorkingDirectory(t *testing.T) {
|
|||||||
assert.Equal(t, fmt.Sprintf("%s\n", wd), buff.String())
|
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) {
|
func TestPlatforms(t *testing.T) {
|
||||||
var buff bytes.Buffer
|
var buff bytes.Buffer
|
||||||
e := task.Executor{
|
e := task.Executor{
|
||||||
|
@ -153,7 +153,7 @@ func (it *IncludedTaskfile) resolvePath(path string) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if filepath.IsAbs(path) {
|
if filepathext.IsAbs(path) {
|
||||||
return path, nil
|
return path, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
testdata/user_working_dir_with_includes/Taskfile.yml
vendored
Normal file
5
testdata/user_working_dir_with_includes/Taskfile.yml
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
includes:
|
||||||
|
included:
|
||||||
|
taskfile: ./included/Taskfile.yml
|
8
testdata/user_working_dir_with_includes/included/Taskfile.yml
vendored
Normal file
8
testdata/user_working_dir_with_includes/included/Taskfile.yml
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
version: '3'
|
||||||
|
|
||||||
|
tasks:
|
||||||
|
echo:
|
||||||
|
dir: '{{.USER_WORKING_DIR}}'
|
||||||
|
cmds:
|
||||||
|
- pwd
|
||||||
|
silent: true
|
0
testdata/user_working_dir_with_includes/somedir/.keep
vendored
Normal file
0
testdata/user_working_dir_with_includes/somedir/.keep
vendored
Normal file
Reference in New Issue
Block a user