Skip to content

Commit

Permalink
feat: add support for Label in nodejs_binary#entry_point (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko authored and gregmagolan committed Jun 2, 2019
1 parent b59e4cf commit ed6a68c
Show file tree
Hide file tree
Showing 30 changed files with 190 additions and 87 deletions.
19 changes: 4 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,8 @@ For example, the `protractor` package has two bin entries in its `package.json`:
These will result in two generated `nodejs_binary` targets in the `@npm//protractor/bin`
package (if your npm deps workspace is `@npm`):

```python
nodejs_binary(
name = "protractor",
entry_point = "protractor/bin/protractor",
data = ["//protractor"],
)

nodejs_binary(
name = "webdriver-manager",
entry_point = "protractor/bin/webdriver-manager",
data = ["//protractor"],
)
```
* `@npm//protractor/bin:protractor`
* `@npm//protractor/bin:webdriver-manager`

These targets can be used as executables for actions in custom rules or can
be run by Bazel directly. For example, you can run protractor with the
Expand Down Expand Up @@ -452,7 +441,7 @@ load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")

nodejs_binary(
name = "rollup",
entry_point = "rollup/bin/rollup",
entry_point = "//:node_modules/rollup/bin/rollup",
)
```

Expand Down Expand Up @@ -480,7 +469,7 @@ nodejs_binary(
"@//:node_modules",
"main.js",
],
entry_point = "workspace_name/main.js",
entry_point = ":main.js",
args = ["--node_options=--expose-gc"],
)
```
Expand Down
10 changes: 10 additions & 0 deletions e2e/jasmine/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ load("@npm_bazel_jasmine//:index.bzl", "jasmine_node_test")
jasmine_node_test(
name = "test",
srcs = ["test.spec.js"],
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"@npm//@bazel/jasmine",
],
)

jasmine_node_test(
name = "shared_env_test",
srcs = ["jasmine_shared_env_test.spec.js"],
bootstrap = ["e2e_jasmine/jasmine_shared_env_bootstrap.js"],
data = ["jasmine_shared_env_bootstrap.js"],
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"@npm//@bazel/jasmine",
"@npm//jasmine",
"@npm//jasmine-core",
"@npm//zone.js",
Expand All @@ -24,4 +30,8 @@ jasmine_node_test(
"coverage_source.js",
],
coverage = True,
jasmine = "@npm_bazel_jasmine//:index.js",
deps = [
"@npm//@bazel/jasmine",
],
)
2 changes: 1 addition & 1 deletion e2e/ts_library/googmodule/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ nodejs_binary(
"@npm//@bazel/typescript",
"@npm//tsickle",
],
entry_point = "@bazel/typescript/internal/tsc_wrapped/tsc_wrapped.js",
entry_point = "@npm//node_modules/@bazel/typescript:internal/tsc_wrapped/tsc_wrapped.js",
install_source_map_support = False,
)

Expand Down
2 changes: 1 addition & 1 deletion e2e/ts_library/some_module/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ nodejs_binary(
":main",
":some_module",
],
entry_point = "e2e_ts_library/some_module/main.js",
entry_point = ":main.js",
)

sh_test(
Expand Down
2 changes: 1 addition & 1 deletion examples/program/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ nodejs_binary(
"index.js",
":node_modules",
],
entry_point = "examples_program/index.js",
entry_point = ":index.js",
)

jasmine_node_test(
Expand Down
2 changes: 1 addition & 1 deletion internal/e2e/fine_grained_no_bin/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ nodejs_binary(
"test/test.js",
"@fine_grained_no_bin//fs.realpath",
],
entry_point = "build_bazel_rules_nodejs/internal/e2e/fine_grained_no_bin/index.js",
entry_point = ":index.js",
)
2 changes: 1 addition & 1 deletion internal/e2e/rollup_fine_grained_deps/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ nodejs_binary(
"@npm//jasmine",
"@npm//unidiff",
],
entry_point = "build_bazel_rules_nodejs/internal/e2e/rollup_fine_grained_deps/update_golden.js",
entry_point = ":update_golden.js",
install_source_map_support = False,
)
2 changes: 1 addition & 1 deletion internal/history-server/history_server.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def history_server(templated_args = [], **kwargs):

nodejs_binary_macro(
node_modules = "@history-server_runtime_deps//:node_modules",
entry_point = "history-server/modules/cli.js",
entry_point = "@history-server_runtime_deps//node_modules/history-server:modules/cli.js",
install_source_map_support = False,
templated_args = templated_args,
**kwargs
Expand Down
2 changes: 1 addition & 1 deletion internal/http-server/http_server.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def http_server(templated_args = [], **kwargs):

nodejs_binary_macro(
node_modules = "@http-server_runtime_deps//:node_modules",
entry_point = "http-server/bin/http-server",
entry_point = "@http-server_runtime_deps//node_modules/http-server:bin/http-server",
install_source_map_support = False,
templated_args = templated_args,
**kwargs
Expand Down
5 changes: 3 additions & 2 deletions internal/jasmine_node_test/jasmine_node_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ def jasmine_node_test(
tags = tags,
)

jasmine_runner_label = Label("//internal/jasmine_node_test:jasmine_runner.js")

all_data = data + srcs + deps
all_data += [Label("//internal/jasmine_node_test:jasmine_runner.js")]
all_data += [":%s_devmode_srcs.MF" % name]
entry_point = "build_bazel_rules_nodejs/internal/jasmine_node_test/jasmine_runner.js"
entry_point = jasmine_runner_label.relative(":jasmine_runner.js")

nodejs_test_macro(
name = name,
Expand Down
2 changes: 1 addition & 1 deletion internal/jasmine_node_test/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ nodejs_test(
"no_jasmine_test.js",
"//internal/jasmine_node_test:jasmine_runner.js",
],
entry_point = "build_bazel_rules_nodejs/internal/jasmine_node_test/test/no_jasmine_test.js",
entry_point = ":no_jasmine_test.js",
)
41 changes: 35 additions & 6 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ a `module_name` attribute can be `require`d by that name.
"""

load("@build_bazel_rules_nodejs//internal/common:node_module_info.bzl", "NodeModuleSources", "collect_node_modules_aspect")
load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfiles")
load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfiles", "expand_path_into_runfiles")
load("//internal/common:module_mappings.bzl", "module_mappings_runtime_aspect")
load("//internal/common:sources_aspect.bzl", "sources_aspect")

Expand Down Expand Up @@ -86,6 +86,9 @@ def _write_loader_script(ctx):

node_modules_root = _compute_node_modules_root(ctx)

if len(ctx.attr.entry_point.files) != 1:
fail("labels in entry_point must contain exactly one file")

ctx.actions.expand_template(
template = ctx.file._loader_template,
output = ctx.outputs.loader,
Expand All @@ -94,7 +97,7 @@ def _write_loader_script(ctx):
"TEMPLATED_bootstrap": "\n " + ",\n ".join(
["\"" + d + "\"" for d in ctx.attr.bootstrap],
),
"TEMPLATED_entry_point": ctx.attr.entry_point,
"TEMPLATED_entry_point": expand_path_into_runfiles(ctx, ctx.file.entry_point.short_path),
"TEMPLATED_gen_dir": ctx.genfiles_dir.path,
"TEMPLATED_install_source_map_support": str(ctx.attr.install_source_map_support).lower(),
"TEMPLATED_module_roots": "\n " + ",\n ".join(module_mappings),
Expand Down Expand Up @@ -169,7 +172,7 @@ def _nodejs_binary_impl(ctx):
is_executable = True,
)

runfiles = depset([node, ctx.outputs.loader, ctx.file._repository_args] + ctx.files._node_runfiles, transitive = [sources, node_modules])
runfiles = depset([node, ctx.outputs.loader, ctx.file._repository_args, ctx.file.entry_point] + ctx.files._node_runfiles, transitive = [sources])

return [DefaultInfo(
executable = ctx.outputs.script,
Expand Down Expand Up @@ -209,12 +212,38 @@ _NODEJS_EXECUTABLE_ATTRS = {
allow_files = True,
aspects = [sources_aspect, module_mappings_runtime_aspect, collect_node_modules_aspect],
),
"entry_point": attr.string(
"entry_point": attr.label(
doc = """The script which should be executed first, usually containing a main function.
This attribute expects a string starting with the workspace name, so that it's not ambiguous
in cases where a script with the same name appears in another directory or external workspace.
The `entry_point` accepts a target's name as an entry point.
If the target is a rule, it should produce the JavaScript entry file that will be passed to the nodejs_binary rule).
For example:
```
filegroup(
name = "entry_file",
srcs = ["workspace/path/to/entry/file"]
)
nodejs_binary(
name = "my_binary",
...
entry_point = ":entry_file",
)
```
If the entry JavaScript file belongs to the same package (as the BUILD file),
you can simply reference it by its relative name to the package directory:
```
nodejs_binary(
name = "my_binary",
...
entry_point = ":file.js",
)
```
""",
mandatory = True,
allow_single_file = True,
),
"install_source_map_support": attr.bool(
doc = """Install the source-map-support package.
Expand Down
28 changes: 19 additions & 9 deletions internal/node/node_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,26 @@ var BOOTSTRAP = [TEMPLATED_bootstrap];
const USER_WORKSPACE_NAME = 'TEMPLATED_user_workspace_name';
const NODE_MODULES_ROOT = 'TEMPLATED_node_modules_root';
const BIN_DIR = 'TEMPLATED_bin_dir';
const ENTRY_POINT = 'TEMPLATED_entry_point';
const GEN_DIR = 'TEMPLATED_gen_dir';
const INSTALL_SOURCE_MAP_SUPPORT = TEMPLATED_install_source_map_support;
const TARGET = 'TEMPLATED_target';

if (DEBUG)
console.error(`
node_loader: running TEMPLATED_target with
MODULE_ROOTS: ${JSON.stringify(MODULE_ROOTS, undefined, 2)}
BOOTSTRAP: ${JSON.stringify(BOOTSTRAP, undefined, 2)}
NODE_MODULES_ROOT: ${NODE_MODULES_ROOT}
node_loader: running ${TARGET} with
cwd: ${process.cwd()}
runfiles: ${process.env.RUNFILES}
BIN_DIR: ${BIN_DIR}
BOOTSTRAP: ${JSON.stringify(BOOTSTRAP, undefined, 2)}
ENTRY_POINT: ${ENTRY_POINT}
GEN_DIR: ${GEN_DIR}
INSTALL_SOURCE_MAP_SUPPORT: ${INSTALL_SOURCE_MAP_SUPPORT}
MODULE_ROOTS: ${JSON.stringify(MODULE_ROOTS, undefined, 2)}
NODE_MODULES_ROOT: ${NODE_MODULES_ROOT}
TARGET: ${TARGET}
USER_WORKSPACE_NAME: ${USER_WORKSPACE_NAME}
`);

function resolveToModuleRoot(path) {
Expand Down Expand Up @@ -444,15 +454,15 @@ module.constructor._resolveFilename = function(request, parent, isMain, options)
}

const error = new Error(
`TEMPLATED_target cannot find module '${request}' required by '${parentFilename}'\n looked in:\n` +
`${TARGET} cannot find module '${request}' required by '${parentFilename}'\n looked in:\n` +
failedResolutions.map(r => ` ${r}`).join('\n') + '\n');
error.code = 'MODULE_NOT_FOUND';
throw error;
}

// Before loading anything that might print a stack, install the
// source-map-support.
if (TEMPLATED_install_source_map_support) {
if (INSTALL_SOURCE_MAP_SUPPORT) {
try {
const sourcemap_support_package = path.resolve(process.cwd(),
'../build_bazel_rules_nodejs/third_party/github.com/source-map-support');
Expand All @@ -461,7 +471,7 @@ if (TEMPLATED_install_source_map_support) {
if (DEBUG) {
console.error(`WARNING: source-map-support module not installed.
Stack traces from languages like TypeScript will point to generated .js files.
Set install_source_map_support = False in TEMPLATED_target to turn off this warning.
Set install_source_map_support = False in ${TARGET} to turn off this warning.
`);
}
}
Expand All @@ -480,7 +490,7 @@ if (require.main === module) {
// Set the actual entry point in the arguments list.
// argv[0] == node, argv[1] == entry point.
// NB: entry_point below is replaced during the build process.
var mainScript = process.argv[1] = 'TEMPLATED_entry_point';
var mainScript = process.argv[1] = ENTRY_POINT;
try {
module.constructor._load(mainScript, this, /*isMain=*/true);
} catch (e) {
Expand All @@ -492,7 +502,7 @@ if (require.main === module) {
// (which is an empty filegroup).
// See https://github.com/bazelbuild/rules_nodejs/wiki#migrating-to-rules_nodejs-013
console.error(
`\nWARNING: Due to a breaking change in rules_nodejs 0.13.0, target TEMPLATED_target\n` +
`\nWARNING: Due to a breaking change in rules_nodejs 0.13.0, target ${TARGET}\n` +
`must now declare either an explicit node_modules attribute, or\n` +
`list explicit deps[] or data[] fine grained dependencies on npm labels\n` +
`if it has any node_modules dependencies.\n` +
Expand Down
18 changes: 14 additions & 4 deletions internal/node/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ load("//:defs.bzl", "nodejs_binary")
nodejs_binary(
name = "no_deps",
data = ["no-deps.js"],
entry_point = "build_bazel_rules_nodejs/internal/node/test/no-deps",
entry_point = ":no-deps.js",
)

# You can have a nodejs_binary with a node_modules attribute
# and no fine grained deps
nodejs_binary(
name = "has_deps_legacy",
data = ["has-deps.js"],
entry_point = "build_bazel_rules_nodejs/internal/node/test/has-deps",
entry_point = ":has-deps.js",
node_modules = "@fine_grained_deps_yarn//:node_modules",
)

Expand All @@ -25,7 +25,7 @@ nodejs_binary(
"has-deps.js",
"@fine_grained_deps_yarn//typescript",
],
entry_point = "build_bazel_rules_nodejs/internal/node/test/has-deps",
entry_point = ":has-deps.js",
)

# You can have a nodejs_binary with both a node_modules attribute
Expand All @@ -36,6 +36,16 @@ nodejs_binary(
"has-deps.js",
"@fine_grained_deps_yarn//typescript",
],
entry_point = "build_bazel_rules_nodejs/internal/node/test/has-deps",
entry_point = ":has-deps.js",
node_modules = "@fine_grained_deps_yarn//:node_modules",
)

filegroup(
name = "entry_file",
srcs = ["no-deps.js"],
)

nodejs_binary(
name = "has_entry_file",
entry_point = ":entry_file",
)
2 changes: 1 addition & 1 deletion internal/npm_install/generate_build_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ npm_umd_bundle(
result += `# Wire up the \`bin\` entry \`${name}\`
nodejs_binary(
name = "${name}__bin",
entry_point = "${pkg._dir}/${path}",
entry_point = ":${path}",
install_source_map_support = False,
data = [":${pkg._name}__pkg"],${additionalAttributes}
)
Expand Down
2 changes: 1 addition & 1 deletion internal/npm_install/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ nodejs_binary(
"@npm//jasmine",
"@npm//unidiff",
],
entry_point = "build_bazel_rules_nodejs/internal/npm_install/test/update_golden.js",
entry_point = ":update_golden.js",
install_source_map_support = False,
)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ed6a68c

Please sign in to comment.