go-task / task

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

Propagate Parent Task Environment Variables to Subtasks #1854

Open borjapazr opened 1 month ago

borjapazr commented 1 month ago

Hello! 👋

Description

I would like to request a feature that allows environment variables defined at the parent task level to be propagated and accessible in subtasks invoked via the task: subtask command.

Currently, when a parent task defines environment variables via the env block, these variables are not available in subtasks unless redefined at the subtask level. The expected behavior is for the parent task's environment variables to be accessible and expanded within any subtasks it calls, allowing for a more modular and DRY approach to managing environment variables across tasks.

Example Use Case

Consider the following Taskfile.yml:

version: "3"

tasks:
  default:
    cmds:
      - task: print
    env:
      WORLD: hello world!

  print:
    cmds:
      - echo $HELLO $WORLD
    env:
      HELLO: hello

Expected Behavior

Actual Behavior

Motivation

This feature would improve task reusability and prevent duplication of environment variables across tasks. For example, in a real-world scenario, services might share common environment variables such as database connection details, API keys, or service endpoints, which are defined once in a parent task but need to be accessible across multiple subtasks.

Having to redefine these variables at each subtask level leads to repetition and increases the risk of inconsistencies and errors. Allowing environment variables to be passed down from parent to subtask would align Taskfile behavior with what is expected in many other task runners and scripting tools, where a parent task's environment variables are inherited by default by its children.

Suggested Implementation

When a task invokes another task using the task: subtask syntax, the environment variables defined in the parent task should be automatically included in the environment of the subtask, unless explicitly overridden by the subtask’s own env block.

If there are conflicting variable names between the parent and child tasks, the child task's env block should take precedence, maintaining current behavior for explicit overrides.

Conclusion

This feature would enhance the flexibility and usability of Taskfile by allowing environment variables to flow between parent and child tasks. This would make it easier to structure tasks in a DRY and maintainable manner, particularly in large-scale projects with complex environment requirements.

I appreciate your consideration of this feature request. Please let me know if any further details or clarifications are needed.

Thanks in advance. 🙏

Best regards!

siku4 commented 1 month ago

Something similar is already discussed here https://github.com/go-task/task/issues/593#issuecomment-2225207006