Skip to content

Commit 67d327f

Browse files
committed
fix(@angular/build): resolve Vitest artifacts when workspace root is aliased
In Vitest 5.1.0, `vitestConfig.root` inherits `viteConfig.root`, which resolves symlinks and Windows `subst` drives via `realpathSync`. Normalize paths under `vitestConfig.root` back to `workspaceRoot` in the in-memory provider plugin so internal setup files and build artifacts resolve consistently.
1 parent 359d74c commit 67d327f

2 files changed

Lines changed: 114 additions & 6 deletions

File tree

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -346,20 +346,41 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
346346
toPosixPath(path.isAbsolute(file) ? file : path.join(workspaceRoot, file)),
347347
),
348348
);
349-
let vitestConfig: ResolvedConfig;
349+
let vitestConfig: ResolvedConfig | undefined;
350+
351+
const normalizeToWorkspace = (filePath: string): string => {
352+
if (vitestConfig?.root && path.isAbsolute(filePath)) {
353+
const relativeToVitestRoot = path.relative(vitestConfig.root, filePath);
354+
if (!relativeToVitestRoot.startsWith('..') && !path.isAbsolute(relativeToVitestRoot)) {
355+
return path.join(workspaceRoot, relativeToVitestRoot);
356+
}
357+
}
358+
359+
return filePath;
360+
};
350361

351362
/** The build output path served for a module, whether it is a test entry point or a built artifact. */
352363
const getOutputPath = (id: string): string => {
353-
const entryPoint = testFileToEntryPoint.get(id);
364+
const normalizedId = toPosixPath(normalizeToWorkspace(id));
365+
const entryPoint = testFileToEntryPoint.get(normalizedId);
354366

355-
return entryPoint ? entryPoint + '.js' : toPosixPath(path.relative(workspaceRoot, id));
367+
return entryPoint
368+
? entryPoint + '.js'
369+
: toPosixPath(path.relative(workspaceRoot, normalizedId));
356370
};
357371

358372
// Setup files must not be wrapped in a virtual import stub because Vitest only invalidates
359373
// the setup file itself between test files; wrapping it would cause the underlying bundle
360374
// to be cached, preventing per-test hooks from running on subsequent test files.
361-
const isCoverageRedirect = (id: string): boolean =>
362-
!!vitestConfig?.coverage?.enabled && testFileToEntryPoint.has(id) && !setupFileSet.has(id);
375+
const isCoverageRedirect = (id: string): boolean => {
376+
const normalizedId = toPosixPath(normalizeToWorkspace(id));
377+
378+
return (
379+
!!vitestConfig?.coverage?.enabled &&
380+
testFileToEntryPoint.has(normalizedId) &&
381+
!setupFileSet.has(normalizedId)
382+
);
383+
};
363384

364385
return [
365386
{
@@ -413,9 +434,12 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
413434
}
414435
}
415436

437+
id = normalizeToWorkspace(id);
438+
416439
// Determine the base directory for resolution.
417440
let baseDir: string;
418441
if (importer) {
442+
importer = toPosixPath(normalizeToWorkspace(importer));
419443
// If the importer is a test entry point, resolve relative to the workspace root.
420444
// Otherwise, resolve relative to the importer's directory.
421445
baseDir = testFileToEntryPoint.has(importer) ? workspaceRoot : path.dirname(importer);
@@ -473,7 +497,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
473497

474498
const map = sourceMapText ? JSON.parse(sourceMapText) : undefined;
475499
if (map) {
476-
adjustSourcemapSources(map, true, workspaceRoot, id);
500+
adjustSourcemapSources(map, true, workspaceRoot, toPosixPath(normalizeToWorkspace(id)));
477501
}
478502

479503
return {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import path from 'node:path';
10+
import type { VitestPluginContext } from 'vitest/node';
11+
import { BuildOutputFileType } from '../../../../tools/esbuild/bundler-files';
12+
import { toPosixPath } from '../../../../utils/path';
13+
import type { ResultFile } from '../../../application/results';
14+
import { createVitestPlugins } from './plugins';
15+
16+
describe('createVitestPlugins', () => {
17+
it('resolves and loads setup files when vitestConfig.root differs from workspaceRoot (e.g., symlink or subst drive)', async () => {
18+
const workspaceRoot = path.resolve('/logical-workspace/test-project');
19+
const physicalRoot = toPosixPath(path.resolve('/physical-disk/tmp/test-project'));
20+
const specFile = toPosixPath(path.join(workspaceRoot, 'src/app/app.spec.ts'));
21+
22+
const buildResultFiles = new Map<string, ResultFile>([
23+
[
24+
'init-compiler.js',
25+
{
26+
origin: 'memory',
27+
path: 'init-compiler.js',
28+
type: BuildOutputFileType.Root,
29+
contents: new TextEncoder().encode('console.log("init-compiler");'),
30+
hash: 'hash-init-compiler',
31+
},
32+
],
33+
[
34+
'spec-src-app-app-spec.js',
35+
{
36+
origin: 'memory',
37+
path: 'spec-src-app-app-spec.js',
38+
type: BuildOutputFileType.Root,
39+
contents: new TextEncoder().encode('console.log("spec");'),
40+
hash: 'hash-spec',
41+
},
42+
],
43+
]);
44+
45+
const testFileToEntryPoint = new Map<string, string>([[specFile, 'spec-src-app-app-spec']]);
46+
47+
const plugins = createVitestPlugins({
48+
workspaceRoot,
49+
projectSourceRoot: path.join(workspaceRoot, 'src'),
50+
projectName: 'test-project',
51+
buildResultFiles,
52+
testFileToEntryPoint,
53+
setupFiles: ['init-compiler.js'],
54+
});
55+
56+
const inMemoryPlugin = plugins.find((p) => p.name === 'angular:test-in-memory-provider');
57+
expect(inMemoryPlugin).toBeDefined();
58+
59+
const configureVitest = inMemoryPlugin?.configureVitest as
60+
((context: VitestPluginContext) => void) | undefined;
61+
configureVitest?.({
62+
vitest: {
63+
config: {
64+
root: physicalRoot,
65+
},
66+
},
67+
} as unknown as VitestPluginContext);
68+
69+
const resolveId = inMemoryPlugin?.resolveId as (
70+
id: string,
71+
importer: string | undefined,
72+
) => string | undefined;
73+
const load = inMemoryPlugin?.load as (
74+
id: string,
75+
) => Promise<{ code: string; map?: unknown } | undefined>;
76+
77+
const expectedSetupId = toPosixPath(path.join(workspaceRoot, 'init-compiler.js'));
78+
const resolvedSetupId = resolveId(`${physicalRoot}/init-compiler.js`, undefined);
79+
expect(resolvedSetupId).toBe(expectedSetupId);
80+
81+
const loadedSetup = await load(expectedSetupId);
82+
expect(loadedSetup?.code).toBe('console.log("init-compiler");');
83+
});
84+
});

0 commit comments

Comments
 (0)