@@ -90,19 +90,6 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*
9090 }
9191 rangeFunc := getRangeFunc (c .Dir )
9292
93- var taskRangeFunc func (k string , v ast.Var ) error
94- if t != nil {
95- // NOTE(@andreynering): We're manually joining these paths here because
96- // this is the raw task, not the compiled one.
97- cache := & templater.Cache {Vars : result }
98- dir := templater .Replace (t .Dir , cache )
99- if err := cache .Err (); err != nil {
100- return nil , err
101- }
102- dir = filepathext .SmartJoin (c .Dir , dir )
103- taskRangeFunc = getRangeFunc (dir )
104- }
105-
10693 for k , v := range c .TaskfileEnv .All () {
10794 if err := rangeFunc (k , v ); err != nil {
10895 return nil , err
@@ -113,31 +100,60 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*
113100 return nil , err
114101 }
115102 }
103+
116104 if t != nil {
117105 for k , v := range t .IncludeVars .All () {
118106 if err := rangeFunc (k , v ); err != nil {
119107 return nil , err
120108 }
121109 }
110+
111+ if ! evaluateShVars {
112+ // Add includedTaskfile.Vars, and replace/overwrite taskfile.Vars,
113+ // _before_ calculating the t.Dir using the templater. Because
114+ // evaluateShVars is not set, the dir used when creating rangeFunc
115+ // will not be used (sh vars are evaluated on subsequent calls).
116+ for k , v := range t .IncludedTaskfileVars .All () {
117+ if err := rangeFunc (k , v ); err != nil {
118+ return nil , err
119+ }
120+ }
121+ }
122+
123+ // Calculate the t.Dir now based on values saved during AST parsing
124+ // and then get a Task RangeFunc.
125+ dir := c .Dir
126+ if len (t .Dir ) == 0 {
127+ // Use the saved include task.Dir.
128+ t .Dir = t .IncludeTaskDir
129+ } else {
130+ cache := & templater.Cache {Vars : result }
131+ taskDir := templater .Replace (t .Dir , cache )
132+ if err := cache .Err (); err != nil {
133+ return nil , err
134+ }
135+ t .Dir = filepathext .SmartJoin (t .IncludeDir , taskDir )
136+ // Update dir (esp. if taskDir is a relative path).
137+ dir = filepathext .SmartJoin (c .Dir , taskDir )
138+ }
139+ taskRangeFunc := getRangeFunc (dir )
140+
122141 for k , v := range t .IncludedTaskfileVars .All () {
123142 if err := taskRangeFunc (k , v ); err != nil {
124143 return nil , err
125144 }
126145 }
127- }
128-
129- if t == nil || call == nil {
130- return result , nil
131- }
132-
133- for k , v := range call .Vars .All () {
134- if err := rangeFunc (k , v ); err != nil {
135- return nil , err
146+ if call != nil {
147+ for k , v := range call .Vars .All () {
148+ if err := rangeFunc (k , v ); err != nil {
149+ return nil , err
150+ }
151+ }
136152 }
137- }
138- for k , v := range t . Vars . All () {
139- if err := taskRangeFunc ( k , v ); err != nil {
140- return nil , err
153+ for k , v := range t . Vars . All () {
154+ if err := taskRangeFunc ( k , v ); err != nil {
155+ return nil , err
156+ }
141157 }
142158 }
143159
0 commit comments