-
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
4 changed files
with
126 additions
and
10 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,12 +1,8 @@ | ||
import {BAZEL_OUT_REGEX} from './paths'; | ||
import {Runfiles} from './runfiles'; | ||
|
||
// Re-export the `Runfiles` class. This class if the runfile helpers need to be | ||
// mocked for testing purposes. This is used by the linker but also publicly exposed. | ||
export {Runfiles}; | ||
// Re-export a RegExp for matching `bazel-out` paths. This is used by the linker | ||
// but not intended for public use. | ||
export {BAZEL_OUT_REGEX as _BAZEL_OUT_REGEX}; | ||
|
||
/** Instance of the runfile helpers. */ | ||
export const runfiles = new Runfiles(process.env); |
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,43 @@ | ||
/** | ||
* 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 = | ||
/\/_main\/bazel-out\/[^/]+\/bin\/external\/([^/]+)\//; | ||
const legacyExternalFile = /\/_main\/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); | ||
} | ||
|
||
export function callerRepositoryFromStack(skip: number): string { | ||
const stack = new Error().stack.split("\n"); | ||
const file = stack[skip + 2]; // 0 is the Error(msg), 1 is this method, 2 is the caller | ||
const match = | ||
file.match(legacyExternalGeneratedFile) || file.match(legacyExternalFile); | ||
|
||
// If a file is not in an external repository, it is in the main repository, | ||
// which has the empty string as its canonical name. | ||
if (!match || match[0] == "_main") { | ||
return ""; | ||
} | ||
|
||
return match[0]; | ||
} |
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