@@ -338,6 +338,21 @@ async function loadResultFile(file: ResultFile): Promise<string> {
338338 return readFile ( file . source , 'utf-8' ) ;
339339}
340340
341+ function rebasePath ( filePath : string , fromRoot : string , toRoot : string ) : string {
342+ if ( path . isAbsolute ( filePath ) ) {
343+ const relativePath = path . relative ( fromRoot , filePath ) ;
344+ if (
345+ relativePath !== '..' &&
346+ ! relativePath . startsWith ( '..' + path . sep ) &&
347+ ! path . isAbsolute ( relativePath )
348+ ) {
349+ return path . join ( toRoot , relativePath ) ;
350+ }
351+ }
352+
353+ return filePath ;
354+ }
355+
341356export function createVitestPlugins ( pluginOptions : PluginOptions ) : Vite . Plugin [ ] {
342357 const { workspaceRoot, buildResultFiles, testFileToEntryPoint, setupFiles } = pluginOptions ;
343358 const isWindows = platform ( ) === 'win32' ;
@@ -348,20 +363,13 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
348363 ) ;
349364 let vitestConfig : ResolvedConfig | undefined ;
350365
351- const normalizeToWorkspace = ( filePath : string ) : string => {
352- if ( vitestConfig ?. root && path . isAbsolute ( filePath ) ) {
353- const relativeToVitestRoot = path . relative ( vitestConfig . root , filePath ) ;
354- if (
355- relativeToVitestRoot !== '..' &&
356- ! relativeToVitestRoot . startsWith ( '..' + path . sep ) &&
357- ! path . isAbsolute ( relativeToVitestRoot )
358- ) {
359- return path . join ( workspaceRoot , relativeToVitestRoot ) ;
360- }
361- }
366+ const normalizeToWorkspace = ( filePath : string ) : string =>
367+ vitestConfig ?. root ? rebasePath ( filePath , vitestConfig . root , workspaceRoot ) : filePath ;
362368
363- return filePath ;
364- } ;
369+ const normalizeToVitestRoot = ( filePath : string ) : string =>
370+ toPosixPath (
371+ vitestConfig ?. root ? rebasePath ( filePath , workspaceRoot , vitestConfig . root ) : filePath ,
372+ ) ;
365373
366374 /** The build output path served for a module, whether it is a test entry point or a built artifact. */
367375 const getOutputPath = ( id : string ) : string => {
@@ -424,7 +432,7 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
424432 resolveId : ( id : string , importer : string | undefined ) => {
425433 // Fast path for test entry points.
426434 if ( testFileToEntryPoint . has ( id ) ) {
427- return id ;
435+ return normalizeToVitestRoot ( id ) ;
428436 }
429437
430438 // Workaround for Vitest in Windows when a fully qualified absolute path is provided with
@@ -466,13 +474,13 @@ export function createVitestPlugins(pluginOptions: PluginOptions): Vite.Plugin[]
466474 fullPath = toPosixPath ( fullPath ) ;
467475
468476 if ( testFileToEntryPoint . has ( fullPath ) ) {
469- return fullPath ;
477+ return normalizeToVitestRoot ( fullPath ) ;
470478 }
471479
472480 // Check if the resolved path corresponds to a known build artifact.
473481 const relativePath = path . relative ( workspaceRoot , fullPath ) ;
474482 if ( buildResultFiles . has ( toPosixPath ( relativePath ) ) ) {
475- return fullPath ;
483+ return normalizeToVitestRoot ( fullPath ) ;
476484 }
477485
478486 // If the module cannot be resolved from the build artifacts, let other plugins handle it.
0 commit comments