Skip to content

Commit eeb322c

Browse files
authored
feat: lazy initialize runfiles (#3839)
Close #3837
1 parent d0d2a31 commit eeb322c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

packages/runfiles/runfiles.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export class Runfiles {
2323
*/
2424
repoMappings: RepoMappings|undefined;
2525

26+
private _runfilesResolutionError = false;
27+
2628
constructor(private _env = process.env) {
2729
// If Bazel sets a variable pointing to a runfiles manifest,
2830
// we'll always use it.
@@ -39,8 +41,7 @@ export class Runfiles {
3941
this.runfilesDir = path.resolve(_env['RUNFILES']!);
4042
this.repoMappings = this.parseRepoMapping(this.runfilesDir);
4143
} else {
42-
throw new Error(
43-
'Every node program run under Bazel must have a $RUNFILES_DIR, $RUNFILES or $RUNFILES_MANIFEST_FILE environment variable');
44+
this._runfilesResolutionError = true;
4445
}
4546
// Under --noenable_runfiles (in particular on Windows)
4647
// Bazel sets RUNFILES_MANIFEST_ONLY=1.
@@ -65,6 +66,13 @@ export class Runfiles {
6566
}
6667
}
6768

69+
private _assertRunfilesResolved() {
70+
if (this._runfilesResolutionError) {
71+
throw new Error(
72+
'Every node program run under Bazel must have a $RUNFILES_DIR, $RUNFILES or $RUNFILES_MANIFEST_FILE environment variable');
73+
}
74+
}
75+
6876
/** Resolves the given path from the runfile manifest. */
6977
private _resolveFromManifest(searchPath: string): string|undefined {
7078
if (!this.manifest) return undefined;
@@ -100,7 +108,6 @@ export class Runfiles {
100108
return result;
101109
}
102110

103-
104111
/**
105112
* The runfiles manifest maps from short_path
106113
* https://docs.bazel.build/versions/main/skylark/lib/File.html#short_path
@@ -152,6 +159,8 @@ export class Runfiles {
152159

153160
/** Resolves the given module path. */
154161
resolve(modulePath: string, sourceRepo?: string): string {
162+
this._assertRunfilesResolved();
163+
155164
// Normalize path by converting to forward slashes and removing all trailing
156165
// forward slashes
157166
modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, '')

0 commit comments

Comments
 (0)