-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
850 additions
and
21 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
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,4 @@ | ||
bazel_runfiles/node_modules | ||
e2e/ | ||
examples/js_binary/node_modules/ | ||
examples/linked_consumer/node_modules | ||
|
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
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,10 @@ | ||
load("@aspect_rules_js//npm:defs.bzl", "npm_package") | ||
load("@npm//:defs.bzl", "npm_link_all_packages") | ||
|
||
npm_link_all_packages(name = "node_modules") | ||
|
||
npm_package( | ||
name = "pkg", | ||
srcs = glob(["*.js", "*.d.ts", "*.json"]), | ||
visibility = ["//visibility:public"], | ||
) |
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,7 @@ | ||
# @bazel/runfiles | ||
|
||
This package provides a basic set of utilities for resolving runfiles within Node.js scripts | ||
executed through `js_binary` or `js_test`. | ||
|
||
Runfile resolution is desirable if your workspace intends to support users that cannot rely | ||
on runfile forest symlinking (most commonly affected are Windows machines). |
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,6 @@ | ||
import { BAZEL_OUT_REGEX } from './paths'; | ||
import { Runfiles } from './runfiles'; | ||
export { Runfiles }; | ||
export { BAZEL_OUT_REGEX as _BAZEL_OUT_REGEX }; | ||
/** Instance of the runfile helpers. */ | ||
export declare const runfiles: Runfiles; |
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,9 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.runfiles = exports._BAZEL_OUT_REGEX = exports.Runfiles = void 0; | ||
const paths_1 = require("./paths"); | ||
Object.defineProperty(exports, "_BAZEL_OUT_REGEX", { enumerable: true, get: function () { return paths_1.BAZEL_OUT_REGEX; } }); | ||
const runfiles_1 = require("./runfiles"); | ||
Object.defineProperty(exports, "Runfiles", { enumerable: true, get: function () { return runfiles_1.Runfiles; } }); | ||
/** Instance of the runfile helpers. */ | ||
exports.runfiles = new runfiles_1.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "@bazel/runfiles", | ||
"description": "Node.js runfiles helpers for Bazel", | ||
"license": "Apache-2.0", | ||
"version": "0.0.0-PLACEHOLDER", | ||
"repository": { | ||
"type" : "git", | ||
"url" : "https://github.com/bazelbuild/rules_nodejs.git", | ||
"directory": "packages/runfiles" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/bazelbuild/rules_nodejs/issues" | ||
}, | ||
"keywords": [ | ||
"bazel", | ||
"runfiles", | ||
"runfiles helpers" | ||
], | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"dependencies": {} | ||
} |
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,2 @@ | ||
export declare const BAZEL_OUT_REGEX: RegExp; | ||
export declare 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,9 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.REPO_MAPPING_RLOCATION = exports.BAZEL_OUT_REGEX = void 0; | ||
// 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 | ||
exports.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 | ||
exports.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,13 @@ | ||
/** | ||
* 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 { | ||
[sourceRepo: string]: { | ||
[targetRepoApparentName: string]: string; | ||
}; | ||
} | ||
export declare function currentRepository(): string; | ||
export declare function callerRepository(): string; | ||
export declare function callerRepositoryFromStack(skip: number): string; |
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,35 @@ | ||
"use strict"; | ||
/** | ||
* Utils for managing repository mappings. | ||
* | ||
* The majority of this code is ported from [rules_go](https://github.com/bazelbuild/rules_go/pull/3347). | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.callerRepositoryFromStack = exports.callerRepository = exports.currentRepository = void 0; | ||
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. | ||
function currentRepository() { | ||
return callerRepositoryFromStack(1); | ||
} | ||
exports.currentRepository = currentRepository; | ||
// CallerRepository returns the canonical name of the Bazel repository that | ||
// contains the source file of the caller of the function that itself calls | ||
// CallerRepository. | ||
function callerRepository() { | ||
return callerRepositoryFromStack(2); | ||
} | ||
exports.callerRepository = callerRepository; | ||
function callerRepositoryFromStack(skip) { | ||
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]; | ||
} | ||
exports.callerRepositoryFromStack = callerRepositoryFromStack; |
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,49 @@ | ||
/// <reference types="node" /> | ||
import { RepoMappings } from './repository'; | ||
/** | ||
* Class that provides methods for resolving Bazel runfiles. | ||
*/ | ||
export declare class Runfiles { | ||
private _env; | ||
manifest: Map<string, string> | undefined; | ||
runfilesDir: string | undefined; | ||
/** | ||
* If the environment gives us enough hints, we can know the workspace name | ||
*/ | ||
workspace: string | undefined; | ||
/** | ||
* If the environment gives us enough hints, we can know the package path | ||
*/ | ||
package: string | undefined; | ||
/** | ||
* If the environment has repo mappings, we can use them to resolve repo relative paths. | ||
*/ | ||
repoMappings: RepoMappings | undefined; | ||
constructor(_env?: NodeJS.ProcessEnv); | ||
/** Resolves the given path from the runfile manifest. */ | ||
private _resolveFromManifest; | ||
/** | ||
* The runfiles manifest maps from short_path | ||
* https://docs.bazel.build/versions/main/skylark/lib/File.html#short_path | ||
* to the actual location on disk where the file can be read. | ||
* | ||
* In a sandboxed execution, it does not exist. In that case, runfiles must be | ||
* resolved from a symlink tree under the runfiles dir. | ||
* See https://github.com/bazelbuild/bazel/issues/3726 | ||
*/ | ||
loadRunfilesManifest(manifestPath: string): Map<any, any>; | ||
parseRepoMapping(runfilesDir: string): RepoMappings | undefined; | ||
/** Resolves the given module path. */ | ||
resolve(modulePath: string, sourceRepo?: string): string; | ||
/** Resolves the given path relative to the current Bazel workspace. */ | ||
resolveWorkspaceRelative(modulePath: string): string; | ||
/** Resolves the given path relative to the current Bazel package. */ | ||
resolvePackageRelative(modulePath: string): string; | ||
/** | ||
* Patches the default Node.js resolution to support runfile resolution. | ||
* @deprecated Use the runfile helpers directly instead. | ||
**/ | ||
patchRequire(): void; | ||
/** Helper for resolving a given module recursively in the runfiles. */ | ||
private _resolve; | ||
} |
Oops, something went wrong.