Running librofm-downloader from any directory other than the one containing config.yaml and secrets.yaml crashes with an unhelpful Python FileNotFoundError traceback. The tool has no concept of a user-level config home — both files are resolved purely as CWD-relative paths with no search strategy. A user who installs via pip and runs the command from their home directory, a cron job, or /tmp gets a raw stack trace instead of guidance.
Solution
Implement XDG Base Directory-compliant config resolution: search ~/.config/librofm-downloader/ first, then fall back to the current working directory. Treat missing config.yaml as non-fatal (built-in defaults exist for every field) but always inform the user. Treat missing secrets.yaml as fatal with a clear error listing all searched paths. Apply the same XDG default to download_history.json. Auto-create the XDG config directory on demand if it doesn't exist.
User Stories
As a first-time user who just installed via pip, I want to run librofm-downloader from my home directory and get a clear message telling me where to put secrets.yaml, so that I don't have to read source code or guess file locations.
As a first-time user, I want the XDG config directory (~/.config/librofm-downloader/) to be created automatically on first run, so that I don't need to manually mkdir before dropping in my files.
As a user without a config.yaml, I want the tool to use built-in defaults and print a non-fatal notice like "config.yaml not found. Using built-in defaults.", so that I know what's happening but can still run the tool.
As a user with only a secrets.yaml, I want the tool to work using all hardcoded defaults for format, output directory, extras, and covers, so that I don't need to create a config file just to get started.
As a power user keeping files in a project directory (e.g., Nix flake, docker-compose), I want --config ./config.yaml --secrets ./secrets.yaml to bypass all search logic and use my explicit paths, so that existing workflows aren't disrupted.
As a user running from cron or a script, I want librofm-downloader to find its config regardless of working directory, so that my automated downloads don't break when the scheduler's CWD changes.
As a user who already has config.yaml and secrets.yaml in CWD, I want the tool to continue finding them there (CWD fallback), so that this change is backward-compatible and requires zero migration.
As a user with secrets.yaml in XDG but no config.yaml anywhere, I want the tool to load secrets successfully, use defaults for everything else, and proceed to download, so that minimal setup is sufficient.
As a user with neither file anywhere, I want a fatal error that lists every path searched (XDG then CWD) for secrets.yaml, so that I know exactly where to place the file.
As a user running with --verbose, I want to see which paths were searched and which was selected for each file, so that I can debug config resolution issues.
As a user, I want download_history.json to default to the XDG directory alongside config/secrets, so that download history is shared regardless of which directory I run the tool from.
As a user who has been using CWD-based history, I want --history ./download_history.json to continue working as an explicit override, so that my existing history file is still found.
Implementation Decisions
Search order: XDG ~/.config/librofm-downloader/ is checked first; current working directory is checked second. First match wins. Explicit CLI flags (--config, --secrets, --history) skip search entirely and use the provided path.
Missing config.yaml: Non-fatal. Always prints a warning message listing searched paths. All fields fall back to their hardcoded defaults (format=m4b_mp3_fallback, output_dir=./audiobooks, download_extras=True, download_covers=True). The load_config() function signature gains optional-path semantics — callers pass None to trigger search.
Missing secrets.yaml: Fatal exit code 1. Error message lists all searched paths in order. No Python traceback leaks to the user.
History file: Default location changes from CWD-relative download_history.json to XDG ~/.config/librofm-downloader/download_history.json. Same XDG-then-CWD search applies when no --history flag is given.
XDG directory auto-creation: The ~/.config/librofm-downloader/ directory is created with Path.mkdir(parents=True, exist_ok=True) on first access. No config files are written — only the directory itself.
Resolution function: A new internal module function handles path resolution. It takes a filename, returns either a resolved Path (file exists) or None (not found anywhere). This keeps search logic out of both load_config() and cli.py.
CLI default values: The run() function's default parameters for config_path, secrets_path, and history_path change from CWD strings to None. None signals "use search." Non-None means "use this exact path."
Error handling boundary: FileNotFoundError inside load_config() is caught and converted to domain errors (ConfigError subclasses) before reaching cli.py. The CLI layer never sees raw OS exceptions from config loading.
Testing Decisions
Tests should verify external behavior: given a set of filesystem states (XDG exists/missing, CWD exists/missing), what does the resolver return? What messages are printed?
Config resolver tests: Test _resolve_config_file() with combinations of (XDG present, CWD present, both present, neither present). Verify return value and search order precedence.
Integration tests for load_config(): Pass None for paths (triggering search) with various filesystem setups. Verify correct Config dataclass output or correct exception type.
CLI tests: Invoke run() with no arguments (all None) against different filesystem layouts. Capture console output to verify the "using defaults" message appears exactly when config is absent, and the fatal error appears exactly when secrets are absent.
Auto-creation test: Verify that a nonexistent XDG parent directory gets created after resolution runs, and that subsequent calls don't fail or warn.
Backward compatibility test: Explicit --config ./config.yaml --secrets ./secrets.yaml still works when CWD has the files but XDG does not — search must be skipped entirely for explicit paths.
Prior art: existing tests/test_config.py (25 tests) and tests/test_cli.py (28+ tests) show patterns for mocking filesystem state and capturing Rich console output.
Out of Scope
XDG XDG_CONFIG_HOME environment variable respect (hardcode ~/.config/ for now).
Migration helper to move existing CWD files to XDG.
Any changes to the config YAML schema, field names, or merge semantics.
Changes to how --select mode or any other feature resolves paths.
Further Notes
This is a pure UX/resilience improvement — zero changes to download pipeline, auth flow, API client, or path sanitization. The blast radius is confined to config.py (resolution + error translation) and cli.py (default values + messaging). All existing tests must continue to pass unchanged since they pass explicit fixture paths.
## Problem Statement
Running `librofm-downloader` from any directory other than the one containing `config.yaml` and `secrets.yaml` crashes with an unhelpful Python `FileNotFoundError` traceback. The tool has no concept of a user-level config home — both files are resolved purely as CWD-relative paths with no search strategy. A user who installs via pip and runs the command from their home directory, a cron job, or `/tmp` gets a raw stack trace instead of guidance.
## Solution
Implement XDG Base Directory-compliant config resolution: search `~/.config/librofm-downloader/` first, then fall back to the current working directory. Treat missing `config.yaml` as non-fatal (built-in defaults exist for every field) but always inform the user. Treat missing `secrets.yaml` as fatal with a clear error listing all searched paths. Apply the same XDG default to `download_history.json`. Auto-create the XDG config directory on demand if it doesn't exist.
## User Stories
1. As a first-time user who just installed via pip, I want to run `librofm-downloader` from my home directory and get a clear message telling me where to put `secrets.yaml`, so that I don't have to read source code or guess file locations.
2. As a first-time user, I want the XDG config directory (`~/.config/librofm-downloader/`) to be created automatically on first run, so that I don't need to manually mkdir before dropping in my files.
3. As a user without a `config.yaml`, I want the tool to use built-in defaults and print a non-fatal notice like `"config.yaml not found. Using built-in defaults."`, so that I know what's happening but can still run the tool.
4. As a user with only a `secrets.yaml`, I want the tool to work using all hardcoded defaults for format, output directory, extras, and covers, so that I don't need to create a config file just to get started.
5. As a power user keeping files in a project directory (e.g., Nix flake, docker-compose), I want `--config ./config.yaml --secrets ./secrets.yaml` to bypass all search logic and use my explicit paths, so that existing workflows aren't disrupted.
6. As a user running from cron or a script, I want `librofm-downloader` to find its config regardless of working directory, so that my automated downloads don't break when the scheduler's CWD changes.
7. As a user who already has `config.yaml` and `secrets.yaml` in CWD, I want the tool to continue finding them there (CWD fallback), so that this change is backward-compatible and requires zero migration.
8. As a user with `secrets.yaml` in XDG but no `config.yaml` anywhere, I want the tool to load secrets successfully, use defaults for everything else, and proceed to download, so that minimal setup is sufficient.
9. As a user with neither file anywhere, I want a fatal error that lists every path searched (XDG then CWD) for `secrets.yaml`, so that I know exactly where to place the file.
10. As a user running with `--verbose`, I want to see which paths were searched and which was selected for each file, so that I can debug config resolution issues.
11. As a user, I want `download_history.json` to default to the XDG directory alongside config/secrets, so that download history is shared regardless of which directory I run the tool from.
12. As a user who has been using CWD-based history, I want `--history ./download_history.json` to continue working as an explicit override, so that my existing history file is still found.
## Implementation Decisions
- **Search order**: XDG `~/.config/librofm-downloader/` is checked first; current working directory is checked second. First match wins. Explicit CLI flags (`--config`, `--secrets`, `--history`) skip search entirely and use the provided path.
- **Missing config.yaml**: Non-fatal. Always prints a warning message listing searched paths. All fields fall back to their hardcoded defaults (`format=m4b_mp3_fallback`, `output_dir=./audiobooks`, `download_extras=True`, `download_covers=True`). The `load_config()` function signature gains optional-path semantics — callers pass `None` to trigger search.
- **Missing secrets.yaml**: Fatal exit code 1. Error message lists all searched paths in order. No Python traceback leaks to the user.
- **History file**: Default location changes from CWD-relative `download_history.json` to XDG `~/.config/librofm-downloader/download_history.json`. Same XDG-then-CWD search applies when no `--history` flag is given.
- **XDG directory auto-creation**: The `~/.config/librofm-downloader/` directory is created with `Path.mkdir(parents=True, exist_ok=True)` on first access. No config files are written — only the directory itself.
- **Resolution function**: A new internal module function handles path resolution. It takes a filename, returns either a resolved `Path` (file exists) or `None` (not found anywhere). This keeps search logic out of both `load_config()` and `cli.py`.
- **CLI default values**: The `run()` function's default parameters for `config_path`, `secrets_path`, and `history_path` change from CWD strings to `None`. `None` signals "use search." Non-None means "use this exact path."
- **Error handling boundary**: `FileNotFoundError` inside `load_config()` is caught and converted to domain errors (`ConfigError` subclasses) before reaching `cli.py`. The CLI layer never sees raw OS exceptions from config loading.
## Testing Decisions
- Tests should verify external behavior: given a set of filesystem states (XDG exists/missing, CWD exists/missing), what does the resolver return? What messages are printed?
- **Config resolver tests**: Test `_resolve_config_file()` with combinations of (XDG present, CWD present, both present, neither present). Verify return value and search order precedence.
- **Integration tests for `load_config()`**: Pass `None` for paths (triggering search) with various filesystem setups. Verify correct Config dataclass output or correct exception type.
- **CLI tests**: Invoke `run()` with no arguments (all None) against different filesystem layouts. Capture console output to verify the "using defaults" message appears exactly when config is absent, and the fatal error appears exactly when secrets are absent.
- **Auto-creation test**: Verify that a nonexistent XDG parent directory gets created after resolution runs, and that subsequent calls don't fail or warn.
- **Backward compatibility test**: Explicit `--config ./config.yaml --secrets ./secrets.yaml` still works when CWD has the files but XDG does not — search must be skipped entirely for explicit paths.
- Prior art: existing `tests/test_config.py` (25 tests) and `tests/test_cli.py` (28+ tests) show patterns for mocking filesystem state and capturing Rich console output.
## Out of Scope
- XDG `XDG_CONFIG_HOME` environment variable respect (hardcode `~/.config/` for now).
- Windows support (no `%APPDATA%` fallback).
- Config file generation/wizard (`librofm-downloader init`).
- Migration helper to move existing CWD files to XDG.
- Any changes to the config YAML schema, field names, or merge semantics.
- Changes to how `--select` mode or any other feature resolves paths.
## Further Notes
This is a pure UX/resilience improvement — zero changes to download pipeline, auth flow, API client, or path sanitization. The blast radius is confined to `config.py` (resolution + error translation) and `cli.py` (default values + messaging). All existing tests must continue to pass unchanged since they pass explicit fixture paths.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Problem Statement
Running
librofm-downloaderfrom any directory other than the one containingconfig.yamlandsecrets.yamlcrashes with an unhelpful PythonFileNotFoundErrortraceback. The tool has no concept of a user-level config home — both files are resolved purely as CWD-relative paths with no search strategy. A user who installs via pip and runs the command from their home directory, a cron job, or/tmpgets a raw stack trace instead of guidance.Solution
Implement XDG Base Directory-compliant config resolution: search
~/.config/librofm-downloader/first, then fall back to the current working directory. Treat missingconfig.yamlas non-fatal (built-in defaults exist for every field) but always inform the user. Treat missingsecrets.yamlas fatal with a clear error listing all searched paths. Apply the same XDG default todownload_history.json. Auto-create the XDG config directory on demand if it doesn't exist.User Stories
librofm-downloaderfrom my home directory and get a clear message telling me where to putsecrets.yaml, so that I don't have to read source code or guess file locations.~/.config/librofm-downloader/) to be created automatically on first run, so that I don't need to manually mkdir before dropping in my files.config.yaml, I want the tool to use built-in defaults and print a non-fatal notice like"config.yaml not found. Using built-in defaults.", so that I know what's happening but can still run the tool.secrets.yaml, I want the tool to work using all hardcoded defaults for format, output directory, extras, and covers, so that I don't need to create a config file just to get started.--config ./config.yaml --secrets ./secrets.yamlto bypass all search logic and use my explicit paths, so that existing workflows aren't disrupted.librofm-downloaderto find its config regardless of working directory, so that my automated downloads don't break when the scheduler's CWD changes.config.yamlandsecrets.yamlin CWD, I want the tool to continue finding them there (CWD fallback), so that this change is backward-compatible and requires zero migration.secrets.yamlin XDG but noconfig.yamlanywhere, I want the tool to load secrets successfully, use defaults for everything else, and proceed to download, so that minimal setup is sufficient.secrets.yaml, so that I know exactly where to place the file.--verbose, I want to see which paths were searched and which was selected for each file, so that I can debug config resolution issues.download_history.jsonto default to the XDG directory alongside config/secrets, so that download history is shared regardless of which directory I run the tool from.--history ./download_history.jsonto continue working as an explicit override, so that my existing history file is still found.Implementation Decisions
~/.config/librofm-downloader/is checked first; current working directory is checked second. First match wins. Explicit CLI flags (--config,--secrets,--history) skip search entirely and use the provided path.format=m4b_mp3_fallback,output_dir=./audiobooks,download_extras=True,download_covers=True). Theload_config()function signature gains optional-path semantics — callers passNoneto trigger search.download_history.jsonto XDG~/.config/librofm-downloader/download_history.json. Same XDG-then-CWD search applies when no--historyflag is given.~/.config/librofm-downloader/directory is created withPath.mkdir(parents=True, exist_ok=True)on first access. No config files are written — only the directory itself.Path(file exists) orNone(not found anywhere). This keeps search logic out of bothload_config()andcli.py.run()function's default parameters forconfig_path,secrets_path, andhistory_pathchange from CWD strings toNone.Nonesignals "use search." Non-None means "use this exact path."FileNotFoundErrorinsideload_config()is caught and converted to domain errors (ConfigErrorsubclasses) before reachingcli.py. The CLI layer never sees raw OS exceptions from config loading.Testing Decisions
_resolve_config_file()with combinations of (XDG present, CWD present, both present, neither present). Verify return value and search order precedence.load_config(): PassNonefor paths (triggering search) with various filesystem setups. Verify correct Config dataclass output or correct exception type.run()with no arguments (all None) against different filesystem layouts. Capture console output to verify the "using defaults" message appears exactly when config is absent, and the fatal error appears exactly when secrets are absent.--config ./config.yaml --secrets ./secrets.yamlstill works when CWD has the files but XDG does not — search must be skipped entirely for explicit paths.tests/test_config.py(25 tests) andtests/test_cli.py(28+ tests) show patterns for mocking filesystem state and capturing Rich console output.Out of Scope
XDG_CONFIG_HOMEenvironment variable respect (hardcode~/.config/for now).%APPDATA%fallback).librofm-downloader init).--selectmode or any other feature resolves paths.Further Notes
This is a pure UX/resilience improvement — zero changes to download pipeline, auth flow, API client, or path sanitization. The blast radius is confined to
config.py(resolution + error translation) andcli.py(default values + messaging). All existing tests must continue to pass unchanged since they pass explicit fixture paths.