Skip to content

d3d12-core: ignore empty user-dir and log env vars - #2874

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
kirigayakazima:fix/d3d12-core-empty-env-fallback
Dec 1, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
kirigayakazima:fix/d3d12-core-empty-env-fallback

Conversation

@kirigayakazima

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-CORE-EMPTY-ENV-FALLBACK (#2570 (comment))

What and why

leonida-core ignored empty values for LEONIDA_GAME_DIR, LEONIDA_CACHE_DIR, LEONIDA_DATA_DIR, LEONIDA_TEST_TITLES, and LEONIDA_CONFIG, but two fallbacks still accepted empty strings:

  1. d3d12/crates/leonida-core/src/config.rs (default_dir):
    • default_dir called std::env::var_os directly (bypassing Config::resolve's env lookup closure) and did not filter .filter(|v| !v.is_empty()). If LOCALAPPDATA="" (or XDG_CACHE_HOME="" / HOME="") was set, default_dir returned a relative path like leonida/cache instead of falling through to the next directory or returning an explicit error.
    • default_dir now takes &env and filters out empty values consistently with the rest of config.rs, and the existing unit test env stubs provide a fake LOCALAPPDATA value so tests do not read host process environment variables.
  2. d3d12/crates/leonida-core/src/logging.rs (resolve_filter):
    • Previously std::env::var("LEONIDA_LOG").or_else(|_| std::env::var("RUST_LOG")).ok().and_then(|v| EnvFilter::try_new(v).ok()) stopped at LEONIDA_LOG="" (or invalid syntax) instead of falling back to RUST_LOG, and passed "" to EnvFilter::try_new("") (which parses to an empty filter that silences all logs instead of using default_level).
    • Extracted resolve_filter, trimming and ignoring empty env strings and falling back cleanly from LEONIDA_LOG -> RUST_LOG -> default_level.

Tested

Verified locally (Windows 11, Rust 1.98.1, no game data):

  • Red before fix: cargo test --locked -p leonida-core failed on both empty_user_dir_env_values_fall_back_and_missing_dirs_error and empty_or_invalid_leonida_log_falls_back_to_rust_log_or_default (13 passed; 2 failed).
  • Green after fix:
    • cargo test --locked -p leonida-core: 15 passed; 0 failed
    • cargo clippy --locked -p leonida-core --all-targets -- -D warnings: 0 warnings, exit 0
    • python -I scripts/check_no_game_data.py & git diff --check: clean

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a)

@merzleckeier

Copy link
Copy Markdown
Contributor

Checked the empty user-directory fallback through Config::load in a child process with a8fcac74a + ed867d7b2 on the Windows target under Wine 11.22. The child inherited explicit empty overrides, a nonempty XDG_CACHE_HOME containing a space, and a temporary leonida.toml. Main resolved the cache as relative leonida/cache, omitting the XDG directory. The PR used the XDG directory for cache and data and kept the config file’s synthetic folder and test-title entries. All 17 core tests passed, including the logging fallback test.

AI-assisted with OpenAI Codex.

@KelboLive KelboLive left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct and well-tested. All five LEONIDA_* path vars plus LOCALAPPDATA/XDG_CACHE_HOME/HOME now treat a zero-length value as unset, the new test pins both the fallback order and the all-empty error, and mutating the default_dir filter fails it exactly as described ("leonida\\cache" vs "/tmp/xdg/leonida/cache"). leonida-core 15/0, clippy clean, merge-tree clean.

The resolve_filter extraction is a real fix, not a tidy-up: EnvFilter::try_new("") returns Ok with an empty filter, i.e. silence everything — so an empty LEONIDA_LOG used to black out logging entirely rather than fall through to RUST_LOG. Worth one sentence in the commit, since that is the actual bug.

Two notes:

  1. "Empty" is zero-length for paths but trim-based for logging. LEONIDA_CACHE_DIR=" " (or LOCALAPPDATA=" ") is still accepted and produces the relative " /leonida/cache" this PR is about. Pre-existing, but it is the same class; either trim path vars too or say "zero-length only" in the comment.
  2. Behaviour change for a mute switch: anyone who exported LEONIDA_LOG= deliberately now inherits RUST_LOG. LEONIDA_LOG=off remains the explicit way — worth a line in the doc/comment.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2874 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files

Verdict: PASS. Both changes fix a real defect, and each new test goes red when the change it belongs to is put back.

Head a8fcac74, base the current main 88a9f133, merge base ed867d7b, 1 commit, 2 files, +95/−15: d3d12/crates/leonida-core/src/config.rs and d3d12/crates/leonida-core/src/logging.rs.

1. The user-directory fix, and what it was doing

main's default_dir (config.rs:138):

fn default_dir(name: &str) -> Result<PathBuf> {
    let base = std::env::var_os("LOCALAPPDATA")
        .or_else(|| std::env::var_os("XDG_CACHE_HOME"))
        .map(PathBuf::from)
        .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".cache")))

var_os returns Some("") for a variable that is set but empty, so an empty LOCALAPPDATA was used, and PathBuf::from("") joined with leonida and cache gives:

PathBuf::from("").join("leonida").join("cache") = "leonida/cache"
  是相对路径吗: true

A relative path — the cache and data directories would land in the current working directory instead of the user's cache directory, and the XDG_CACHE_HOME/HOME fallbacks would never be reached. The pull request filters empty values out and passes the environment in so it can be tested:

let non_empty = |k: &str| env(k).filter(|v| !v.is_empty());
let base = non_empty("LOCALAPPDATA")
    .or_else(|| non_empty("XDG_CACHE_HOME"))
    .map(PathBuf::from)
    .or_else(|| non_empty("HOME").map(|h| PathBuf::from(h).join(".cache")))

The new test covers the three cases that matter: an empty LOCALAPPDATA falls through to XDG_CACHE_HOME (/tmp/xdg/leonida/cache), empty LOCALAPPDATA and XDG_CACHE_HOME fall through to HOME (/tmp/home/.cache/leonida/cache), and all three empty is an error rather than a relative path.

2. The log-filter fix, and a behaviour change worth naming

main's init():

let filter = std::env::var("LEONIDA_LOG")
    .or_else(|_| std::env::var("RUST_LOG"))
    .ok()
    .and_then(|v| EnvFilter::try_new(v).ok())
    .unwrap_or_else(|| EnvFilter::new(default_level));

Two problems: or_else only fires when the variable is unset, so LEONIDA_LOG="" was used as an empty filter (logging off) instead of falling through; and EnvFilter::try_new failing sent the whole thing to the default level, ignoring RUST_LOG.

The pull request's resolve_filter trims, ignores empty, and tries the next variable when one cannot be parsed:

let parse = |key: &str| {
    env(key)
        .map(|v| v.trim().to_owned())
        .filter(|v| !v.is_empty())
        .and_then(|v| EnvFilter::try_new(v).ok())
};
parse("LEONIDA_LOG").or_else(|| parse("RUST_LOG")).unwrap_or_else(|| EnvFilter::new(default_level))

So the behaviour changes in one place beyond the empty-value case: an invalid LEONIDA_LOG now falls through to RUST_LOG before the default, where main went straight to the default. The new test pins that (LEONIDA_LOG="invalid[filter" with RUST_LOG="debug" gives debug). I think falling through is the better reading of "ignore the bad one", but it is a deliberate change and worth naming rather than leaving implicit.

3. Red and green, and the discrimination

cargo test -p leonida-core (Linux, pinned toolchain):

tree result
this pull request 15 passed, 0 failed
default_dir reverted to main's empty-value handling (the test kept) 14 passed, 1 FAILED — config::tests::empty_user_dir_env_values_fall_back_and_missing_dirs_error
resolve_filter reverted to no trim / no empty filter (the test kept) 14 passed, 1 FAILED — logging::tests::empty_or_invalid_leonida_log_falls_back_to_rust_log_or_default, left: "" right: "warn"

With both reverts in place the file is back to the pull request's version and the suite is 15 passed.

4. Two of the four new tests guard behaviour that was already there

config::tests::empty_env_values_are_ignored and empty_test_titles_env_is_ignored check that an empty LEONIDA_GAME_DIR / LEONIDA_TEST_TITLES is ignored. That was already the case in main — env_path has filtered empty values all along (config.rs:81):

let env_path = |k: &str| env(k).filter(|v| !v.is_empty()).map(PathBuf::from);

So those two are regression guards for existing behaviour rather than fixes, and they would have passed against main's production code. The two that do not pass against main are the ones in §3. Not a criticism of the pull request — a guard is worth having — but it is worth knowing which of the new tests carry the change.

5. What I did not check

  • Windows. Everything above is Linux (Fedora 45, x86_64) with the pinned toolchain (rustc 1.98.1). LOCALAPPDATA is the Windows variable, and the bug it fixes is most likely to be hit on Windows; I exercised it through the injected environment rather than through a real Windows process.
  • The other crates in the workspace — leonida-gpu, leonida-hle and leonida-loader do not build on Linux for reasons that predate this pull request (upstream: "The crates are Windows-only today … Linux follows Linux: build leonida.exe and run it under Proton or Wine #2577"), so I ran leonida-core on its own and not the workspace suite.
  • The real tracing_subscriber output. The test compares EnvFilter::to_string(), not what actually gets logged; I did not capture log lines.
  • EnvFilter::try_new on other invalid inputs — I only exercised the empty, whitespace and one malformed value that the test uses.
  • cargo clippy (already failing on main, run as a report by the workflow) and cargo fmt (left out of the d3d12 workflow).

AI-assisted: MiniMax M3.1 Flash.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR on native Windows 11 (MSVC x64), Core Ultra 7 155H, RTX 4070 Laptop. Not the author. AI-assisted (Claude Code agent oreofr-agent, for OreoFR). No game files needed.

Head a8fcac74a merged into the current main 0ff8a532f (tree 2159e6be3, clean merge).

  • Green: cargo test --locked -p leonida-core → 15 passed / 0 failed, including config::tests::empty_user_dir_env_values_fall_back_and_missing_dirs_error and logging::tests::empty_or_invalid_leonida_log_falls_back_to_rust_log_or_default.
  • The new tests call default_dir(name, env) and resolve_filter(default_level, env), which this PR introduces, so they cannot run on main as they are. Red by mutation instead, one at a time, each restored afterwards:
    1. config.rs default_dir: non_empty = |k| env(k) (empty-value filter removed) → empty_user_dir_env_values_fall_back_and_missing_dirs_error FAILED, 14 others ok.
    2. logging.rs resolve_filter: .filter(|v| !v.is_empty()) removed → empty_or_invalid_leonida_log_falls_back_to_rust_log_or_default FAILED, 14 others ok.
  • Each new test fails exactly when its own fix is removed, and passes with it. Tree clean afterwards.

Verdict from this side: the change does what it says, and its tests guard it.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging. Head a8fcac74a merges cleanly with main 0d4cbd589.
Claim: kirigayakazima's PR, part of MoonEyes's DX12 train (Moon told on the bridge before this merge, no objection).
Red/green: by oreofr-agent (not the author), native Windows 11, comment above. Green cargo test --locked -p leonida-core 15/0 on main 85ec1d33b + this head (tree 22b78d06f); red by mutation, each new test fails without its own fix. Main since gained only #2898 (leonida-hle, the Bink skip), which this crate does not use. Also verified by imShangLi (Linux) and merzleckeier.
In-game run: not needed for the change itself (empty values of the user-directory and log variables at start-up, nothing that runs per frame). It was also in dadacap-main's train run: RTX 5080, 303 s in the world, 0 device loss.
CI: green on the head (all checks); main's CI green on 0d4cbd589.
AI-assisted: yes, Claude Code agent oreofr-agent, merged on OreoFR's (maintainer) written OK.

@OreoFR
OreoFR merged commit 3d8ea10 into ProjectLeonida:main Dec 1, 2026
6 checks passed
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.

5 participants