Skip to content

Commit 5fee67b

Browse files
committed
Improved fix
1 parent 63f4123 commit 5fee67b

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

packages/runfiles/runfiles.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,6 @@ export class Runfiles {
155155
(repoMappings[sourceRepo] ??= Object.create(null))[targetRepoApparentName] = targetRepoDirectory;
156156
}
157157

158-
// Ensure empty source repository mapping exists
159-
if (!repoMappings[""]) {
160-
repoMappings[""] = Object.create(null);
161-
repoMappings[""]["__main__"] = "__main__";
162-
}
163-
164158
return repoMappings;
165159
}
166160

@@ -183,13 +177,31 @@ export class Runfiles {
183177

184178
// If the repository mappings were loaded ensure the source repository is valid.
185179
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+
}
193205
}
194206
}
195207

@@ -264,7 +276,7 @@ export class Runfiles {
264276
}
265277

266278
// 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]) {
268280
const mappedRepo = this.repoMappings[sourceRepo][moduleBase];
269281
if (mappedRepo !== moduleBase) {
270282
const maybe = this._resolve(sourceRepo, mappedRepo, moduleTail);
@@ -278,6 +290,17 @@ export class Runfiles {
278290
if (fs.existsSync(maybe)) {
279291
return maybe;
280292
}
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+
}
281304
}
282305
const dirname = path.dirname(moduleBase);
283306
if (dirname == '.') {

0 commit comments

Comments
 (0)