@@ -30,15 +30,21 @@ import { serializeValue } from '@vitest/utils/serialize'
3030import { parseErrorStacktrace , parseStacktrace } from '@vitest/utils/source-map'
3131import { extractSourcemapFromFile } from '@vitest/utils/source-map/node'
3232import mime from 'mime/lite'
33- import { basename , extname , resolve } from 'pathe'
33+ import { basename , extname , relative , resolve } from 'pathe'
34+ import { withLabel } from './reporters/renderers/utils'
3435
3536export class TestRun {
3637 private externalSourceMaps = new Map < string , unknown > ( )
38+ // the first project that attached each file in this run
39+ private attachmentProjects = new Map < string , string > ( )
40+ private warnedProjectPairs = new Set < string > ( )
3741
3842 constructor ( private vitest : Vitest ) { }
3943
4044 async start ( specifications : TestSpecification [ ] ) : Promise < void > {
4145 this . externalSourceMaps . clear ( )
46+ this . attachmentProjects . clear ( )
47+ this . warnedProjectPairs . clear ( )
4248 const filepaths = specifications . map ( ( spec ) => spec . moduleId )
4349 this . vitest . state . collectPaths ( filepaths )
4450
@@ -345,9 +351,10 @@ export class TestRun {
345351 if ( path && ! path . startsWith ( 'http://' ) && ! path . startsWith ( 'https://' ) ) {
346352 const currentPath = resolve ( project . config . root , path )
347353 const hash = createHash ( 'sha1' ) . update ( currentPath ) . digest ( 'hex' )
354+ const suffix = this . getProjectSuffix ( project , currentPath )
348355 const newPath = resolve (
349356 project . config . attachmentsDir ,
350- `${ filename ? `${ sanitizeFilePath ( filename ) } -` : '' } ${ hash } ${ extname ( currentPath ) } ` ,
357+ `${ filename ? `${ sanitizeFilePath ( filename ) } -` : '' } ${ hash } ${ suffix } ${ extname ( currentPath ) } ` ,
351358 )
352359 if ( ! existsSync ( project . config . attachmentsDir ) ) {
353360 await mkdir ( project . config . attachmentsDir , { recursive : true } )
@@ -361,6 +368,31 @@ export class TestRun {
361368 return attachment
362369 }
363370
371+ // the copies of a file that other projects attach get the project name,
372+ // so each project keeps its own copy
373+ private getProjectSuffix ( project : TestProject , path : string ) : string {
374+ const firstProject = this . attachmentProjects . get ( path )
375+ if ( firstProject === undefined ) {
376+ this . attachmentProjects . set ( path , project . name )
377+ return ''
378+ }
379+ if ( firstProject === project . name ) {
380+ return ''
381+ }
382+ const pair = `${ firstProject } \0${ project . name } `
383+ if ( ! this . warnedProjectPairs . has ( pair ) ) {
384+ this . warnedProjectPairs . add ( pair )
385+ this . vitest . logger . warn (
386+ withLabel (
387+ 'yellow' ,
388+ 'Vitest' ,
389+ `The projects "${ firstProject } " and "${ project . name } " attach the same file "${ relative ( this . vitest . config . root , path ) } ", so they can overwrite each other's file. Save the file to a different path in each project.` ,
390+ ) ,
391+ )
392+ }
393+ return `-${ sanitizeFilePath ( project . name ) } `
394+ }
395+
364396 private async reportChildren ( children : TestCollection ) {
365397 for ( const child of children ) {
366398 if ( child . type === 'test' ) {
0 commit comments