go-task / task

A task runner / simpler Make alternative written in Go
https://taskfile.dev
MIT License
11.01k stars 584 forks source link

Included taskfiles don’t use provided vars while evaluating their vars #1295

Open JonZeolla opened 1 year ago

JonZeolla commented 1 year ago

I expected the included taskfile to exit 1 because it uses the passed CODE var that was passed to it, but it seems to be unset and uses the default instead.

Taskfile.yml:

version: '3'

includes:
  example:
    taskfile: ./Taskfile.included.yml
    vars:
      CODE: '{{.CODE | default 2}}'

vars:
  CODE: 1

tasks:
  echo-code:
    cmds:
      - 'echo {{.CODE | default 4}}'

Taskfile.included.yml:

version: '3'

vars:
  CODE:
    sh: 'exit {{.CODE | default 3}}'

Expected output

$ task echo-code
task: Command "exit 1" failed: exit status 1

Actual output

$ task echo-code
task: Command "exit 3" failed: exit status 3
JonZeolla commented 1 year ago

I was hopeful that reordering the Taskfile.yml to put vars before the includes section but that didn't work either.

JonZeolla commented 1 year ago

In doing more testing, it seems that the problem is that includes may be running before vars. Here's a test diff which shows the issue:

diff --git a/testdata/include_with_vars/Taskfile.yml b/testdata/include_with_vars/Taskfile.yml
index aee8f51..96c799a 100644
--- a/testdata/include_with_vars/Taskfile.yml
+++ b/testdata/include_with_vars/Taskfile.yml
@@ -1,5 +1,8 @@
 version: "3"

+vars:
+  VAR_2: included2-var2
+
 includes:
   included1:
     taskfile: include/Taskfile.include.yml
@@ -9,6 +12,7 @@ includes:
     taskfile: include/Taskfile.include.yml
     vars:
       VAR_1: included2-var1
+      VAR_2: '{{.VAR_2}}'
   included3:
     taskfile: include/Taskfile.include.yml

diff --git a/testdata/include_with_vars/include/Taskfile.include.yml b/testdata/include_with_vars/include/Taskfile.include.yml
index 3bb7a68..f902f91 100644
--- a/testdata/include_with_vars/include/Taskfile.include.yml
+++ b/testdata/include_with_vars/include/Taskfile.include.yml
@@ -2,7 +2,8 @@ version: "3"

 vars:
   VAR_1: '{{.VAR_1 | default "included-default-var1"}}'
-  VAR_2: '{{.VAR_2 | default "included-default-var2"}}'
+  VAR_2:
+    sh: 'echo {{.VAR_2 | default "included-default-var2"}}'

 tasks:
   task1:

And this one works as expected:

diff --git a/testdata/include_with_vars/Taskfile.yml b/testdata/include_with_vars/Taskfile.yml
index aee8f51..aa3ecb4 100644
--- a/testdata/include_with_vars/Taskfile.yml
+++ b/testdata/include_with_vars/Taskfile.yml
@@ -9,6 +9,7 @@ includes:
     taskfile: include/Taskfile.include.yml
     vars:
       VAR_1: included2-var1
+      VAR_2: included2-var2
   included3:
     taskfile: include/Taskfile.include.yml

diff --git a/testdata/include_with_vars/include/Taskfile.include.yml b/testdata/include_with_vars/include/Taskfile.include.yml
index 3bb7a68..f902f91 100644
--- a/testdata/include_with_vars/include/Taskfile.include.yml
+++ b/testdata/include_with_vars/include/Taskfile.include.yml
@@ -2,7 +2,8 @@ version: "3"

 vars:
   VAR_1: '{{.VAR_1 | default "included-default-var1"}}'
-  VAR_2: '{{.VAR_2 | default "included-default-var2"}}'
+  VAR_2:
+    sh: 'echo {{.VAR_2 | default "included-default-var2"}}'

 tasks:
   task1:
JonZeolla commented 1 year ago

This was also unexpected (with the below patch). I expected bar:lib:greet to be Hello bar_local but want it to be Hello bar_global:

$ go run ./cmd/task --dir ./testdata/include_with_vars_multi_level default
task: [lib:greet] echo 'Hello world'
Hello world
task: [foo:lib:greet] echo 'Hello foo'
Hello foo
task: [bar:lib:greet] echo 'Hello world'
Hello world
diff --git a/testdata/include_with_vars_multi_level/bar/Taskfile.yml b/testdata/include_with_vars_multi_level/bar/Taskfile.yml
index f280f3f..b2495bb 100644
--- a/testdata/include_with_vars_multi_level/bar/Taskfile.yml
+++ b/testdata/include_with_vars_multi_level/bar/Taskfile.yml
@@ -1,7 +1,10 @@
 version: "3"

+vars:
+  RECEIVER: 'bar_global'
+
 includes:
   lib:
     taskfile: ../lib/Taskfile.yml
     vars:
-      RECEIVER: "bar"
+      RECEIVER: '{{.RECEIVER | default "bar_local"}}'
JonZeolla commented 4 months ago

Still an issue as of 3.36.0