Task: dirname of current Taskfile

Created on 16 Jun 2019  路  4Comments  路  Source: go-task/task

I often want to use the directory of where the Taskfile is located as an input to other commands (not the current working directory), but it seems like sprig really locks down access to the filesystem.

Is this possible to do with Task as a first-class idea?

For example, something like:

tasks:
  show-current-dir:
    desc: "show where Task is executing from"
    cmds:
      - echo {{ .TASKFILE_LOCATION }}

Note that this isn't the same as just using $PWD or -d because the working directory might be different; Task should probably provide this as a first-class feature if it doesn't already.

feature

Most helpful comment

Example use-case:

root/Taskfile.yaml:

version: "2.X"
includes:
  sub: sub/Tasks.yaml
tasks:
  default:
    cmds:
    - sub:test

root/sub/Tasks.yaml:

version: "2.X"
tasks:
  test:
    dir: "{{.TASKFILE_DIR}}"
    cmds:
    - ./bin/executable

root/sub/bin/executable

#!/bin/sh
echo "Hello World"

All 4 comments

Hi @fj,

You should actually be fine using pwd if you haven't specified another dir: or your haven't cded in the command.

Otherwise, yeah, I think it makes sense to a have variable with the Taskfile location.

Actually, could be handy for when Taskfiles are imported as well.

Example use-case:

root/Taskfile.yaml:

version: "2.X"
includes:
  sub: sub/Tasks.yaml
tasks:
  default:
    cmds:
    - sub:test

root/sub/Tasks.yaml:

version: "2.X"
tasks:
  test:
    dir: "{{.TASKFILE_DIR}}"
    cmds:
    - ./bin/executable

root/sub/bin/executable

#!/bin/sh
echo "Hello World"

Just stumbled upon this exact issue. I guess a workaround for now is manually setting the dir to the respective folder. The negative side-effect: The Taskfile in the subfolder can no longer be used independently.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

stelcheck picture stelcheck  路  10Comments

tommyalatalo picture tommyalatalo  路  7Comments

sheerun picture sheerun  路  5Comments

testautomation picture testautomation  路  4Comments

smyrman picture smyrman  路  5Comments