Skip to content

Commit 2c883d1

Browse files
devversionalexeagle
authored andcommitted
feat: introduce package for runfile helpers
Introduces a new package called `@bazel/runfiles` that provides runfile helpers for Bazel with NodeJS. The benefit of providing the helpers as a separate package is that people can explicitly specify a dependency on the helpers and also can get typings. The current best practice of using a require w/ an environment variable is untyped and prone to mistakes (and doesn't fit well into the Node/NPM ecosystem).
1 parent 49b5e08 commit 2c883d1

31 files changed

+630
-352
lines changed

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pkg_npm(
8989
"//internal/pkg_npm:package_contents",
9090
"//internal/pkg_web:package_contents",
9191
"//internal/providers:package_contents",
92+
"//internal/runfiles:package_contents",
9293
"//third_party/github.com/bazelbuild/bazel:package_contents",
9394
"//third_party/github.com/bazelbuild/bazel-skylib:package_contents",
9495
"//third_party/github.com/bazelbuild/bazel/tools/bash/runfiles:package_contents",

commitlint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
'labs',
1818
'protractor',
1919
'rollup',
20+
'runfiles',
2021
'terser',
2122
'typescript',
2223
'worker',

examples/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ example_integration_test(
6565

6666
example_integration_test(
6767
name = "examples_create-react-app",
68+
npm_packages = {
69+
"//packages/runfiles:npm_package": "@bazel/runfiles",
70+
},
6871
)
6972

7073
example_integration_test(
@@ -92,6 +95,9 @@ example_integration_test(
9295

9396
example_integration_test(
9497
name = "examples_closure",
98+
npm_packages = {
99+
"//packages/runfiles:npm_package": "@bazel/runfiles",
100+
},
95101
)
96102

97103
example_integration_test(

examples/closure/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ google_closure_compiler(
1616

1717
nodejs_test(
1818
name = "test",
19-
data = ["bundle.js"],
19+
data = [
20+
"bundle.js",
21+
"@npm//@bazel/runfiles",
22+
],
2023
entry_point = "test.js",
2124
)

examples/closure/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"private": true,
3+
"//comment": "TODO: Change runfiles dependency to an actual version once released.",
34
"dependencies": {
5+
"@bazel/runfiles": "latest",
46
"google-closure-compiler": "20190729.0.0"
57
}
68
}

examples/closure/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
1+
const {runfiles} = require('@bazel/runfiles');
22

33
const closureOutput = runfiles.resolve('examples_closure/bundle.js');
44

examples/create-react-app/BUILD.bazel

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ react_scripts(
5959

6060
nodejs_test(
6161
name = "build_smoke_test",
62-
data = ["build"],
62+
data = [
63+
"build",
64+
"@npm//@bazel/runfiles",
65+
],
6366
entry_point = "build_smoke_test.js",
6467
)
6568

examples/create-react-app/build_smoke_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const assert = require('assert');
22
const fs = require('fs');
3-
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
3+
const {runfiles} = require('@bazel/runfiles');
44

55
// Make sure there's a file like build/static/js/main.12345678.chunk.js
66
const jsDir = runfiles.resolvePackageRelative('build/static/js');

examples/create-react-app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
"react-scripts": "3.4.1",
1717
"typescript": "~3.7.2"
1818
},
19+
"//comment": "TODO: Change runfiles dependency to an actual version once released.",
1920
"devDependencies": {
2021
"@bazel/ibazel": "^0.15.6",
22+
"@bazel/runfiles": "latest",
2123
"patch-package": "^6.2.2"
2224
},
2325
"scripts": {

internal/js_library/js_library.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ _ATTRS = {
3838
),
3939
"deps": attr.label_list(),
4040
"external_npm_package": attr.bool(
41-
doc = """Internal use only. Indictates that this js_library target is one or more external npm packages in node_modules.
41+
doc = """Internal use only. Indicates that this js_library target is one or more external npm packages in node_modules.
4242
This is used by the yarn_install & npm_install repository rules for npm dependencies installed by
4343
yarn & npm. When true, js_library will provide ExternalNpmPackageInfo.
44-
44+
4545
It can also be used for user-managed npm dependencies if node_modules is layed out outside of bazel.
4646
For example,
4747

0 commit comments

Comments
 (0)