--- type: Module title: xdg description: XDG Base Directory resolution -- turning the environment into namespaced, precedence-ordered application directories and a config-file resolver chain, with no database and no runtime dependencies. status: stable x-okfit-kind: package resource: ../../packages/xdg x-okfit-layer: L1 tags: - architecture sources: - id: xdg-package-json resource: ../../packages/xdg/package.json - id: xdg-claude-md resource: ../../packages/xdg/CLAUDE.md generated: by: "okfit/claude-code" at: 2026-11-06T20:09:15Z x-okfit-integrity: generated: body: dc1446eb76c1dc8d --- # xdg ## Purpose and tier `@effected/xdg` is XDG Base Directory resolution. Its one job is to turn the environment into paths: read the XDG Base Directory environment, map it onto a platform, namespace it for an application, create the directories on demand, and expose that as a config-file resolver chain. No database, no cache, no format parsing. **Boundary tier** — see [the library-tier glossary entry](../glossary/library-tier.md). IO happens exclusively through `effect` core's `FileSystem` and `Path`, arriving via the `R` channel from the consumer's platform layer, so there is no `@effect/platform` peer and no platform-node devDependency even in tests.[^xdg-package-json] `peerDependencies` is `effect`, `@effected/walker` and `@effected/config-file`, plus `@effected/glob`, `@effected/jsonc`, `@effected/toml` and `@effected/yaml`, which those two bring into the closure; there are **no runtime dependencies**. The two direct workspace edges, `walker` and `config-file`, are boundary→boundary and therefore non-propagating; the other four are pure-tier peers that complete the closure. `walker` depends on nothing, keeping the graph acyclic, and `config-file` is a peer rather than a regular dependency because the bridge exposes config-file's types in xdg's own public signatures, so a single copy in a consumer's graph is load-bearing. xdg **does not depend on** [store](store.md), and must not — see [xdg does not depend on store](../decisions/xdg-does-not-depend-on-store.md). xdg also **does not build on** core's `UserDirectories` (`effect/persistence`, unstable): core folds the XDG variable and the native directory into one answer per kind, so xdg's precedence ladder cannot sit on it — see [xdg stays independent of core UserDirectories](../decisions/xdg-stays-independent-of-core-userdirectories.md). The two agree on absolute `XDG_*_HOME` values and on the macOS and Windows native directories, a subset a parity test pins. ## Module layout Module-per-concept, four files under `src/`: `Xdg.ts` (the environment), `NativeDirs.ts` (the pure platform map), `AppDirs.ts` (namespace, precedence, creation) and `XdgConfig.ts` (the config-file bridge). Import direction is a DAG: `Xdg.ts` depends on nothing, `NativeDirs.ts` and `AppDirs.ts` build on it, `XdgConfig.ts` builds on `AppDirs.ts`. There is no `internal/` directory — there is no engine here, only resolution.[^xdg-claude-md] ## Public surface ### Xdg — the environment - **The service's shape IS the resolved paths.** The environment does not change during a process, so resolution happens once, at layer construction, and the service is the resolved value rather than an effect that computes one — the whole downstream chain is infallible as a result. - `Schema.optionalKey` models an absent XDG variable as an absent key, read with a `??` fallback — no `Option` in the model. - The **system search paths** are modeled too, not just the per-user home variables: the colon-separated `XDG_CONFIG_DIRS` and `XDG_DATA_DIRS` with their spec defaults. Modeling the search-path half of the spec is what makes [the walker edge](#where-walker-fits) load-bearing rather than decorative. - **The platform is injected, never read from a global.** `CurrentPlatform` is a `Context.Reference` whose default reads `process.platform` once, so production is unchanged while a test pins macOS or Windows with a one-line `Layer.succeed`. Three sites read it — the `Xdg` layer, the `AppDirs` layer and the native config resolver — so an override is provided once, at the edge, beneath all three. - **A relative value is ignored**, as the XDG Base Directory specification requires: a relative `XDG_*_HOME`, `XDG_RUNTIME_DIR` or search-path entry reads as unset, and a search path left empty takes the spec default. `XDG_*` values are judged by the platform's absolute rule; the Windows `APPDATA`/`LOCALAPPDATA` always by the Windows rule, so the answer never depends on the host. On win32 the search paths split on `;`. A missing home directory is the only environment failure; everything else is optional by construction. Home is `HOME`, or on win32 `USERPROFILE` then `HOME` (the error then names `USERPROFILE`, meaning neither was set). Home is deliberately not validated as absolute, and its absence fails the layer eagerly even when every `XDG_*_HOME` is set. There is deliberately **no** posix `USERPROFILE` fallback for an unset or empty `HOME`; the owner declined it, so the home rule is the same in both reading layers. `layerFrom` supplies explicit paths, trusted as given, and is the test layer. **`Xdg.layerFromEnv(env)`** reads the same environment from an explicit record instead of the ambient `ConfigProvider`. It shares one resolution with `Xdg.layer` (`readPaths` in `Xdg.ts`), rule for rule, and the record goes through `ConfigProvider.fromEnvRecord`, so `""` reads as unset, as `process.env` does. It takes a record only. Each call returns a new layer, and the layer is `Layer.fresh` (as `CurrentRuntimeEnv.layerFrom` is), so one returned value re-reads its source on every build, even nested inside a provide of itself. Never answer "read an env record" by providing a `ConfigProvider` beneath the static `Xdg.layer`: the static is memoized by reference, so within one graph the first provider wins everywhere it is used, which the `XdgLayerFromEnv` suite pins as its control. A caller who holds a `ConfigProvider` rather than a record, and needs only one in the graph, composes `Layer.provide(Xdg.layer, ConfigProvider.layer(p))`. **Opt-in departures: `Xdg.layerWith(options)` and `Xdg.layerFromEnv(env, options)`.** Both take `XdgEnvOptions`, whose every field defaults to the specification, so `layerWith({})` resolves what `Xdg.layer` does and the defaults above never change. They exist for a program that must read the same directory as an existing shell rule, the case a consumer raised (effected#1119): pluginfinity's shell library writes logs under `${XDG_STATE_HOME:-${HOME:-…}/.local/state}`, and its CLI must read them back. - `homeFallback?: () => string` replaces a **missing** home: unset, or empty (the env providers read `""` as unset). A set home, even a relative one, wins and the fallback is never called; a provider that cannot be read still fails typed, because the read is `Config.option` and only `none` falls back. It is called once, at build; an empty result fails with the variable's own `XdgEnvError`, and a throw is a defect. It is a function rather than an `os.homedir()` call so xdg keeps reading no `os` module: the application passes `os.homedir` from its edge. This is not the declined `USERPROFILE` substitution — the default stays off, and the application chooses where its files live. - `relativeHomes?: "ignore" | "keep"`. The issue's "shell-compatible relative values" means one thing precisely: a shell's `${VAR:-default}` uses a non-empty value as given, relative or not, where the specification says a relative `XDG_*` value is invalid and must be ignored. The two conflict, so `"keep"` is opt-in and the default stays the spec's `"ignore"`. `"keep"` covers exactly the four `XDG_*_HOME` variables — the ones a `${VAR:-default}` rule reads — and keeps the value unresolved, so it is relative to whatever directory a later `Path.resolve` or filesystem call sees. `XDG_RUNTIME_DIR` keeps the spec rule (it must be a user-owned absolute directory, and has no fallback to lose), as do the `*_DIRS` search-path entries. An empty value is unset under both settings, as `:-` treats it. ### NativeDirs — the pure platform map - **darwin** — config, data and state under `~/Library/Application Support/`; cache under `~/Library/Caches/`. - **win32** — config and data under `%APPDATA%/`; cache and state under `%LOCALAPPDATA%`, falling back to the standard `AppData/Roaming`/`AppData/Local` locations under home when those variables are absent. - **everything else** — `Option.none()`. On Linux, XDG is the native convention, so returning `none` lets the precedence ladder skip the rung cleanly rather than duplicating the XDG answer. `NativeDirs.resolve` is pure — no IO, no env, no clock — joined through `Path.Path` so a win32 `Path` layer produces win32 separators. ### AppDirs — namespace, precedence, creation Resolution happens once, at layer construction, so reading a path cannot fail — it is a `string`. The layer's error channel is `never`; the one failure that could hide behind it, a missing home directory, surfaces on the `Xdg` layer instead, before an `AppDirs` exists. That infallible channel is also the only way the config-file save path fits config-file's `defaultPath` slot without an `orDie`. The **five-level precedence** per directory kind: 1. an explicit per-kind override; 2. the XDG environment variable, namespaced; 3. the native directory, when native mode is on and the platform has one; 4. a single dot-directory under the home directory that all four kinds collapse to; 5. `/.`. Rungs 4 and 5 are **not** the XDG spec's per-kind defaults — a caller wanting spec defaults passes them as per-kind overrides. Native mode defaults **off**, because creating a native directory commits an application to a location; the `ensure*` operations `mkdir -p` and return the path, with the runtime one `Option`-returning since a runtime directory exists only when the environment says so. **`AppDirs.segment(name)`** maps an arbitrary name (a package name, a project key) to one filesystem-safe path segment, returning `Result`. Three rules, in order: `/` becomes `__` (so `@org/pkg` is `@org__pkg`); every character outside `[A-Za-z0-9._@-]` becomes `_`; runs of three or more `_` collapse to `__`. The regex has **no `u` flag**, so it matches per UTF-16 code unit and an astral character becomes `__`. The result never holds a separator, and the three results that name no child (`""`, `.`, `..`) fail. It is pure, so a `Result` rather than an `Effect`; Effect code lifts it with `Effect.fromResult`. The mapping is stable and **not injective** (`@org/pkg` and `@org__pkg` share a segment); hash the name when distinct names must never share a directory. It is a **persisted contract**: vitest-agent's `normalizeWorkspaceKey` byte for byte, with the suite running a copy of the original as an oracle, so never "fix" its regex flags or collapse rule — stored data directories are keyed on today's output. It refuses neither Windows trailing dots nor device names, because the mapping must be the same on every platform → [AppDirs.segment does not refuse Windows trailing dots or device names](../limitations/xdg-segment-win32-aliasing.md). ### XdgConfig — the config-file bridge Statics on a concept class: a config-search-path resolver, a native-directory resolver and a save path. The primary resolver searches the whole XDG config search path — the app's own config directory, then each system config directory, namespaced, in that order — placed ahead of the native resolver so an existing `~/.config/` still beats the native directory. Both resolvers get a `never` error channel from walker rather than a hand-rolled `catchAll`. The save path is meant to be paired with config-file's `saveTarget: "default"`. Under `firstMatch` an unset `saveTarget` means `"source"`: write back into the file that was found. Because the primary resolver also searches the system config directories, that file can be `/etc/xdg//`, which a normal user cannot write. With `"default"` the write goes to the user's own config directory, which the resolver searches first, so the saved file wins the next load. Every example in the package pins it. Deliberately not present: preset ladders and format-coupled factories (that composition belongs in [app](app.md)), and a central error union (each error lives with the concept that raises it). ## Where walker fits Walker earns its edge in the one place xdg does a search: the config resolver builds the ordered candidate list from the app's config search path and hands it to `Walker.firstMatch`. That single call buys per-candidate absorption (a permission failure on a system config directory must not hide a readable `~/.config`), short-circuiting (the first hit wins), and defect propagation (`firstMatch` uses `Effect.catch`, not `catchCause`). The native resolver has exactly one candidate but goes through `firstMatch` too, for the absorption contract. Nothing in xdg ascends a directory chain — its candidates come from the environment, not the tree — so [walker](walker.md)'s `ascend` and `findRoot` go unused here. ## Errors Three `Schema.TaggedError` types. Two carry their underlying failure structurally in a `cause: Schema.Defect()` field: `XdgEnvError`, naming the missing variable, and `AppDirsError`, the directory-creation error carrying the failing kind as a literal union plus the path. The third, `AppDirsSegmentError`, is the `AppDirs.segment` failure: `input` (the name as given) and `reason` (`"empty"`, `"dot"` or `"dot-dot"`), with no cause because nothing failed underneath it. `PlatformError` is wrapped, never leaked. Nothing is `orDie`d — "the cache directory could not be created" is an expected, recoverable boundary failure. Wiring errors are construction defects: an empty namespace, or one containing a path separator, dies at layer construction, because a namespace with a separator could silently escape the app's directory. ## Observability Named spans on every public fallible boundary, uniformly — the `ensure*` set. Path reads are property accesses on a value and are unspanned; the two resolvers have a `never` channel by contract and carry no spans; the platform map is pure. No metrics, no logging, no `@effect/opentelemetry` — telemetry-agnostic. ## Testing Suites in `__test__/`, one per concept module, with suite-boundary `layer(...)` blocks. The platform matrix is tested with **no platform IO at all**: because the platform is a `Context.Reference` and the native map is pure, a suite pins darwin/win32/linux behaviour by providing the reference at the group boundary and asserting on strings. `AppDirs`' filesystem behaviour runs on a real in-memory volume ([`@effected/memfs`](memfs.md), a devDependency) with `makeDirectory` intercepted by a delegate-by-default spy: the handler records the path and returns `undefined`, so the directory is genuinely created rather than merely observed. **Never record eagerly** — `AppDirs` builds its `ensure*` effects once, at layer construction, so a stub that records in its body counts directories that were never created; a fault handler consulted when the method is called gets the property for free. `XdgConfig`'s suites run on memfs too, with an `exists` fault handler that records each probe and denies seeded paths. The environment rules run through the real `Xdg` layer under a pinned platform and a `ConfigProvider` built from a record, and a differential suite compares the agreed subset with core's `UserDirectories`. ## Hardening Not a parser — no recursion, no untrusted text, no nesting cap, no numeric option. `AppDirs.segment` is the sanctioned route for a runtime-supplied name that becomes a directory: its output never contains a separator and never names the directory itself or its parent. The namespace is a path component, validated as one: an empty namespace or one containing a separator is rejected at layer construction as a defect. Every join goes through `Path.Path`, never string interpolation. Absorption is per candidate, not per resolver. Defects propagate — xdg adds no `catchCause` anywhere. ## Build The bundler's default config carries the standard narrow `{ messageId: "ae-forgotten-export", pattern: "_base" }` suppression. Gate on a zero-warning `dist/prod/issues.json` via `pnpm build --filter @effected/xdg`. [^xdg-package-json]: `packages/xdg/package.json` — `peerDependencies` lists `effect`, `@effected/walker`, `@effected/config-file`, `@effected/glob`, `@effected/jsonc`, `@effected/toml` and `@effected/yaml`; no `dependencies` block. [^xdg-claude-md]: `packages/xdg/CLAUDE.md` — module layout and the resolve-once-at-construction design.