Skip to content

[BUG] [v0.0.7] CORTEX_CONFIG_DIR/DATA_DIR/CACHE_DIR env overrides don't resolve relative paths, unlike CORTEX_HOME #175

@climax-dev-1

Description

@climax-dev-1

Bug Report

Project

cortex

Description

In cortex-common/src/dirs.rs, the AppDirs::new() function has explicit relative-path resolution for the CORTEX_HOME environment variable (lines 53-68, fixing issue #2008), but the individual directory overrides CORTEX_CONFIG_DIR, CORTEX_DATA_DIR, and CORTEX_CACHE_DIR (lines 104-112) use the raw value from the environment without any relative-path resolution.

// CORTEX_HOME: properly resolves relative paths (lines 49-71)
if let Ok(home) = std::env::var("CORTEX_HOME") {
    let home = PathBuf::from(&home);
    let home = if home.is_relative() {
        match std::env::current_dir() {
            Ok(cwd) => {
                let resolved = cwd.join(&home);
                resolved.canonicalize().unwrap_or(resolved)
            }
            Err(_) => { /* fallback */ }
        }
    } else {
        home
    };
    // ...
}

// Individual overrides: NO relative-path resolution (lines 103-112)
let config_dir = std::env::var("CORTEX_CONFIG_DIR")
    .map(PathBuf::from)       // BUG: raw PathBuf, no relative-path handling
    .unwrap_or(config_dir);
let data_dir = std::env::var("CORTEX_DATA_DIR")
    .map(PathBuf::from)       // BUG: same issue
    .unwrap_or(data_dir);
let cache_dir = std::env::var("CORTEX_CACHE_DIR")
    .map(PathBuf::from)       // BUG: same issue
    .unwrap_or(cache_dir);

This means setting CORTEX_CONFIG_DIR=. or CORTEX_CONFIG_DIR=./config will create config files relative to whatever the current working directory happens to be at startup, which changes depending on where the user invokes cortex from. This is the exact same class of bug that was fixed for CORTEX_HOME in issue #2008.

Error Message

No error message — config/data/cache files are silently created in unexpected locations relative to the current working directory.

Debug Logs

N/A — source code analysis.

System Information

Linux (Ubuntu), source code review.

Steps to Reproduce

  1. Set CORTEX_CONFIG_DIR=. (or any relative path)
  2. Run cortex from /home/user/project-a — config is created at /home/user/project-a/config.toml
  3. Run cortex from /home/user/project-b — a separate config is created at /home/user/project-b/config.toml
  4. The user now has fragmented config files scattered across directories

Expected Behavior

The individual directory overrides should apply the same relative-path resolution as CORTEX_HOME: resolve relative paths against the current working directory at startup and canonicalize them to absolute paths.

Fix — apply the same resolution helper to all three overrides:

fn resolve_env_path(var_name: &str) -> Option<PathBuf> {
    std::env::var(var_name).ok().map(|val| {
        let path = PathBuf::from(&val);
        if path.is_relative() {
            match std::env::current_dir() {
                Ok(cwd) => {
                    let resolved = cwd.join(&path);
                    resolved.canonicalize().unwrap_or(resolved)
                }
                Err(_) => path,
            }
        } else {
            path
        }
    })
}

let config_dir = resolve_env_path("CORTEX_CONFIG_DIR").unwrap_or(config_dir);
let data_dir = resolve_env_path("CORTEX_DATA_DIR").unwrap_or(data_dir);
let cache_dir = resolve_env_path("CORTEX_CACHE_DIR").unwrap_or(cache_dir);

Actual Behavior

Relative paths in CORTEX_CONFIG_DIR, CORTEX_DATA_DIR, and CORTEX_CACHE_DIR are used as-is without resolution, causing config/data/cache files to be created in different locations depending on the current working directory at startup. This leads to fragmented state and user confusion.

Additional Context

The doc comment for AppDirs::new() (lines 42-44) explicitly mentions that relative paths are resolved: "Note: Relative paths in environment variables (like CORTEX_HOME=.) are automatically resolved to absolute paths to prevent config files being created in unexpected locations." However, this promise only applies to CORTEX_HOME, not to the individual directory overrides documented on lines 38-40.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions