diff --git a/CHANGELOG.md b/CHANGELOG.md index a70ce149fb..ff5a657655 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +- Allow calling a task of the root Taskfile from an included Taskfile + by prefixing it with `:` + ([#161](https://github.com/go-task/task/issues/161), [#172](https://github.com/go-task/task/issues/172)). + ## v2.3.0 - 2019-01-02 - On Windows, Task can now be installed using [Scoop](https://scoop.sh/) diff --git a/docs/usage.md b/docs/usage.md index ab291428e1..b334595cdb 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -258,6 +258,10 @@ tasks: The above syntax is also supported in `deps`. +> NOTE: If you want to call a task declared in the root Taskfile from within an +> [included Taskfile](#including-other-taskfiles), add a leading `:` like this: +> `task: :task-name`. + ## Prevent unnecessary work If a task generates something, you can inform Task the source and generated diff --git a/internal/taskfile/merge.go b/internal/taskfile/merge.go index ac4dbd773b..b9f415ba0c 100644 --- a/internal/taskfile/merge.go +++ b/internal/taskfile/merge.go @@ -66,5 +66,8 @@ func Merge(t1, t2 *Taskfile, namespaces ...string) error { } func taskNameWithNamespace(taskName string, namespaces ...string) string { + if strings.HasPrefix(taskName, ":") { + return strings.TrimPrefix(taskName, ":") + } return strings.Join(append(namespaces, taskName), NamespaceSeparator) } diff --git a/task_test.go b/task_test.go index 02f7b0ff86..76dcdb4685 100644 --- a/task_test.go +++ b/task_test.go @@ -511,3 +511,15 @@ func TestIncludesDependencies(t *testing.T) { } tt.Run(t) } + +func TestIncludesCallingRoot(t *testing.T) { + tt := fileContentTest{ + Dir: "testdata/includes_call_root_task", + Target: "included:call-root", + TrimSpace: true, + Files: map[string]string{ + "root_task.txt": "root task", + }, + } + tt.Run(t) +} diff --git a/testdata/includes_call_root_task/.gitignore b/testdata/includes_call_root_task/.gitignore new file mode 100644 index 0000000000..2211df63dd --- /dev/null +++ b/testdata/includes_call_root_task/.gitignore @@ -0,0 +1 @@ +*.txt diff --git a/testdata/includes_call_root_task/Taskfile.yml b/testdata/includes_call_root_task/Taskfile.yml new file mode 100644 index 0000000000..41e60ad03e --- /dev/null +++ b/testdata/includes_call_root_task/Taskfile.yml @@ -0,0 +1,9 @@ +version: '2' + +includes: + included: Taskfile2.yml + +tasks: + root-task: + cmds: + - echo "root task" > root_task.txt diff --git a/testdata/includes_call_root_task/Taskfile2.yml b/testdata/includes_call_root_task/Taskfile2.yml new file mode 100644 index 0000000000..99d5cb3a07 --- /dev/null +++ b/testdata/includes_call_root_task/Taskfile2.yml @@ -0,0 +1,6 @@ +version: '2' + +tasks: + call-root: + cmds: + - task: :root-task