Skip to content

docs(config): state the layer-vs-specificity precedence rule - #3789

Closed
worktrunk-bot wants to merge 2 commits into
mainfrom
docs/issue-3788-config-precedence
Closed

worktrunk-bot wants to merge 2 commits into
mainfrom
docs/issue-3788-config-precedence

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

#3788 reports that WORKTRUNK_WORKTREE_PATH does not override a [projects."<id>"].worktree-path, contradicting the config docs' claim that "All user config options can be overridden with environment variables using the WORKTRUNK_ prefix."

Verified against main, and it is not an env-layer bug — the env var reaches worktree-path correctly. All three invocation-level layers set the global key, and any matching [projects."…"] entry beats the global key regardless of which layer supplied it, because layer merging (UserConfig::load_with_warnings) and specificity resolution (worktree_path_for_project) are separate steps:

Invocation Worktree created at
WORKTRUNK_WORKTREE_PATH=$tmp/from-environment $tmp/from-project
--config-set 'worktree-path = "…/from-config-set"' $tmp/from-project
--config-set 'projects."github.com/acme/demo".worktree-path = "…"' the --config-set value

So --config-set's "higher priority than both config files and WORKTRUNK_ env vars" is also imprecise for a key that has a project counterpart — it is the highest layer, not the most specific key.

Solution

Documents the actual rule rather than changing it. A new "Precedence" section under Environment variables names the two axes — layer (system config → user config → env vars → --config-set) and specificity (global key → [projects."…"] entry, applied to the merged result) — and gives the --config-set 'projects."<id>".…' form as the way to override a project entry for one invocation. The --config-set section cross-references it.

Whether an invocation-level layer should outrank a project entry is a separate design question, left open in the issue thread: making WORKTRUNK_WORKTREE_PATH win would mean a shell-rc export silently clobbering every per-project layout, so it is a maintainer call rather than a docs fix. This change is accurate for the current behavior either way and is a two-paragraph amend if the precedence later changes.

Primary source is after_long_help in src/cli/mod.rs; docs/content/config.md, both skill reference mirrors, and the wt config --help snapshot are regenerated.

Testing

test_switch_create_project_worktree_path_outranks_invocation_layers in tests/integration_tests/switch.rs pins all three rows of the table above end-to-end through wt switch --create --format=json. It caught a wrong project identifier in its own first draft, so the assertions do discriminate. Each invocation layer additionally runs once with no [projects."…"] entry present, so a layer that stopped reaching worktree-path fails a control rather than passing the specificity assertion for the wrong reason.

cargo test --test integration integration_tests::switch:: (209 passed), plus test_docs_are_in_sync, test_help, cargo clippy --all-targets, and cargo fmt.

The one unrelated line in the help snapshot (WORKTRUNK_TEST_PROBE_TIMEOUT_MS in the non-asserted info: block) is insta refreshing metadata that predates that env var; the committed snapshots are stale on it across the board.


Refs #3788 — automated triage

`WORKTRUNK_WORKTREE_PATH` and `--config-set worktree-path` set the global
key; a `[projects."<id>"]` entry beats the global key whichever layer set
it. The docs promised env vars override "all user config options" without
naming the specificity axis, so a project entry silently winning read as a
bug.

Documents the rule under a new Precedence heading, cross-references it from
the `--config-set` section, and pins all three behaviors with an
integration test.

Refs #3788
@worktrunk-bot worktrunk-bot added the automated-fix Automated CI fix label Aug 9, 2026

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the documented rule against the code: layer order matches UserConfig::load_with_warnings (system → user → env → --config-set), specificity matches worktree_path_for_project / project_field, and the exact-key --config-set advice holds even against a [projects."host/*"] pattern entry because project_match::matching_keys orders least- to most-specific. wt config show does print Identifier: …, and #precedence is a unique heading so both @/config.md#precedence links resolve.

One gap in the test, inline: it pins the specificity half of the rule but not the layer half.

Comment thread tests/integration_tests/switch.rs Resolved
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Superseded by #3790: @max-sixty called for the behavior change instead, so this page now documents a rule that no longer holds. #3790 carries the corrected precedence section (plus the test, with its expectations inverted). Closing — happy to reopen if the docs-only framing is wanted back.

max-sixty added a commit that referenced this pull request Aug 13, 2026
)

Fixes #3788.

Layer and specificity were separate steps. `load_with_warnings`
flattened system config → user config → `WORKTRUNK_*` env vars →
`--config-set` into one document, and the accessors then resolved
specificity on that document, so a `[projects."<id>"]` entry answered
for the global key of the same name whichever layer set it.
`WORKTRUNK_WORKTREE_PATH` could therefore not override a project's
`worktree-path`, and a global `--config-set` hit the same wall.

Per @max-sixty in the issue thread — "env vars should indeed take
precedence over the user project config, we should fix this throughout"
— the two invocation layers now cross the axes: they're typed for one
run, so they outrank a project entry as well as the global key. Load
applies them at both scopes (`apply_invocation_layer_over_projects`, the
last step before `finalize`): whatever the layer set is dropped from
every project entry, leaving the global key it also set to answer for
it.

Two kinds of key are held back:

- **Keys the layer restates under `projects."<name>"`** — `--config-set
'projects."github.com/owner/repo".worktree-path = …'` is both the
highest layer *and* the most specific key, so it still wins over the
same layer's global key.
- **Composing keys** — hooks, aliases, and `step.copy-ignored.exclude` —
whose project-scoped values append to the global ones rather than
replacing them. Both already apply, so an env-set hook was never
outranked, and dropping the project's copy would silently stop it
running. Hook names come from `HooksConfig`'s schema, so a new hook
can't be forgotten.

Two sections have to go as a unit rather than leaf by leaf.
`[commit.generation]`'s mutually exclusive pairs: `template` and
`template-file` clear one another in `merge_with` *and* are rejected
together by `validate`, so overriding either has to displace both at
project scope — otherwise the project's partner would still win the
merge. `exclusive_sibling` names those pairs. And
`[list.custom-columns]`, which `ListConfig::merge_with` extends per
whole column, so a partial removal leaves the project's column replacing
the global one anyway — and `ListColumnConfig::template` is required, so
it can also strand a column that no longer deserializes.
`is_atomic_section` names that table.

Both are enumerations, so the pass degrades as a unit behind them: the
removals land on a candidate, kept only if it still deserializes and
validates. That is the guarantee the env and `--config-set` layers
already have, and without it the next required field would answer a
stranded leaf with `UserConfig::default()` — costing the user their
whole config for that invocation rather than one project entry's
precedence.

The precedence table now reads:

| Source of `worktree-path` | Loses to |
|---|---|
| `--config-set 'worktree-path = …'` | — |
| `WORKTRUNK_WORKTREE_PATH` | `--config-set` |
| `[projects."github.com/owner/repo"]` in a config file | either
invocation layer |
| global `worktree-path` in a config file | all of the above |

## Docs

The help text had no precedence section at all — the gap that made this
read as a bug — so this adds one under **Environment variables**, plus a
pointer from **User project-specific settings**. That supersedes #3789,
which documented the old behavior; I'll close it in favour of this.

## Testing

Nine unit tests in `src/config/user/tests.rs` cover the table-level rule
(both layers, pattern entries, restated project-scoped overrides,
untouched sibling keys, composing keys, the exclusive pair, the atomic
custom column, a rolled-back layer, and the no-override no-op), and
`test_switch_create_invocation_layers_outrank_project_worktree_path`
proves it end-to-end — a real process is the only thing that reads
`WORKTRUNK_WORKTREE_PATH` off the environment. That test keeps a control
showing the project entry still beats the config file's own global key,
so it can't pass by project entries having stopped applying.

The reproduction from the issue now lands where it says it should:

```console
$ WORKTRUNK_CONFIG_PATH="$tmp/wt.toml" WORKTRUNK_WORKTREE_PATH="$tmp/from-environment" \
    wt switch --create feature --no-cd --no-hooks --yes --format=json
{"action":"created","branch":"feature","path":"/tmp/tmp.jQiTAuNPFd/from-environment",…}
```

<details><summary>Local suite</summary>

`cargo test --lib --bins` and `cargo test --test integration` are green
apart from `test_copy_ignored_preserves_file_executable_permissions`,
which fails in this sandbox because its umask is `0002` (file created
`0664`, test expects `0644`) — unrelated to this change and not
reproducible on a `0022` runner. `cargo fmt --check` and `cargo clippy
--all-targets --all-features` are clean.

</details>

---------

Co-authored-by: worktrunk-bot <[email protected]>
Co-authored-by: Maximilian Roos <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant