Run tasks relative to `tasks.py`

Open
#972 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
cli, tooling

Research direction

Reproduce the invoke serve-docs and invoke --list commands from the project root and from docs or src, using the tasks.py example and its ctx.run() call. Read the task runner's handling of the current working directory and ctx.cd(). Done means tasks can consistently run relative to tasks.py, or the task can reliably determine that location.

Written by the indexing model from the issue text.

Description

Is there a way to have a consistent initial working directory when running tasks?
In my tasks.py file, I have a task to serve sphinx documentation with sphinx-autobuild:

from invoke import task, Context


@task
def serve_docs(ctx: Context):
    ctx.run("sphinx-autobuild ./docs/source/ ./docs/build/html --watch ./src/")

If I run this task from the project root (where tasks.py is) using invoke serve-docs, then it works fine. However, if I change directory into docs or src or any other directory in my project and run the same command invoke serve-docs, it fails since this command only works from the project root.

I expected that tasks always run with the project root as the initial working directory, which can be changed in the tasks with ctx.cd(), but it seems the initial working directory is the current working directory in the command line. I find that more confusing than helpful, especially since invoke --list still works and displays all the available tasks even if I am not in the project root near the tasks.py file. Since invoke --list works from any subdirectory of the project and I can also run tasks from any subdirectory, I would expect that the tasks will be ran with the same working directory so I get consistent results regardless of where I run the task from.

Is there a configuration option to make tasks run relative to tasks.py regardless of where I execute invoke from. If not, is there a way to make the task itself aware of the location tasks.py so I can execute commands relative to it?

Dominant language
Python
Stars
4.8k
Forks
412
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pyinvoke/invoke

All issues in pyinvoke/invoke

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.