Skip to content

fix: dynamic var uses wrong dir#2931

Open
kjasn wants to merge 2 commits into
go-task:mainfrom
kjasn:fix/wrong-dir-for-dynamic-var
Open

fix: dynamic var uses wrong dir#2931
kjasn wants to merge 2 commits into
go-task:mainfrom
kjasn:fix/wrong-dir-for-dynamic-var

Conversation

@kjasn

@kjasn kjasn commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

The shell command value which stored using the command as a cache key may have collisions with the same command
but different dir. When the command value is related with dir, it will get a wrong command value.

Close #2928

I used AI assistant to inspect the repo structure and help with tests.

Copilot AI review requested due to automatic review settings July 22, 2026 12:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@trulede

trulede commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

This reminds me of #524 where a solution is presented sh: 'task --offline env # {{now}}'. There is a reason its this way, I think that in global vars you can get the same sh constantly evaluating (undesirable).

What I mean is, probably, the cause of the problem is not: "var uses the wrong dir".

Looking at this PR makes me wonder if the hash of a Task could be used as par of the index:

type dynamicCacheKey struct {
	hash string
	sh  string
}

for global vars the hash would be "", which is probably OK. Imports would need something too. I don't think dir is going to be robust in all situations, however it seems like a good idea regardless.

@kjasn

kjasn commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

This reminds me of #524 where a solution is presented sh: 'task --offline env # {{now}}'. There is a reason its this way, I think that in global vars you can get the same sh constantly evaluating (undesirable).

What I mean is, probably, the cause of the problem is not: "var uses the wrong dir".

Looking at this PR makes me wonder if the hash of a Task could be used as par of the index:

type dynamicCacheKey struct {
	hash string
	sh  string
}

for global vars the hash would be "", which is probably OK. Imports would need something too. I don't think dir is going to be robust in all situations, however it seems like a good idea regardless.

I found it doesn't work for some situation like this: the same task run with different dirs when its dir is templated from var

tasks:
  default:
    cmds:
      - task: build
        vars: { PROJECT: dir1 }
      - task: build
        vars: { PROJECT: dir2 }

  build:
    dir: "{{.PROJECT}}"
    vars:
      HERE:
        sh: pwd
    cmds:
      - echo "{{.HERE}}"
      - echo "{{.PROJECT}}"

Expecte dir1 and dir2, but actual both aredir1 if dynamicCacheKey holds hash.

@ikshantshukla123

Copy link
Copy Markdown
Contributor

This reminds me of #524 where a solution is presented sh: 'task --offline env # {{now}}'. There is a reason its this way, I think that in global vars you can get the same sh constantly evaluating (undesirable).
What I mean is, probably, the cause of the problem is not: "var uses the wrong dir".
Looking at this PR makes me wonder if the hash of a Task could be used as par of the index:

type dynamicCacheKey struct {
	hash string
	sh  string
}

for global vars the hash would be "", which is probably OK. Imports would need something too. I don't think dir is going to be robust in all situations, however it seems like a good idea regardless.

I found it doesn't work for some situation like this: the same task run with different dirs when its dir is templated from var

tasks:
  default:
    cmds:
      - task: build
        vars: { PROJECT: dir1 }
      - task: build
        vars: { PROJECT: dir2 }

  build:
    dir: "{{.PROJECT}}"
    vars:
      HERE:
        sh: pwd
    cmds:
      - echo "{{.HERE}}"
      - echo "{{.PROJECT}}"

Expecte dir1 and dir2, but actual both aredir1 if dynamicCacheKey holds hash.

thats true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dynamic variables use the wrong directory when the same Taskfile is included multiple times

4 participants