go-task / task

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

Env access for sh vars #1742

Open titpetric opened 1 month ago

titpetric commented 1 month ago

I declared global env in a taskfile like so:

---
version: "3"

env:
  DB_DRIVER: mysql
  DB_DSN: "etl:etl@tcp(localhost:3306)/etl"

tasks:
  default:
    desc: "Fill repos with details"
    vars:
      repos:
        sh: env | grep DB_ || true
    cmds:
      - env | grep DB || true
      - for: { var: repos, as: repo, split: "\n" }
        silent: true
        cmd: echo "X{{.repo}}"

The expected output of this would be:

DB_DRIVER=mysql
DB_DSN=etl:etl@tcp(localhost:3306)/etl
XDB_DRIVER=mysql
XDB_DSN=etl:etl@tcp(localhost:3306)/etl

Actual output of this is:

DB_DRIVER=mysql
DB_DSN=etl:etl@tcp(localhost:3306)/etl
X

Seems the vars sh are not picking up the taskfile env which would be my expectation...

nierob commented 2 weeks ago

I have stumbled over that too:

~/d/tmp►batcat Taskfile.yml                                                                                                                            4.526s 14:52
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: Taskfile.yml
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ version: "3"
   2   │
   3   │ env:
   4   │   GLOBAL_NAME: "foo"
   5   │
   6   │ tasks:
   7   │   hello:
   8   │     cmds:
   9   │       - echo "$GLOBAL_NAME"
  10   │       - echo "$INDIRECT_NAME"
  11   │       - echo "$ENV_NAME"
  12   │     env:
  13   │       INDIRECT_NAME:
  14   │         sh: echo "$GLOBAL_NAME"
  15   │       ENV_NAME:
  16   │         sh: echo "bar"
  17   │
  18   │
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
~/d/tmp►task hello                                                                                                                                            14:52
task: [hello] echo "$GLOBAL_NAME"
foo
task: [hello] echo "$INDIRECT_NAME"

task: [hello] echo "$ENV_NAME"
bar

It looks like sh initialization does not get globally set env