diff --git a/packages/runfiles/runfiles.ts b/packages/runfiles/runfiles.ts index 1d3a98bcd6..ea236042e3 100644 --- a/packages/runfiles/runfiles.ts +++ b/packages/runfiles/runfiles.ts @@ -154,6 +154,7 @@ export class Runfiles { (repoMappings[sourceRepo] ??= Object.create(null))[targetRepoApparentName] = targetRepoDirectory; } + return repoMappings; } @@ -176,13 +177,31 @@ export class Runfiles { // If the repository mappings were loaded ensure the source repository is valid. if (!(sourceRepo in this.repoMappings)) { - throw new Error( - `source repository "${sourceRepo}" not found in repo mappings: ${JSON.stringify( - this.repoMappings, - null, - 2, - )}`, - ); + // Try common main repository names as fallback + const mainRepoAliases = ['__main__', '_main']; + for (const alias of mainRepoAliases) { + if (alias in this.repoMappings) { + sourceRepo = alias; + break; + } + } + + // If no fallback worked, create a synthetic mapping for the main repository + if (!(sourceRepo in this.repoMappings)) { + // In non-bzlmod mode, create a synthetic mapping from empty repo to main repo + if (sourceRepo === '') { + this.repoMappings[''] = {}; + sourceRepo = ''; + } else { + throw new Error( + `source repository "${sourceRepo}" not found in repo mappings: ${JSON.stringify( + this.repoMappings, + null, + 2, + )}`, + ); + } + } } } @@ -257,7 +276,7 @@ export class Runfiles { } // Apply repo mappings to the moduleBase if it is a known repo. - if (this.repoMappings && moduleBase in this.repoMappings[sourceRepo]) { + if (this.repoMappings && this.repoMappings[sourceRepo] && moduleBase in this.repoMappings[sourceRepo]) { const mappedRepo = this.repoMappings[sourceRepo][moduleBase]; if (mappedRepo !== moduleBase) { const maybe = this._resolve(sourceRepo, mappedRepo, moduleTail); @@ -271,6 +290,17 @@ export class Runfiles { if (fs.existsSync(maybe)) { return maybe; } + + // If not found and we have repo mappings, try under main repository aliases + if (this.repoMappings && !this.repoMappings[sourceRepo]) { + const mainRepoAliases = ['__main__', '_main']; + for (const alias of mainRepoAliases) { + const fallbackPath = path.join(this.runfilesDir, alias, moduleBase, moduleTail || ''); + if (fs.existsSync(fallbackPath)) { + return fallbackPath; + } + } + } } const dirname = path.dirname(moduleBase); if (dirname == '.') {