-
Notifications
You must be signed in to change notification settings - Fork 523
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(runfiles): support bzlmod repo mappings
- Loading branch information
Showing
3 changed files
with
164 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
// NB: on windows thanks to legacy 8-character path segments it might be like | ||
// c:/b/ojvxx6nx/execroot/build_~1/bazel-~1/x64_wi~1/bin/internal/npm_in~1/test | ||
export const BAZEL_OUT_REGEX = /(\/bazel-out\/|\/bazel-~1\/x64_wi~1\/)/; | ||
|
||
// The runfiles root symlink under which the repository mapping can be found. | ||
// https://cs.opensource.google/bazel/bazel/+/1b073ac0a719a09c9b2d1a52680517ab22dc971e:src/main/java/com/google/devtools/build/lib/analysis/Runfiles.java;l=424 | ||
export const REPO_MAPPING_RLOCATION = "_repo_mapping"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Utils for managing repository mappings. | ||
* | ||
* The majority of this code is ported from [rules_go](https://github.com/bazelbuild/rules_go/pull/3347). | ||
*/ | ||
|
||
export interface RepoMappings { | ||
[repo: string]: { | ||
[mapped: string]: string; | ||
}; | ||
} | ||
|
||
const legacyExternalGeneratedFile = | ||
/^bazel-out[\/][^\/]+\/bin\/external\/([^\/]+)\//; | ||
const legacyExternalFile = /^external\/([^\/]+)\//; | ||
|
||
// CurrentRepository returns the canonical name of the Bazel repository that | ||
// contains the source file of the caller of CurrentRepository. | ||
// export function currentRepository(): string { | ||
// return callerRepositoryFromStack(1) | ||
// } | ||
|
||
// CallerRepository returns the canonical name of the Bazel repository that | ||
// contains the source file of the caller of the function that itself calls | ||
// CallerRepository. | ||
export function callerRepository(): string { | ||
return callerRepositoryFromStack(2); | ||
} | ||
|
||
function callerRepositoryFromStack(skip: number): string { | ||
const file = new Error().stack.split("\n")[skip + 1]; | ||
const match = | ||
file.match(legacyExternalGeneratedFile) || file.match(legacyExternalFile); | ||
|
||
if (match) { | ||
return match[0]; | ||
} | ||
|
||
// If a file is not in an external repository, it is in the main repository, | ||
// which has the empty string as its canonical name. | ||
return ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters