casey / casey/just

submodule recipes should respect `JUST_WORKING_DIR` environment variable and `--working-directory` command line parameter

Open
#2,862 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
35.8k
Forks
846
Avg merge
27m
Merged PRs (30d)
3

Description

## issue

submodule recipes do not make use of environment variable `JUST_WORKING_DIRECTORY` (or `--working-directory`) to set their working directory if it is present. they ignore it

## context

from `just --help`:

> -d, --working-directory
Use as working directory. --justfile must also be set [env:
JUST_WORKING_DIRECTORY=.]

and there's this [documented behavior](https://github.com/casey/just/blob/c9ebc97808888394b291763b7dbf61aa83b54842/README.md?plain=1#L3771) for submodules:

> Recipes in submodules without the `[no-cd]` attribute run with the working directory set to the directory containing the submodule source file.

i think submodule recipes should only do that if they're not explicitly told otherwise by `JUST_WORKING_DIRECTORY` or `--working-directory`

## reproduction

### by script

script available in [this gist](https://gist.github.com/sean-xyz/be45862e532035f475a22a2082248b2a)

### step by step

click to expand

1. create a directory for justfiles, not where you're currently at:
```bash
mkdir -p /tmp/some/other/location`
```

2. create `/tmp/some/other/location/justfile`:

```just
mod foo

@pwd:
echo "in justfile:"
echo " \$JUST_WORKING_DIRECTORY: $JUST_WORKING_DIRECTORY"
echo "{{ style("warning") }} \`pwd\`: $(pwd){{ NORMAL }}"
echo "{{ style("warning") }} \$PWD: $PWD{{ NORMAL }}"
```

3. create module `/tmp/some/other/location/foo.just`:

```just
@pwd:
echo "in mod foo:"
echo " \$JUST_WORKING_DIRECTORY: $JUST_WORKING_DIRECTORY"
echo "{{ style("error") }} \`pwd\`: $(pwd){{ NORMAL }}"
echo "{{ style("error") }} \$PWD: $PWD{{ NORMAL }}"
```

4. invoke `justfile` with:

```bash
JUST_WORKING_DIRECTORY="." JUST_JUSTFILE="/tmp/some/other/location/justfile" just pwd
```

note the *warning*-style (yellow) text, showing the _`justfile` respect `JUST_WORKING_DIRECTORY`_

5. invoke submodule `foo.just` with:

```bash
JUST_WORKING_DIRECTORY="." JUST_JUSTFILE="/tmp/some/other/location/justfile" just foo pwd
```

note the *error*-style (red) text, showing the _`submodule` ignore `JUST_WORKING_DIRECTORY`_

## workarounds

* ~add `[no-cd]` to each submodule recipe~
* _this uses invocation directory, rather than `JUST_WORKING_DIRECTORY`_
* use [invocation_directory()](https://github.com/casey/just/tree/c9ebc97808888394b291763b7dbf61aa83b54842?tab=readme-ov-file#invocation-directory) in each submodule recipe
* _not strictly a workaround, as invocation directory != working directory, but they may be the same for many use cases_
* read and use `env('JUST_WORKING_DIRECTORY')` from the recipes themselves, or the submodule file
* _not attempted, but i think it would work_

## related

* [Feature request: setting `[no-cd]` for entire submodule file](https://github.com/casey/just/issues/2754)
* [Shell not found in module](https://github.com/casey/just/issues/2575) and [Improve error message if working-directory does not exist](https://github.com/casey/just/issues/2295) - not directly related, but an issue i hit a few times while trying to find workarounds
`error: Recipe ... could not be run because just could not find the shell: No such file or directory (os error 2)`

Contributor guide

Open the contributing guide

Research direction

Start by running the documented reproduction with JUST_WORKING_DIRECTORY and --working-directory, comparing the top-level recipe with the submodule recipe. Trace the submodule recipe working-directory handling and verify that an explicit working directory takes precedence over the submodule source directory; done means both invocation forms behave consistently while the documented default remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.