@@ -155,12 +155,6 @@ export class Runfiles {
155
155
( repoMappings [ sourceRepo ] ??= Object . create ( null ) ) [ targetRepoApparentName ] = targetRepoDirectory ;
156
156
}
157
157
158
- // Ensure empty source repository mapping exists
159
- if ( ! repoMappings [ "" ] ) {
160
- repoMappings [ "" ] = Object . create ( null ) ;
161
- repoMappings [ "" ] [ "__main__" ] = "__main__" ;
162
- }
163
-
164
158
return repoMappings ;
165
159
}
166
160
@@ -183,13 +177,31 @@ export class Runfiles {
183
177
184
178
// If the repository mappings were loaded ensure the source repository is valid.
185
179
if ( ! ( sourceRepo in this . repoMappings ) ) {
186
- throw new Error (
187
- `source repository "${ sourceRepo } " not found in repo mappings: ${ JSON . stringify (
188
- this . repoMappings ,
189
- null ,
190
- 2 ,
191
- ) } `,
192
- ) ;
180
+ // Try common main repository names as fallback
181
+ const mainRepoAliases = [ '__main__' , '_main' ] ;
182
+ for ( const alias of mainRepoAliases ) {
183
+ if ( alias in this . repoMappings ) {
184
+ sourceRepo = alias ;
185
+ break ;
186
+ }
187
+ }
188
+
189
+ // If no fallback worked, create a synthetic mapping for the main repository
190
+ if ( ! ( sourceRepo in this . repoMappings ) ) {
191
+ // In non-bzlmod mode, create a synthetic mapping from empty repo to main repo
192
+ if ( sourceRepo === '' ) {
193
+ this . repoMappings [ '' ] = { } ;
194
+ sourceRepo = '' ;
195
+ } else {
196
+ throw new Error (
197
+ `source repository "${ sourceRepo } " not found in repo mappings: ${ JSON . stringify (
198
+ this . repoMappings ,
199
+ null ,
200
+ 2 ,
201
+ ) } `,
202
+ ) ;
203
+ }
204
+ }
193
205
}
194
206
}
195
207
@@ -264,7 +276,7 @@ export class Runfiles {
264
276
}
265
277
266
278
// Apply repo mappings to the moduleBase if it is a known repo.
267
- if ( this . repoMappings && moduleBase in this . repoMappings [ sourceRepo ] ) {
279
+ if ( this . repoMappings && this . repoMappings [ sourceRepo ] && moduleBase in this . repoMappings [ sourceRepo ] ) {
268
280
const mappedRepo = this . repoMappings [ sourceRepo ] [ moduleBase ] ;
269
281
if ( mappedRepo !== moduleBase ) {
270
282
const maybe = this . _resolve ( sourceRepo , mappedRepo , moduleTail ) ;
@@ -278,6 +290,17 @@ export class Runfiles {
278
290
if ( fs . existsSync ( maybe ) ) {
279
291
return maybe ;
280
292
}
293
+
294
+ // If not found and we have repo mappings, try under main repository aliases
295
+ if ( this . repoMappings && ! this . repoMappings [ sourceRepo ] ) {
296
+ const mainRepoAliases = [ '__main__' , '_main' ] ;
297
+ for ( const alias of mainRepoAliases ) {
298
+ const fallbackPath = path . join ( this . runfilesDir , alias , moduleBase , moduleTail || '' ) ;
299
+ if ( fs . existsSync ( fallbackPath ) ) {
300
+ return fallbackPath ;
301
+ }
302
+ }
303
+ }
281
304
}
282
305
const dirname = path . dirname ( moduleBase ) ;
283
306
if ( dirname == '.' ) {
0 commit comments