Skip to content

Commit 385957e

Browse files
authored
fix(core): resolve main worktree root without a Windows verbatim prefix (#36649)
## Current Behavior On Windows, running any cache-touching target from inside a **git worktree** with **Nx Cloud enabled** crashes after the tasks themselves have already succeeded: ``` NX Successfully ran target build for 3 projects NX EISDIR: illegal operation on a directory, lstat 'C:' Error: EISDIR: illegal operation on a directory, lstat 'C:' at Object.realpathSync (node:fs:2729:25) at toRealPath (node:internal/modules/helpers:65:13) at tryFile (node:internal/modules/cjs/loader:589:10) at tryPackage (node:internal/modules/cjs/loader:548:16) at Module._findPath (node:internal/modules/cjs/loader:823:18) at Module._resolveFilename (node:internal/modules/cjs/loader:1487:27) ``` This is a regression introduced in 22.7 and still present on 23.1.1. It makes Nx Cloud effectively unusable on Windows for anyone working in a worktree. The only workarounds are `NX_NO_CLOUD=true` or setting `NX_CACHE_DIRECTORY` by hand. ### Root cause 22.7 added worktree-shared caching, which routed `cacheDir` through `getMainWorktreeRoot`. That function resolves the main repo root with Rust's `std::fs::canonicalize` — which on Windows **always** returns a verbatim (extended-length) path: ``` \\?\C:\Users\Viktor\work\nx_test\nx_issue_demo\org ``` `path.win32.join` treats the device namespace as opaque and preserves that prefix, so it survives all the way down: ``` get_main_worktree_root -> cacheDir -> join(cacheDir, 'cloud') -> require(fullPath) (worktree.rs) (cache-directory.ts) (nx-cloud/update-manager.ts:185) (:98, :114, :139, :155) ``` Node's CJS resolver does not recognize the drive boundary in `\\?\C:\`, so `Module._nodeModulePaths` walks *past* the filesystem root and ends up calling `realpathSync('C:')` — hence `EISDIR`. The underlying defect is a long-standing Node bug ([nodejs/node#60438](nodejs/node#60438), open for months; see also [#60435](nodejs/node#60435), [#61165](nodejs/node#61165)). Rather than wait on it, Nx should stop emitting the path form Node mishandles. ## Expected Behavior Targets run normally in a worktree with Nx Cloud enabled. `get_main_worktree_root` now canonicalizes via [`dunce`](https://lib.rs/crates/dunce) instead of `std`: ```rust let abs_path = dunce::canonicalize(&abs_path).unwrap_or(abs_path); ``` `dunce` was already a dependency of the crate and already used elsewhere in the native code (`watch_filterer.rs`), so this adds nothing new to the dependency tree. Importantly, this is **not** a blind prefix strip. `dunce` removes the `\\?\` prefix only when the result is still a valid Win32 path, and deliberately keeps it otherwise — so `\\?\UNC\server\share` is not mangled into a broken relative path, and paths that genuinely require the prefix are left intact. This also adds the first tests for `resolve_main_worktree_root`, which previously had none: one covering the main-repo case, one covering resolution from a linked worktree plus the absence of a verbatim prefix. ### Notes for reviewers Two limitations worth knowing, both deliberate: 1. **`dunce` keeps the prefix when the path exceeds 260 characters or contains a reserved DOS name** (`CON`, `NUL`, `COM1`…). Those workspaces will still hit this crash. There is no clean fix on the Nx side — such a path genuinely needs the prefix to work with Win32 APIs — so the residual case is left to the upstream Node fix. 2. **The new tests cannot fail in CI.** `dunce::canonicalize` is identical to `std::fs::canonicalize` on non-Windows, and `test-native` (`cargo test`, reached via `test` → `dependsOn: ["test-native", ...]`) runs on Linux only — there is no Windows Rust job. I verified this by reverting the fix locally and confirming the tests still passed on macOS. They add real first-time coverage of worktree resolution and document the constraint, but they are not a regression guard for this platform. **A Windows canary would be valuable before merge** — several people on the issue offered to test a fix. ## Related Issue(s) Fixes #35637 <!-- polygraph-session-start --> --- <p><a href="https://app.trypolygraph.com/orgs/6a061dcb561c062131116eca/sessions/Fix-Windows-UNC-path-crash-in-Nx-Cloud-worktrees-nx35637-ac64aa6c">View Polygraph session ↗</a></p> <!-- polygraph-session-end -->
1 parent 05c8313 commit 385957e

1 file changed

Lines changed: 78 additions & 2 deletions

File tree

‎packages/nx/src/native/worktree.rs‎

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ fn resolve_main_worktree_root(workspace_root: &str) -> anyhow::Result<Option<Str
4040
};
4141

4242
// Resolve symlinks and ".." segments so the path is clean and
43-
// comparable across worktrees (e.g., in reset's equality check)
44-
let abs_path = abs_path.canonicalize().unwrap_or(abs_path);
43+
// comparable across worktrees (e.g., in reset's equality check).
44+
// dunce, not std: std returns a `\\?\` verbatim path on Windows, which
45+
// Node's module resolution walks past the drive root on (nx#35637).
46+
let abs_path = dunce::canonicalize(&abs_path).unwrap_or(abs_path);
4547

4648
// The common dir is the .git directory — its parent is the repo root
4749
let main_root = abs_path
@@ -50,3 +52,77 @@ fn resolve_main_worktree_root(workspace_root: &str) -> anyhow::Result<Option<Str
5052

5153
Ok(Some(main_root.to_string_lossy().to_string()))
5254
}
55+
56+
#[cfg(test)]
57+
mod tests {
58+
use super::*;
59+
use tempfile::TempDir;
60+
61+
fn git(dir: &Path, args: &[&str]) {
62+
let output = create_command("git")
63+
.args(args)
64+
.current_dir(dir)
65+
.output()
66+
.expect("failed to run git");
67+
assert!(
68+
output.status.success(),
69+
"git {args:?} failed: {}",
70+
String::from_utf8_lossy(&output.stderr)
71+
);
72+
}
73+
74+
fn init_repo(path: &Path) {
75+
std::fs::create_dir_all(path).unwrap();
76+
git(path, &["init", "-b", "main"]);
77+
git(
78+
path,
79+
&[
80+
"-c",
81+
82+
"-c",
83+
"user.name=Test",
84+
"commit",
85+
"--allow-empty",
86+
"-m",
87+
"init",
88+
],
89+
);
90+
}
91+
92+
#[test]
93+
fn returns_none_when_not_in_a_worktree() {
94+
let tmp = TempDir::new().unwrap();
95+
let main = tmp.path().join("main");
96+
init_repo(&main);
97+
98+
assert_eq!(
99+
resolve_main_worktree_root(main.to_str().unwrap()).unwrap(),
100+
None
101+
);
102+
}
103+
104+
#[test]
105+
fn resolves_main_repo_root_from_a_worktree() {
106+
let tmp = TempDir::new().unwrap();
107+
let main = tmp.path().join("main");
108+
init_repo(&main);
109+
110+
let worktree = tmp.path().join("wt");
111+
git(&main, &["worktree", "add", worktree.to_str().unwrap()]);
112+
113+
let resolved = resolve_main_worktree_root(worktree.to_str().unwrap())
114+
.unwrap()
115+
.expect("a linked worktree should resolve to the main repo root");
116+
117+
assert_eq!(
118+
PathBuf::from(&resolved),
119+
dunce::canonicalize(&main).unwrap()
120+
);
121+
// A `\\?\` prefix here reaches cacheDir, and Node's module resolution
122+
// walks past the drive root on such paths (nx#35637)
123+
assert!(
124+
!resolved.starts_with(r"\\?\"),
125+
"expected a plain path, got verbatim: {resolved}"
126+
);
127+
}
128+
}

0 commit comments

Comments
 (0)