Skip to content

test(tauri): stop tests resolving paths against another test's HOME - #263

Merged
LIUBINfighter merged 1 commit into
devfrom
fix/test-home-isolation
Oct 3, 2026
Merged

LIUBINfighter merged 1 commit into
devfrom
fix/test-home-isolation

Conversation

@LIUBINfighter

Copy link
Copy Markdown
Owner

The failure

CI on #261 failed once and passed on rerun:

test tests::delete_file_repo_trash_rejects_paths_outside_the_repo_root ... FAILED
  left:  Some("No such file or directory (os error 2)")
  right: Some("path-outside-workspace")

The assertion is about path scope, so the error surfaces far from its cause. delete_file first canonicalizes the target, then resolves the implicit default workspace root, and only then checks scope. The ENOENT came out of that middle step.

Root cause

with_temp_home replaces the process-global HOME variable for the duration of a test, because Tauri resolves app_data_dir() straight from it. Nothing stopped other tests from reading HOME while it was replaced.

Instrumenting the resolver showed 10 misresolutions in one test run — a test running concurrently resolved its default workspace root inside another test's temporary home:

RACE-WINDOW observed HOME=.../tabst-tauri-test-load-app-state-standalone-12736-1791039316453
                     default_root=".../tabst-tauri-test-load-app-state-standalone-12736-1791039316453/Documents/tabst"

with_temp_home then restores HOME and deletes that directory. If create_dir_all on the misresolved path loses the race against remove_dir_all, it reports No such file or directory (os error 2) — which is exactly what the scope assertion ended up printing. That is why the failure is rare, unrelated to the assertion it breaks, and why a rerun "fixes" it.

Change

Resolve deterministically instead of trying to win the timing:

  • default_save_dir and global_metadata_dir resolve through test_resolved_home, which prefers a thread-local override and otherwise uses the real home captured before any test replaced HOME. Nothing on the resolution path reads the mutable variable any more.
  • with_temp_home still writes HOME (Tauri needs it) but also installs the thread-local override, so tests that install it keep their private layout and tests that do not are immune.
  • lock_home_env ignores mutex poisoning, so one panicking test no longer turns every later test into a secondary failure.

Production resolution is untouched: without cfg(test) the override path compiles to None and home_dir() / document_dir() stay the single source of truth.

Verification

  • a_replaced_home_does_not_leak_into_path_resolution is a new deterministic regression test. It fails against the previous resolution (left: Some(".../tabst-tauri-test-foreign-home"), right: Some("/Users/runner")) and passes now.
  • The instrumented detector fired 3–8 times per run against the old resolution and 0 times against the new one, over 3 runs each.
  • cargo test — 28 passed
  • cargo fmt --check, cargo check, cargo clippy --all-targets — clean, no new warnings
  • pnpm verify:tauri — end to end

An earlier version of this fix made the resolver lock the HOME mutex. That deadlocked the suite: scope_for_canonical_path is called while holding the allowed-path registry mutex, so locking in the resolver inverted the lock order against with_temp_home. The capture-based approach avoids taking any lock on the resolution path.

CI on #261 failed once with

    tests::delete_file_repo_trash_rejects_paths_outside_the_repo_root
    left:  Some("No such file or directory (os error 2)")
    right: Some("path-outside-workspace")

and passed on rerun. The assertion is about path scope, so the error is reported
miles away from its cause: `delete_file` first canonicalizes the target, then
resolves the implicit default workspace root, and only then checks scope. The
ENOENT came out of that middle step.

`with_temp_home` replaces the process-global `HOME` for the duration of a test,
because Tauri resolves `app_data_dir()` straight from it. Nothing kept other
tests from reading `HOME` while it was replaced, so a test running concurrently
resolved its default workspace root inside another test's temporary home and then
raced the `fs::remove_dir_all` that deletes it. `create_dir_all` losing that race
reports `No such file or directory (os error 2)`, which is what the scope
assertion ended up printing.

Instrumenting the resolver showed 10 such misresolutions in a single test run,
so this was a race the suite hit often and only diagnosed rarely.

Fix the resolution instead of the timing:

- `default_save_dir` and `global_metadata_dir` now resolve through
  `test_resolved_home`, which prefers a thread-local override and otherwise uses
  the real home captured before any test replaced `HOME`. Nothing reads the
  mutable variable on the resolution path any more.
- `with_temp_home` still writes `HOME` (Tauri needs it) but also installs the
  thread-local override, so tests that do install it keep their private layout,
  and tests that do not are immune.
- `lock_home_env` ignores mutex poisoning, so one panicking test no longer turns
  every later test into a secondary failure.

`a_replaced_home_does_not_leak_into_path_resolution` pins the invariant and fails
against the previous resolution (`left: Some(".../tabst-tauri-test-foreign-home")`).

Test-support only; production resolution still goes straight to `home_dir()` and
`document_dir()`.

Verified: `cargo test` 28 passed, `cargo fmt --check`, `cargo check`,
`cargo clippy --all-targets` (no new warnings), `pnpm verify:tauri` end to end.
@LIUBINfighter
LIUBINfighter merged commit c56d99a into dev Oct 3, 2026
3 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.

1 participant