Skip to content

Add XDG Base Directory Specification support#342

Open
christopher-buss wants to merge 8 commits into
lune-org:mainfrom
christopher-buss:feature/xdg
Open

Add XDG Base Directory Specification support#342
christopher-buss wants to merge 8 commits into
lune-org:mainfrom
christopher-buss:feature/xdg

Conversation

@christopher-buss

@christopher-buss christopher-buss commented Jul 30, 2025

Copy link
Copy Markdown

Summary

Implements XDG Base Directory Specification compliance for Lune's cache and state directories while maintaining full backwards compatibility with existing installations.

  • Cache directory: $XDG_CACHE_HOME/lune (fallback: ~/.lune)
  • State directory: $XDG_STATE_HOME/lune (fallback: ~/.lune or $XDG_CACHE_HOME/lune)
  • REPL history: Stored in state directory as .lune_history
  • Type definitions: Remain in ~/.lune/.typedefs/ for LSP compatibility

Key Features

  • Full backwards compatibility: Automatically detects and prefers existing legacy directories
  • Environment variable support: LUNE_CACHE and LUNE_STATE override all other settings
  • Cross-platform: Works on Linux (XDG), Windows (Known Folders), and macOS (Standard Directories)

Implementation Details

  • Uses existing directories crate
  • New dirs module provides cache_dir(), state_dir(), and typedefs_dir() functions
  • Integrated across all components: build cache, REPL history, LSP setup
  • Automatic directory creation prevents file operation errors

Backwards Compatibility

The implementation prioritizes existing user setups:

  1. If legacy .lune/target exists but XDG cache doesn't → use legacy
  2. If legacy .lune_history exists but XDG state doesn't → use legacy
  3. Environment variables (LUNE_CACHE/LUNE_STATE) always take precedence
  4. Fallback to original logic if XDG detection fails

Test Plan

  • All existing functionality works unchanged
  • New XDG directories are created and used appropriately
  • Environment variable overrides work correctly
  • Backwards compatibility scenarios function properly
  • Cross-platform compatibility verified
  • Error handling and fallbacks tested
  • Thread safety and cleanup in tests

Fixes #300

Adds etcetera 0.10.0 to enable cross-platform XDG directory compliance for cache and state files.
Adds cross-platform XDG-compliant directory handling for cache and state
files while maintaining LSP compatibility for type definitions.

- Add etcetera 0.10.0 dependency for robust XDG support
- Create dirs.rs module with cache_dir(), state_dir(), and typedefs_dir()
- Support LUNE_CACHE and LUNE_STATE environment variable overrides
- Include comprehensive unit tests with cross-platform compatibility
- Use proper anyhow::Result error handling matching project standards
- Keep type definitions in ~/.lune/.typedefs/ for LSP compatibility

Part of implementing issue lune-org#300 - XDG Base Directory specification support.
Implements XDG Base Directory Specification for cache and state directories
while maintaining backwards compatibility by preferring existing legacy
directories over new XDG locations to minimize user friction.

Key features:
- Respects XDG_CACHE_HOME and XDG_STATE_HOME environment variables
- Supports LUNE_CACHE and LUNE_STATE override variables
- Graceful fallback when state directory is unsupported (Windows/macOS)
- Comprehensive test coverage with cross-platform compatibility
- Maintains typedefs in home directory for LSP compatibility

Addresses issue lune-org#300 - Cache & REPL history paths do not respect XDG
Eliminates duplicate dependency by using the existing directories crate
instead of etcetera for XDG Base Directory support. Both provide identical
XDG compliance and cross-platform directory handling.

Changes:
- Remove etcetera dependency from Cargo.toml
- Update dirs.rs to use directories::BaseDirs instead of choose_base_strategy()
- Fix test concurrency issues with proper mutex synchronization
- Maintain identical XDG functionality and backwards compatibility

Benefits:
- Consistent dependency usage across codebase
- Reduced dependency count and build complexity
- Same XDG compliance behavior on all platforms
- All existing functionality preserved
Integrates XDG helper functions throughout the Lune codebase, providing
full XDG Base Directory Specification compliance while maintaining
backwards compatibility.

Integration changes:
- Add dirs module declaration to main.rs for crate-wide access
- Update build target cache (target.rs) to use XDG cache_dir()
- Update REPL history (repl.rs) to use XDG state_dir()
- Update LSP typedefs (setup.rs) to use XDG typedefs_dir()
- Fix collapsible-if clippy warnings in test cleanup code

XDG compliance behavior:
- Build cache: $XDG_CACHE_HOME/lune/target (fallback: ~/.lune/target)
- REPL history: $XDG_STATE_HOME/lune/.lune_history (fallback: ~/.lune_history)
- LSP typedefs: ~/.lune/.typedefs/{version}/ (consistent for LSP compatibility)

All functions include robust error handling with fallback to original
logic if XDG helpers fail. Environment variable overrides (LUNE_CACHE,
LUNE_STATE) take precedence. Backwards compatibility ensures existing
user setups continue working without changes.
Documents the new XDG Base Directory Specification compliance with
backwards compatibility for existing users. Includes details about
directory locations, environment variable overrides, and automatic
detection of existing installations.
Create parent directories for .lune_history file when using XDG state directory
to prevent file creation errors when the state directory doesn't exist yet.
Replace unwrap_or_else with ignored error parameters with unwrap_or
for better code clarity and performance in XDG fallback logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cache & REPL history paths do not respect XDG

1 participant