Skip to content

Commit fb5dca4

Browse files
committed
fix(@angular/build): resolve test files correctly on Windows when using non-C drives
Ensures that test files requested via root-relative paths (common on Windows with non-C drives or specific Vitest configurations) are correctly resolved to their absolute path entry points. This fix prevents 'Cannot find module' errors by explicitly checking the test entry point map after normalizing the path to an absolute POSIX path.
1 parent 59e3ccc commit fb5dca4

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

packages/angular/build/src/builders/unit-test/runners/vitest/plugins.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ export function createVitestPlugins(pluginOptions: PluginOptions): VitestPlugins
229229
// Construct the full, absolute path and normalize it to POSIX format.
230230
const fullPath = toPosixPath(path.join(baseDir, id));
231231

232+
if (testFileToEntryPoint.has(fullPath)) {
233+
return fullPath;
234+
}
235+
232236
// Check if the resolved path corresponds to a known build artifact.
233237
const relativePath = path.relative(workspaceRoot, fullPath);
234238
if (buildResultFiles.has(toPosixPath(relativePath))) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import assert from 'node:assert/strict';
2+
import { exec, silentExec } from '../../utils/process';
3+
import { applyVitestBuilder } from '../../utils/vitest';
4+
5+
export default async function (): Promise<void> {
6+
// This test uses `subst` to map the project directory to a virtual drive letter
7+
// to simulate running tests from a non-C drive on Windows.
8+
if (process.platform !== 'win32') {
9+
return;
10+
}
11+
12+
await applyVitestBuilder();
13+
14+
const originalCwd = process.cwd();
15+
const driveLetter = 'X:'; // Pick a drive letter that is unlikely to be in use.
16+
17+
try {
18+
// 1. Map the current project directory to the virtual drive.
19+
// The current CWD is the test project root.
20+
await silentExec('subst', driveLetter, originalCwd);
21+
22+
// 2. Change the current process's working directory to the virtual drive root.
23+
// This ensures that all subsequent `ng` commands (which use process.cwd() by default)
24+
// operate within the context of the new drive letter.
25+
process.chdir(driveLetter + '\\');
26+
27+
// 3. Run `ng test`.
28+
const { stdout } = await exec('ng', 'test', '--watch=false');
29+
30+
assert.match(stdout, /passed/, 'Expected tests to pass when running from a subst drive.');
31+
} finally {
32+
// Teardown: Restore CWD and remove the virtual drive mapping.
33+
try {
34+
process.chdir(originalCwd);
35+
} catch (e) {
36+
console.error('Failed to restore CWD:', e);
37+
}
38+
39+
try {
40+
await silentExec('subst', driveLetter, '/d');
41+
} catch (e) {
42+
// Ignore errors if the drive wasn't mounted or if unmount fails (best effort)
43+
console.error(`Failed to unmount ${driveLetter}:`, e);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)