Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions packages/runfiles/runfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ export class Runfiles {

(repoMappings[sourceRepo] ??= Object.create(null))[targetRepoApparentName] = targetRepoDirectory;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the fix just defaulting one of the fields here? See #3797 (comment)

}

return repoMappings;
}

Expand All @@ -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,
)}`,
);
}
}
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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 == '.') {
Expand Down