1+ import { normalize } from 'node:path' ;
12import type { ConfigParams } from '@rsbuild/core' ;
23import {
34 type RstestConfig ,
45 type RstestConfigExport ,
56 mergeRstestConfig ,
67} from '@rstest/core' ;
8+ import { type LoadProjectConfig , PROJECT_LOADER } from '@rstest/core/api' ;
79import { loadRstackConfig , type RstackConfigDefinitions } from './config.ts' ;
810import { resolveConfigLayers } from './configLayers.ts' ;
911
1012const resolveAutomaticExtends = async (
1113 layers : readonly RstackConfigDefinitions [ ] ,
1214 params : ConfigParams ,
15+ cwd ?: string ,
1316) : Promise < RstestConfig [ 'extends' ] | undefined > => {
1417 // Prefer the app when both app and lib are defined. Merging both adapters can
1518 // introduce conflicting runtime, resolve, and source transform settings.
@@ -34,6 +37,10 @@ const resolveAutomaticExtends = async (
3437
3538 return withRslibConfig ( {
3639 config : await resolveRslibConfig ( layers , params ) ,
40+ // The adapter reads the project's tsconfig for decorator detection. It
41+ // falls back to `process.cwd()`, so external projects need their own
42+ // directory here or they would inherit the root tsconfig.
43+ cwd,
3744 } ) ;
3845 }
3946
@@ -58,13 +65,14 @@ const extendsConfig = async (
5865 layers : readonly RstackConfigDefinitions [ ] ,
5966 testConfig : RstestConfig ,
6067 params : ConfigParams ,
68+ cwd ?: string ,
6169) => {
6270 if ( 'extends' in testConfig ) {
6371 return testConfig ;
6472 }
6573
6674 if ( testConfig . projects === undefined ) {
67- const automaticExtends = await resolveAutomaticExtends ( layers , params ) ;
75+ const automaticExtends = await resolveAutomaticExtends ( layers , params , cwd ) ;
6876 return injectExtends ( testConfig , automaticExtends ) ;
6977 }
7078
@@ -75,7 +83,7 @@ const extendsConfig = async (
7583 return testConfig ;
7684 }
7785
78- const automaticExtends = await resolveAutomaticExtends ( layers , params ) ;
86+ const automaticExtends = await resolveAutomaticExtends ( layers , params , cwd ) ;
7987
8088 return {
8189 ...testConfig ,
@@ -99,14 +107,45 @@ export const mergeRstestConfigLayers = async (
99107export const resolveRstestConfig = async (
100108 layers : readonly RstackConfigDefinitions [ ] ,
101109 params : ConfigParams ,
110+ cwd ?: string ,
102111) : Promise < RstestConfig > => {
103112 const testConfig = await mergeRstestConfigLayers ( layers ) ;
104- return extendsConfig ( layers , testConfig , params ) ;
113+ return extendsConfig ( layers , testConfig , params , cwd ) ;
105114} ;
106115
116+ // Rstest reads the host loader only from the root config and calls it for every
117+ // directory or file a string `projects` entry expands to. Rstack does not load
118+ // native tool configs anywhere else, so a project without an Rstack config
119+ // becomes an empty project instead of falling back to `rstest.config.*`.
120+ const createProjectLoader =
121+ ( params : ConfigParams ) : LoadProjectConfig =>
122+ async ( { dirPath, filePath } ) => {
123+ // `null` searches the project directory with the default config file names
124+ // instead of reusing the `rs --config` path that loads the root config.
125+ const loaded = await loadRstackConfig ( {
126+ cwd : dirPath ,
127+ configFilePath : filePath ?? null ,
128+ } ) ;
129+ if ( loaded . filePath === null ) {
130+ return null ;
131+ }
132+
133+ return {
134+ content : await resolveRstestConfig ( [ loaded . configs ] , params , dirPath ) ,
135+ // Rstest dedupes projects by this string. Directory and file entries can
136+ // reach the same file with different path separators, so normalize.
137+ filePath : normalize ( loaded . filePath ) ,
138+ dependencies : loaded . dependencies ,
139+ } ;
140+ } ;
141+
107142const loadRstestConfig = ( async ( params : ConfigParams ) => {
108143 const { configs } = await loadRstackConfig ( ) ;
109- return resolveRstestConfig ( [ configs ] , params ) ;
144+ const config = await resolveRstestConfig ( [ configs ] , params ) ;
145+ return {
146+ ...config ,
147+ [ PROJECT_LOADER ] : createProjectLoader ( params ) ,
148+ } ;
110149} ) as RstestConfigExport ;
111150
112151export default loadRstestConfig ;
0 commit comments