Skip to content

config: CLI scopes should override environments#50853

Merged
tgamblin merged 3 commits intodevelopfrom
fix-cli-scope-order
Jun 10, 2025
Merged

config: CLI scopes should override environments#50853
tgamblin merged 3 commits intodevelopfrom
fix-cli-scope-order

Conversation

@tgamblin
Copy link
Copy Markdown
Member

@tgamblin tgamblin commented Jun 7, 2025

Caution

Requires spack/spack-packages#119 to be merged first, or CI will break.

Currently, active environments, from the CLI or from $SPACK_ENV, will override configuration set on the command line. This is confusing, as users would normally expect command line options to override environment options (e.g. SPACK_ENV).

See more discussion here.

This PR changes how we process configuration options on the CLI so that:

  1. Any CLI option overrides an active environment from SPACK_ENV
  2. CLI options that add config scopes (-D, -C, and -e) are now considered for scope precedence in the order they appear on the CLI.

What this means is that if you write:

  • spack -C config_dir: config_dir will override any active enviornment.
  • spack -C config_dir -e env: env will override config_dir
  • spack -e env -C config_dir -e env: config_dir will override env

And similar for -D. This makes the order much more obvious.

This is a breaking change, in that historically environments have been higher precedence than config scopes.

  • rework scope adding in main.py
  • add an action to handle config from CLI
  • add shell tests and unit tests

@tgamblin tgamblin requested a review from alalazo June 7, 2025 10:03
@spackbot-app spackbot-app bot added core PR affects Spack core functionality environments shell-support tests General test capability(ies) labels Jun 7, 2025
@tgamblin tgamblin requested a review from kwryankrattiger June 7, 2025 10:03
@tgamblin tgamblin force-pushed the fix-cli-scope-order branch from e161bf5 to 8ea01dd Compare June 8, 2025 09:35
tgamblin added 3 commits June 9, 2025 20:41
Currently, active environments, from the CLI or from `$SPACK_ENV`, will override
configuration set on the command line. This is confusing, as users would normally
expect command line options to override environment options (e.g. SPACK_ENV).

See [more discussion here](#49187 (comment)).

This PR changes how we process configuration options on the CLI so that:

1. Any CLI option overrides an active environment from `SPACK_ENV`
2. CLI options that add config scopes (`-D`, `-C`, and `-e`) are now considered
   for scope precedence in the order they appear on the CLI.

What this means is that if you write:
 * `spack -C config_dir`: `config_dir` will override any active enviornment.
 * `spack -C config_dir -e env`: `env` will override `config_dir`
 * `spack -e env -C config_dir -e env`: `config_dir` will override `env`

And similar for `-D`. This makes the order much more obvious.

This is a breaking change, in that historically environments have been higher
precedence than config scopes.

- [x] rework scope adding in `main.py`
- [x] add an action to handle config from CLI
- [x] add shell tests and unit tests

Signed-off-by: Todd Gamblin <[email protected]>
Signed-off-by: Todd Gamblin <[email protected]>
Signed-off-by: Todd Gamblin <[email protected]>
@tgamblin tgamblin force-pushed the fix-cli-scope-order branch from 2cabe6b to a1d1486 Compare June 9, 2025 18:41
@kwryankrattiger
Copy link
Copy Markdown
Contributor

@tgamblin tgamblin merged commit ec1e8cb into develop Jun 10, 2025
38 of 41 checks passed
@tgamblin tgamblin deleted the fix-cli-scope-order branch June 10, 2025 17:03
@alalazo
Copy link
Copy Markdown
Member

alalazo commented Oct 22, 2025

This makes the order much more obvious.

This was merged without updates to the docs, so not sure it's obvious 😞

@alalazo
Copy link
Copy Markdown
Member

alalazo commented Oct 22, 2025

I missed this PR back in June, but it basically recreates the issue that was solved by #48420 The gist of the original fix was to:

  1. Add priority for config scopes
  2. Ensure that the environment has its own priority, so that it could be swapped in and out without losing information on where it would be inserted (since we allow only one environment to be active at each time)

If we add an environment at CUSTOM priority, it's indistinguishable from other things added at CUSTOM priority and we need to do more bookkeeping to swap it in and out correctly.

Given this was not documented, and that debugging #51459 was really hard, I'll submit a fix that uses ENVIRONMENT priority for environments. After all, that's what currently happens behind the scenes when the environment is loaded, with the exception that:

$ spack config blame

says otherwise 😓

tgamblin pushed a commit that referenced this pull request Oct 28, 2025
…e used together (#51461)

Fixes #51459.

#50853 attempted to make configuration (envs and config scopes) specified on the CLI
merge in CLI order, but did not quite succeed.  Environments, when loaded, would still
appear out of order in the scope stack. This fixes the issues and establishes, after much
discussion, the ordering we want for configuration scopes specified on the CLI:

1. Ordering of scopes will be enforced *within* the same option on the CLI, e.g. bar is higher precedence in `-C foo -C bar`;
2. Ordering *across* options is counterintuitive, and the `-e` option (or the active environment via `SPACK_ENV`) should always be lower priority than `-C` or `-c` options; and
4. `-C` options have lower priority than `-c`.

Environments are now added using `ConfigScopePriority.ENVIRONMENT` in all cases.
Since they are the only entity using that scope priority, it's safe to activate and deactivate them.

Documentation has been updated to reflect that the CUSTOM scopes have higher priority
than ENVIRONMENT.

---------

Signed-off-by: Massimiliano Culpo <[email protected]>
kshea21 pushed a commit to kshea21/spack that referenced this pull request Nov 4, 2025
…e used together (spack#51461)

Fixes spack#51459.

spack#50853 attempted to make configuration (envs and config scopes) specified on the CLI
merge in CLI order, but did not quite succeed.  Environments, when loaded, would still
appear out of order in the scope stack. This fixes the issues and establishes, after much
discussion, the ordering we want for configuration scopes specified on the CLI:

1. Ordering of scopes will be enforced *within* the same option on the CLI, e.g. bar is higher precedence in `-C foo -C bar`;
2. Ordering *across* options is counterintuitive, and the `-e` option (or the active environment via `SPACK_ENV`) should always be lower priority than `-C` or `-c` options; and
4. `-C` options have lower priority than `-c`.

Environments are now added using `ConfigScopePriority.ENVIRONMENT` in all cases.
Since they are the only entity using that scope priority, it's safe to activate and deactivate them.

Documentation has been updated to reflect that the CUSTOM scopes have higher priority
than ENVIRONMENT.

---------

Signed-off-by: Massimiliano Culpo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core PR affects Spack core functionality environments shell-support tests General test capability(ies)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants