Skip to content
Merged
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
15 changes: 12 additions & 3 deletions packages/runfiles/runfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export class Runfiles {
*/
repoMappings: RepoMappings|undefined;

private _runfilesResolutionError = false;

constructor(private _env = process.env) {
// If Bazel sets a variable pointing to a runfiles manifest,
// we'll always use it.
Expand All @@ -39,8 +41,7 @@ export class Runfiles {
this.runfilesDir = path.resolve(_env['RUNFILES']!);
this.repoMappings = this.parseRepoMapping(this.runfilesDir);
} else {
throw new Error(
'Every node program run under Bazel must have a $RUNFILES_DIR, $RUNFILES or $RUNFILES_MANIFEST_FILE environment variable');
this._runfilesResolutionError = true;
}
// Under --noenable_runfiles (in particular on Windows)
// Bazel sets RUNFILES_MANIFEST_ONLY=1.
Expand All @@ -65,6 +66,13 @@ export class Runfiles {
}
}

private _assertRunfilesResolved() {
if (this._runfilesResolutionError) {
throw new Error(
'Every node program run under Bazel must have a $RUNFILES_DIR, $RUNFILES or $RUNFILES_MANIFEST_FILE environment variable');
}
}

/** Resolves the given path from the runfile manifest. */
private _resolveFromManifest(searchPath: string): string|undefined {
if (!this.manifest) return undefined;
Expand Down Expand Up @@ -100,7 +108,6 @@ export class Runfiles {
return result;
}


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

/** Resolves the given module path. */
resolve(modulePath: string, sourceRepo?: string): string {
this._assertRunfilesResolved();

// Normalize path by converting to forward slashes and removing all trailing
// forward slashes
modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, '')
Expand Down