env() does not see exported variables in the same Justfile
- Dominant language
- Rust
- Stars
- 35.8k
- Forks
- 846
- Avg merge
- 27m
- Merged PRs (30d)
- 3
Description
From the docs:
- `env(key, default)` — Retrieves the environment variable with name key, aborting if it is not present. ([link][1])
- Assignments prefixed with the export keyword will be exported to recipes as environment variables ([link][2])
Using the above logic, it seems the following will work.
```bash
export ENV_VAR := "Hello World from ENV_VAR!"
export ENV_VAR_SHELL := `uname -m`
VAR := "Hello World from VAR!"
VAR_SHELL := `uname -o`
# Test the environment variables.
@test-env-var $PARAM_VAR="Hello World from PARAM_VAR!":
echo "env(ENV_VAR): '{{ env("ENV_VAR", "not set") }}'"
echo "\$ENV_VAR: '${ENV_VAR}'"
echo "{ {ENV_VAR} }: '{{ ENV_VAR }}'"
echo ""
echo "env(PARAM_VAR): '{{ env("PARAM_VAR", "not set") }}'"
echo "\$PARAM_VAR: '${PARAM_VAR}'"
echo "{ {PARAM_VAR} }: '{{ PARAM_VAR }}'"
echo ""
echo "env(ENV_VAR_SHELL): '{{ env("ENV_VAR_SHELL", "not set") }}'"
echo "\$ENV_VAR_SHELL: '${ENV_VAR_SHELL}'"
echo "{ {ENV_VAR_SHELL} }: '{{ ENV_VAR_SHELL }}'"
```
The result is that `env("ENV_VAR")` does not see the variable exported in the Justfile.
```text
env(ENV_VAR): 'not set'
$ENV_VAR: 'Hello World from ENV_VAR!'
{ {ENV_VAR} }: 'Hello World from ENV_VAR!'
env(PARAM_VAR): 'not set'
$PARAM_VAR: 'Hello World from PARAM_VAR!'
{ {PARAM_VAR} }: 'Hello World from PARAM_VAR!'
env(ENV_VAR_SHELL): 'not set'
$ENV_VAR_SHELL: 'x86_64'
{ {ENV_VAR_SHELL} }: 'x86_64'
```
The docs DO state [Exported variables and parameters are not exported to backticks in the same scope.][2] In the example above, setting the environment variable is in the global (script?) scope while `env()` is in the recipe scope. Is the same Justfile considered the same scope?
[1]: https://just.systems/man/en/functions.html#environment-variables
[2]: https://just.systems/man/en/getting-and-setting-environment-variables.html?highlight=getting-and-setting-environment-variables
Contributor guide
Research direction
Start by reproducing the Justfile example from the issue and read the linked documentation on env() and exported variables. Determine whether env() should see exported assignments in the same Justfile; done means either the behavior matches the documented scope rules or the relevant documentation and behavior are aligned.
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
- 35/100