Skip to content

Commit

Permalink
REVERT: add local copy of runfiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Sep 17, 2024
1 parent 1d2f8c0 commit 91fdb64
Show file tree
Hide file tree
Showing 31 changed files with 850 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# @generated
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "@//:pnpm-lock.yaml").
# Input hashes for repository rule npm_translate_lock(name = "npm", pnpm_lock = "@@//:pnpm-lock.yaml").
# This file should be checked into version control along with the pnpm-lock.yaml file.
.npmrc=-2065072158
bazel_runfiles/package.json=616841026
examples/js_binary/package.json=-41174383
examples/linked_consumer/package.json=-1109186228
examples/linked_empty_node_modules/package.json=-1039372825
Expand All @@ -28,6 +29,6 @@ npm/private/test/package.json=600650131
npm/private/test/vendored/is-odd/package.json=1041695223
npm/private/test/vendored/lodash-4.17.21.tgz=-1206623349
npm/private/test/vendored/semver-max/package.json=578664053
package.json=-275319675
pnpm-lock.yaml=715709952
pnpm-workspace.yaml=-762451270
package.json=-385441671
pnpm-lock.yaml=-301996680
pnpm-workspace.yaml=157507124
1 change: 1 addition & 0 deletions .bazelignore
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
Expand Down
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ npm_translate_lock(
"//:examples/npm_deps/patches/meaning-of-life@1.0.0-pnpm.patch",
"//:package.json",
"//:pnpm-workspace.yaml",
"//bazel_runfiles:package.json",
"//examples/js_binary:package.json",
"//examples/linked_consumer:package.json",
"//examples/linked_empty_node_modules:package.json",
Expand Down
10 changes: 10 additions & 0 deletions bazel_runfiles/BUILD.bazel
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"],
)
7 changes: 7 additions & 0 deletions bazel_runfiles/README.md
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).
6 changes: 6 additions & 0 deletions bazel_runfiles/index.d.ts
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;
9 changes: 9 additions & 0 deletions bazel_runfiles/index.js
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);
22 changes: 22 additions & 0 deletions bazel_runfiles/package.json
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": {}
}
2 changes: 2 additions & 0 deletions bazel_runfiles/paths.d.ts
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";
9 changes: 9 additions & 0 deletions bazel_runfiles/paths.js
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';
13 changes: 13 additions & 0 deletions bazel_runfiles/repository.d.ts
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;
35 changes: 35 additions & 0 deletions bazel_runfiles/repository.js
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;
49 changes: 49 additions & 0 deletions bazel_runfiles/runfiles.d.ts
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;
}
Loading

0 comments on commit 91fdb64

Please sign in to comment.