From c8f6852ec4ed79fd18591fbdbcdd528bd1e1daac Mon Sep 17 00:00:00 2001 From: Jason Bedard Date: Sun, 18 Aug 2024 18:50:01 -0700 Subject: [PATCH] Revert "REVERT: add local copy of runfiles" This reverts commit de093399c96e998eda64cc0f00e3e53a5e60e971. --- .../npm_translate_lock_LTE4Nzc1MDcwNjU= | 9 +- .bazelignore | 1 - WORKSPACE | 1 - bazel_runfiles/BUILD.bazel | 10 - bazel_runfiles/README.md | 7 - bazel_runfiles/index.d.ts | 6 - bazel_runfiles/index.js | 9 - bazel_runfiles/package.json | 22 -- bazel_runfiles/paths.d.ts | 2 - bazel_runfiles/paths.js | 7 - bazel_runfiles/runfiles.d.ts | 47 ---- bazel_runfiles/runfiles.js | 224 ------------------ package.json | 3 +- pnpm-lock.yaml | 15 +- pnpm-workspace.yaml | 1 - 15 files changed, 13 insertions(+), 351 deletions(-) delete mode 100644 bazel_runfiles/BUILD.bazel delete mode 100644 bazel_runfiles/README.md delete mode 100644 bazel_runfiles/index.d.ts delete mode 100644 bazel_runfiles/index.js delete mode 100644 bazel_runfiles/package.json delete mode 100644 bazel_runfiles/paths.d.ts delete mode 100644 bazel_runfiles/paths.js delete mode 100644 bazel_runfiles/runfiles.d.ts delete mode 100644 bazel_runfiles/runfiles.js diff --git a/.aspect/rules/external_repository_action_cache/npm_translate_lock_LTE4Nzc1MDcwNjU= b/.aspect/rules/external_repository_action_cache/npm_translate_lock_LTE4Nzc1MDcwNjU= index bc6214f9f7..18e903c413 100755 --- a/.aspect/rules/external_repository_action_cache/npm_translate_lock_LTE4Nzc1MDcwNjU= +++ b/.aspect/rules/external_repository_action_cache/npm_translate_lock_LTE4Nzc1MDcwNjU= @@ -1,8 +1,7 @@ # @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 @@ -29,6 +28,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=-385441671 -pnpm-lock.yaml=-301996680 -pnpm-workspace.yaml=157507124 +package.json=-275319675 +pnpm-lock.yaml=715709952 +pnpm-workspace.yaml=-762451270 diff --git a/.bazelignore b/.bazelignore index bec3795e74..4627aa799f 100644 --- a/.bazelignore +++ b/.bazelignore @@ -1,4 +1,3 @@ -bazel_runfiles/node_modules e2e/ examples/js_binary/node_modules/ examples/linked_consumer/node_modules diff --git a/WORKSPACE b/WORKSPACE index e2e35fd056..d0d11dcd18 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -77,7 +77,6 @@ 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", diff --git a/bazel_runfiles/BUILD.bazel b/bazel_runfiles/BUILD.bazel deleted file mode 100644 index 60e75546bf..0000000000 --- a/bazel_runfiles/BUILD.bazel +++ /dev/null @@ -1,10 +0,0 @@ -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"], -) diff --git a/bazel_runfiles/README.md b/bazel_runfiles/README.md deleted file mode 100644 index 255c83e9f0..0000000000 --- a/bazel_runfiles/README.md +++ /dev/null @@ -1,7 +0,0 @@ -# @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). diff --git a/bazel_runfiles/index.d.ts b/bazel_runfiles/index.d.ts deleted file mode 100644 index 3a6693b199..0000000000 --- a/bazel_runfiles/index.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -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; diff --git a/bazel_runfiles/index.js b/bazel_runfiles/index.js deleted file mode 100644 index 5c23bc723f..0000000000 --- a/bazel_runfiles/index.js +++ /dev/null @@ -1,9 +0,0 @@ -"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); diff --git a/bazel_runfiles/package.json b/bazel_runfiles/package.json deleted file mode 100644 index 9d7022bc62..0000000000 --- a/bazel_runfiles/package.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "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": {} -} diff --git a/bazel_runfiles/paths.d.ts b/bazel_runfiles/paths.d.ts deleted file mode 100644 index 4a86f0022d..0000000000 --- a/bazel_runfiles/paths.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -export declare const BAZEL_OUT_REGEX: RegExp; -export declare const REPO_MAPPINGS = "_repo_mapping"; diff --git a/bazel_runfiles/paths.js b/bazel_runfiles/paths.js deleted file mode 100644 index d99ca98e38..0000000000 --- a/bazel_runfiles/paths.js +++ /dev/null @@ -1,7 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.REPO_MAPPINGS = 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\/)/; -exports.REPO_MAPPINGS = "_repo_mapping"; diff --git a/bazel_runfiles/runfiles.d.ts b/bazel_runfiles/runfiles.d.ts deleted file mode 100644 index 5cdf68ce0b..0000000000 --- a/bazel_runfiles/runfiles.d.ts +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Class that provides methods for resolving Bazel runfiles. - */ -export declare class Runfiles { - private _env; - manifest: Map | 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: Map | undefined; - constructor(_env: typeof process.env); - /** 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; - loadRepoMapping(runfilesDir: string): Map; - /** Resolves the given module path. */ - resolve(modulePath: 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; -} diff --git a/bazel_runfiles/runfiles.js b/bazel_runfiles/runfiles.js deleted file mode 100644 index b3f1e7407c..0000000000 --- a/bazel_runfiles/runfiles.js +++ /dev/null @@ -1,224 +0,0 @@ -"use strict"; -Object.defineProperty(exports, "__esModule", { value: true }); -exports.Runfiles = void 0; -const fs = require("fs"); -const path = require("path"); -const paths_1 = require("./paths"); -/** - * Class that provides methods for resolving Bazel runfiles. - */ -class Runfiles { - constructor(_env) { - this._env = _env; - // If Bazel sets a variable pointing to a runfiles manifest, - // we'll always use it. - // Note that this has a slight performance implication on Mac/Linux - // where we could use the runfiles tree already laid out on disk - // but this just costs one file read for the external npm/node_modules - // and one for each first-party module, not one per file. - if (!!_env['RUNFILES_MANIFEST_FILE']) { - this.manifest = this.loadRunfilesManifest(_env['RUNFILES_MANIFEST_FILE']); - } - else if (!!_env['RUNFILES_DIR']) { - this.runfilesDir = path.resolve(_env['RUNFILES_DIR']); - this.repoMappings = this.loadRepoMapping(this.runfilesDir); - } - else if (!!_env['RUNFILES']) { - this.runfilesDir = path.resolve(_env['RUNFILES']); - this.repoMappings = this.loadRepoMapping(this.runfilesDir); - } - else { - throw new Error('Every node program run under Bazel must have a $RUNFILES_DIR, $RUNFILES or $RUNFILES_MANIFEST_FILE environment variable'); - } - // Under --noenable_runfiles (in particular on Windows) - // Bazel sets RUNFILES_MANIFEST_ONLY=1. - // When this happens, we need to read the manifest file to locate - // inputs - if (_env['RUNFILES_MANIFEST_ONLY'] === '1' && !_env['RUNFILES_MANIFEST_FILE']) { - console.warn(`Workaround https://github.com/bazelbuild/bazel/issues/7994 - RUNFILES_MANIFEST_FILE should have been set but wasn't. - falling back to using runfiles symlinks. - If you want to test runfiles manifest behavior, add - --spawn_strategy=standalone to the command line.`); - } - // Bazel starts actions with pwd=execroot/my_wksp or pwd=runfiles/my_wksp - this.workspace = _env['BAZEL_WORKSPACE'] || _env['JS_BINARY__WORKSPACE'] || undefined; - // If target is from an external workspace such as @npm//rollup/bin:rollup - // resolvePackageRelative is not supported since package is in an external - // workspace. - let target = _env['BAZEL_TARGET'] || _env['JS_BINARY__TARGET']; - if (!!target && !target.startsWith('@')) { - // //path/to:target -> path/to - this.package = target.split(':')[0].replace(/^\/\//, ''); - } - } - /** Resolves the given path from the runfile manifest. */ - _resolveFromManifest(searchPath) { - if (!this.manifest) - return undefined; - let result; - for (const [k, v] of this.manifest) { - // Account for Bazel --legacy_external_runfiles - // which pollutes the workspace with 'my_wksp/external/...' - if (k.startsWith(`${searchPath}/external`)) - continue; - // If the manifest entry fully matches, return the value path without - // considering other manifest entries. We already have an exact match. - if (k === searchPath) { - return v; - } - // Consider a case where `npm/node_modules` is resolved, and we have the following - // manifest: `npm/node_modules/semver/LICENSE - // /path/to/external/npm/node_modules/semver/LICENSE` To resolve the directory, we look for - // entries that either fully match, or refer to contents within the directory we are looking - // for. We can then subtract the child path to resolve the directory. e.g. in the case above - // we subtract `length(`/semver/LICENSE`)` from the entry value. - if (k.startsWith(`${searchPath}/`)) { - const l = k.length - searchPath.length; - const maybe = v.substring(0, v.length - l); - if (maybe.match(paths_1.BAZEL_OUT_REGEX)) { - return maybe; - } - else { - result = maybe; - } - } - } - return result; - } - /** - * 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) { - const runfilesEntries = new Map(); - const input = fs.readFileSync(manifestPath, { encoding: 'utf-8' }); - for (const line of input.split('\n')) { - if (!line) - continue; - const [runfilesPath, realPath] = line.split(' '); - runfilesEntries.set(runfilesPath, realPath); - } - return runfilesEntries; - } - loadRepoMapping(runfilesDir) { - const repoMappingPath = path.join(runfilesDir, paths_1.REPO_MAPPINGS); - const repoMappings = new Map(); - if (fs.existsSync(repoMappingPath)) { - const mappings = fs.readFileSync(repoMappingPath, { encoding: 'utf-8' }); - for (const line of mappings.split('\n')) { - if (!line) - continue; - const [from, repoName, repoPath] = line.split(','); - // TODO: from !== ''? - if (from === '') { - repoMappings.set(repoName, repoPath); - } - } - } - return repoMappings; - } - /** Resolves the given module path. */ - resolve(modulePath) { - // Normalize path by converting to forward slashes and removing all trailing - // forward slashes - modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, ''); - if (path.isAbsolute(modulePath)) { - return modulePath; - } - const result = this._resolve(modulePath, undefined); - if (result) { - return result; - } - const e = new Error(`could not resolve module ${modulePath}`); - e.code = 'MODULE_NOT_FOUND'; - throw e; - } - /** Resolves the given path relative to the current Bazel workspace. */ - resolveWorkspaceRelative(modulePath) { - // Normalize path by converting to forward slashes and removing all trailing - // forward slashes - modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, ''); - if (!this.workspace) { - throw new Error('workspace could not be determined from the environment; make sure BAZEL_WORKSPACE is set'); - } - let workspace = this.workspace; - if (this.repoMappings && this.repoMappings.has(workspace)) { - workspace = this.repoMappings.get(workspace); - } - return this._resolve(workspace, modulePath); - } - /** Resolves the given path relative to the current Bazel package. */ - resolvePackageRelative(modulePath) { - // Normalize path by converting to forward slashes and removing all trailing - // forward slashes - modulePath = modulePath.replace(/\\/g, '/').replace(/\/+$/g, ''); - if (!this.workspace) { - throw new Error('workspace could not be determined from the environment; make sure BAZEL_WORKSPACE is set'); - } - // NB: this.package may be '' if at the root of the workspace - if (this.package === undefined) { - throw new Error('package could not be determined from the environment; make sure BAZEL_TARGET is set'); - } - let workspace = this.workspace; - if (this.repoMappings && this.repoMappings.has(workspace)) { - workspace = this.repoMappings.get(workspace); - } - return this._resolve(workspace, path.posix.join(this.package, modulePath)); - } - /** - * Patches the default Node.js resolution to support runfile resolution. - * @deprecated Use the runfile helpers directly instead. - **/ - patchRequire() { - const requirePatch = this._env['BAZEL_NODE_PATCH_REQUIRE']; - if (!requirePatch) { - throw new Error('require patch location could not be determined from the environment'); - } - require(requirePatch); - } - /** Helper for resolving a given module recursively in the runfiles. */ - _resolve(moduleBase, moduleTail) { - if (this.manifest) { - const result = this._resolveFromManifest(moduleBase); - if (result) { - if (moduleTail) { - const maybe = path.join(result, moduleTail || ''); - if (fs.existsSync(maybe)) { - return maybe; - } - } - else { - return result; - } - } - } - if (this.repoMappings && this.repoMappings.has(moduleBase)) { - const mappedRepo = this.repoMappings.get(moduleBase); - if (mappedRepo !== moduleBase) { - const maybe = this._resolve(mappedRepo, moduleTail); - if (maybe !== undefined) { - return maybe; - } - } - } - if (this.runfilesDir) { - const maybe = path.join(this.runfilesDir, moduleBase, moduleTail || ''); - if (fs.existsSync(maybe)) { - return maybe; - } - } - const dirname = path.dirname(moduleBase); - if (dirname == '.') { - // no match - return undefined; - } - return this._resolve(dirname, path.join(path.basename(moduleBase), moduleTail || '')); - } -} -exports.Runfiles = Runfiles; diff --git a/package.json b/package.json index ca2ab7b8eb..b0b5848cb6 100644 --- a/package.json +++ b/package.json @@ -41,8 +41,7 @@ "overrides": { "jsonify": "https://github.com/aspect-build/test-packages/releases/download/0.0.0/@foo-jsonify-0.0.0.tgz", "semver-max": "file:./npm/private/test/vendored/semver-max", - "is-odd": "file:./npm/private/test/vendored/is-odd", - "@bazel/runfiles": "workspace:*" + "is-odd": "file:./npm/private/test/vendored/is-odd" }, "patchedDependencies": { "meaning-of-life@1.0.0": "examples/npm_deps/patches/meaning-of-life@1.0.0-pnpm.patch" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4b780d72a9..d3d8e61c57 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,9 +8,8 @@ overrides: jsonify: https://github.com/aspect-build/test-packages/releases/download/0.0.0/@foo-jsonify-0.0.0.tgz semver-max: file:./npm/private/test/vendored/semver-max is-odd: file:./npm/private/test/vendored/is-odd - '@bazel/runfiles': workspace:* -packageExtensionsChecksum: 4368ccb508aca4e83892ade0753603ce +packageExtensionsChecksum: e978e3e510efb71c3833e1915611f5d9 patchedDependencies: meaning-of-life@1.0.0: @@ -37,8 +36,6 @@ importers: specifier: 4.9.5 version: 4.9.5 - bazel_runfiles: {} - examples/js_binary: devDependencies: '@mycorp/pkg-a': @@ -190,8 +187,8 @@ importers: examples/runfiles: dependencies: '@bazel/runfiles': - specifier: workspace:* - version: link:../../bazel_runfiles + specifier: ^5.8.1 + version: 5.8.1 examples/webpack_cli: dependencies: @@ -690,6 +687,10 @@ packages: to-fast-properties: 2.0.0 dev: true + /@bazel/runfiles@5.8.1: + resolution: {integrity: sha512-NDdfpdQ6rZlylgv++iMn5FkObC/QlBQvipinGLSOguTYpRywmieOyJ29XHvUilspwTFSILWpoE9CqMGkHXug1g==} + dev: false + /@bcoe/v8-coverage@0.2.3: resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==} dev: false @@ -7360,6 +7361,6 @@ packages: version: 2.10.0 requiresBuild: true dependencies: - '@types/node': registry.npmjs.org/@types/node@18.13.0 + '@types/node': 18.13.0 dev: true optional: true diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 3c7206e465..843e2c2f35 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,5 +1,4 @@ packages: - - 'bazel_runfiles' - 'examples/*' - 'examples/npm_package/libs/*' - 'examples/npm_package/packages/*'