Skip to content

refactor(config): read system config from one fixed path per platform - #4169

Merged
max-sixty merged 6 commits into
mainfrom
nightly/clean-xdg-config-dirs-absolute
Oct 11, 2026
Merged

max-sixty merged 6 commits into
mainfrom
nightly/clean-xdg-config-dirs-absolute

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Collaborator

System config is now read from one fixed path per platform, and XDG_CONFIG_DIRS is no longer consulted. The location is WORKTRUNK_SYSTEM_CONFIG_PATH when set, otherwise:

  • macOS: /Library/Application Support/worktrunk/config.toml
  • Windows: %PROGRAMDATA%\worktrunk\config.toml
  • other Unix: /etc/xdg/worktrunk/config.toml

This PR started as a filter for relative XDG_CONFIG_DIRS entries (#4106, item 4). Removing the search path removes that case without a filter, and git's --system file is a fixed path for the same reason. system_config_path() now returns the location whether or not the file exists, so default_system_config_path(), which repeated the lookup order for the wt config show hint, is deleted, along with system_config_dirs(), platform_default_dirs(), the three XDG_CONFIG_DIRS tests and the env-var docs row.

Behavior changes: directories listed in XDG_CONFIG_DIRS are no longer searched (common values such as NixOS's, Ubuntu's and Flatpak's already include /etc/xdg), and macOS no longer falls back to /etc/xdg after /Library/Application Support.

This was written by Claude Code on behalf of max-sixty

`system_config_dirs()` split `XDG_CONFIG_DIRS` on `:` and dropped only the
empty entries, so a relative one survived. `system_config_path()` then joined
`worktrunk/config.toml` onto it and resolved the result against the process
cwd — inside whatever repository `wt` was run in.

That file loads as system config, which `load_with_warnings` merges into the
same `UserConfig` table the user's own file merges into. Its `[aliases]` and
`post-*` hooks are therefore user hooks, and the approval gate deliberately
does not cover those, so the commands would run unprompted.

The XDG spec already says to ignore a relative entry, and `etcetera` applies
that rule to the `XDG_*_HOME` variables it resolves for wt. It exposes no
accessor for `XDG_CONFIG_DIRS`, so filter on `is_absolute()` here. An
all-relative value now falls back to the platform defaults, as an empty one
already did.

`resolve_input_path`'s docstring cites the spec's absoluteness requirement as
its reason for leaving `XDG_CONFIG_DIRS` alone; this makes that true.

@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.

Self-review; not approving, since GitHub rejects a self-approval. Two findings, both pushed as a follow-up commit on this branch.

The regression test held over output that said nothing about system config. render_user_config returns right after the "Not found" hint when the user config file is absent — before it reaches render_system_config_hint — and set_xdg_config_path points WORKTRUNK_CONFIG_PATH at a file the test never creates. So wt config show printed USER CONFIG / PROJECT CONFIG / SHELL INTEGRATION / OTHER and no system-config line at all, which satisfies !contains("SYSTEM CONFIG") and !contains("/planted/") without ever observing the fallback the change introduces. The test still discriminated on the bug, but any future break in that render path would turn it green rather than red. The fix plants a user config the way test_config_show_absent_system_config_is_a_user_config_hint does, then asserts Optional system config not found — the hint naming the platform default the all-relative value fell back to. Checked both directions: it fails with the old !d.is_empty() filter restored and passes with the change.

Two comments still state the contract the change replaces. system_config_dirs's docstring ("When set, it defines the search path exclusively (per XDG spec) — no fallback to platform defaults") and the matching comment in system_config_path are now false for exactly the case this PR introduces: an all-relative value drops every entry, leaves dirs empty, and falls through to platform_default_dirs(). Reworded to "names at least one absolute directory", with the fallback spelled out — the PR description states it but the code didn't.

Nothing else came out of the pass. The filter swap keeps the empty-entry drop it replaced (PathBuf::from("").is_absolute() is false), both callers of system_config_dirs only improve, and the all-relative fallback to /etc/xdg is what the spec asks for on an effectively-empty value. The hand-rolled rule is the documented escape hatch in CLAUDE.md → Use Existing Dependencies rather than a re-derivation: etcetera has no XDG_CONFIG_DIRS accessor, and its env_var_or_none applies the same is_absolute() filter to the variables it does own.

…back

The assertions were all negative, and `config show` never reached the
system-config hint: `render_user_config` returns after the "Not found"
hint when the user config file is absent, which `set_xdg_config_path`
guarantees. Plant a user config so the hint renders, then assert it — the
hint names the platform default an all-relative value falls back to.

Also correct the docstring and comment that still said XDG_CONFIG_DIRS
being set means no fallback to platform defaults.
`resolve_input_path` is already imported in this module, so the bare
label resolves. The explicit target tripped
`rustdoc::redundant_explicit_links`, which `-D warnings` makes fatal, and
failed the pre-merge `doc` step in fast-checks.
@max-sixty

Copy link
Copy Markdown
Owner

this is an incredibly minor issue and we're spending a lot of text on it here (and so many PRs)

can we just delegate it all to etcetera? even if not perfect; we shouldn't be spending our time on building lots of corner cases to slightly incorrectly created env bars. see if we can wash all this config code away and just delegate it

Per review: the fix is one predicate, so the explanation should be a sentence. The docstring loses the spec quote and the approval-gate argument (both live in the PR body and #4106), the call-site comment is now shorter than the one it replaced, and the test's commentary is trimmed to what a reader needs.
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Can't delegate this one to etcetera — grep -rn 'CONFIG_DIRS\|config_dirs\|DATA_DIRS' over the 0.11 source returns nothing. BaseStrategy is home_dir/config_dir/data_dir/cache_dir/state_dir/runtime_dir only; the *_DIRS search paths aren't modelled. The one place etcetera does own is config_path()'s platform default, which already goes through choose_base_strategy().

The other delegate would be the xdg crate's BaseDirectories::get_config_dirs(), and it's a bad trade: it's Unix-only, so platform_default_dirs() (macOS /Library/Application Support, Windows %PROGRAMDATA%) stays regardless — a new dependency to remove about ten lines.

So what's left is the one-line predicate this PR already is, and it's the same rule etcetera applies internally to the variables it does own:

// etcetera-0.11.0/src/base_strategy/xdg.rs
fn env_var_or_none(env_var: &str) -> Option<PathBuf> {
    std::env::var_os(env_var).and_then(|path| {
        let path = PathBuf::from(path);
        // Return None if the path obtained from the environment variable isn't absolute.
        if path.is_absolute() { Some(path) } else { None }
    })
}

Your other point stands on its own though, so I've cut the text: the docstring goes from 14 lines to 3 (the spec quote and the approval-gate argument were already in the PR body and #4106), the call-site comment is now shorter than the one it replaces, the test's commentary is trimmed, and the PR description is a third of its old length. That's 30 lines off the branch. The whole PR is now +57 / -7, 46 of which is the regression test; the source change is .filter(|d| !d.is_empty()) → .filter(|d| d.is_absolute()) plus the docstring. Pushed as 37bb17c; cargo clippy --all-targets clean and cargo test --test integration test_system_config 7 passed.

On the PR count: #4106 is a survey of nine items and I've been opening one PR per item. Happy to stop there and leave the rest as issue text unless you want a specific one picked up.

@max-sixty

Copy link
Copy Markdown
Owner

BaseStrategy is home_dir/config_dir

what's this config_dir?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

etcetera's user config dir — $XDG_CONFIG_HOME when absolute, else ~/.config (%APPDATA% on Windows). We already delegate to it: default_config_path() is just strategy.config_dir().join("worktrunk").join("config.toml").

It's a different variable from the one this PR touches, though — config_dir() reads XDG_CONFIG_HOME (singular, one dir, user layer); the bug is in XDG_CONFIG_DIRS (plural, a search path, system layer), which etcetera doesn't model at all. BaseStrategy is six accessors each returning one directory.

What carries over is the rule. etcetera's Xdg::env_var_or_none is if path.is_absolute() { Some(path) } else { None }, applied to every XDG_*_HOME it reads — and the diff is that same predicate applied to XDG_CONFIG_DIRS.

max-sixty and others added 2 commits October 10, 2026 16:37
System config no longer consults XDG_CONFIG_DIRS. The location is
WORKTRUNK_SYSTEM_CONFIG_PATH when set, otherwise one path per platform:
/Library/Application Support/worktrunk/config.toml on macOS,
%PROGRAMDATA%\worktrunk\config.toml on Windows, and
/etc/xdg/worktrunk/config.toml elsewhere. That removes the search path, and
with it the relative-entry case this branch first patched, rather than
filtering it.

Behavior changes: XDG_CONFIG_DIRS directories are no longer searched, and
macOS no longer falls back to /etc/xdg. system_config_path() now returns the
location whether or not the file exists, so default_system_config_path() goes;
callers that need an existing file check for it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…fig-dirs-absolute

# Conflicts:
#	src/commands/config/show.rs
@max-sixty max-sixty changed the title fix(config): ignore a relative XDG_CONFIG_DIRS entry refactor(config): read system config from one fixed path per platform Oct 10, 2026
@max-sixty
max-sixty merged commit 501d017 into main Oct 11, 2026
37 checks passed
@max-sixty
max-sixty deleted the nightly/clean-xdg-config-dirs-absolute branch October 11, 2026 00:17
max-sixty added a commit that referenced this pull request Oct 12, 2026
Adds **Other Tools Own Their Conventions** under Code Quality in
`AGENTS.md`, and folds "Use Existing Dependencies"' closing sentence
("Let dependencies own their own rules") into it:

> Where another tool owns a convention, such as its config location,
file format, URL syntax, or error reporting, ask that tool or a library
that implements its rules instead of encoding them in Worktrunk: `nu`
reports its own directories, and `gh auth token --hostname` reports
whether a host is authenticated. Worktrunk's role is the standard uses
of those conventions. Where neither the tool nor a library answers,
support the standard case and remove rather than refine: an input that
only misbehaves outside standard use is a reason to stop reading it, and
a tool that isn't installed has nothing to integrate with, so needs no
fallback table.

#4169 is the case it comes from: filtering relative `XDG_CONFIG_DIRS`
entries became, on review, no longer reading `XDG_CONFIG_DIRS` at all.

Code the rule implies removing, to be done in a follow-up rather than
found piecemeal: `nushell_data_dir_fallback` in `src/shell/paths.rs`,
which re-encodes Nushell's data-dir rule for when `nu` can't be queried,
and the PowerShell `~/.config/powershell` fallback for when `pwsh` can't
answer.

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nightly-cleanup Issues found by nightly code quality sweep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants