Skip to content

Commit e383505

Browse files
authored
Add install_tools option to yarn_modules rule (#60)
1 parent cbff612 commit e383505

File tree

6 files changed

+78
-2
lines changed

6 files changed

+78
-2
lines changed

node/internal/yarn_modules.bzl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,17 @@ def _yarn_modules_impl(ctx):
5555
execute(ctx, ["cp", parse_yarn_lock_js, "internal/parse_yarn_lock.js"])
5656
execute(ctx, ["cp", yarn_js, "yarn.js"])
5757

58+
install_path = [node.dirname]
59+
for tool in ctx.attr.install_tools:
60+
tool_path = ctx.which(tool)
61+
if not tool_path:
62+
fail("Required install tool '%s' is not in the PATH" % tool, "install_tools")
63+
install_path.append(tool_path.dirname)
64+
install_path.append("$PATH")
65+
5866
# Build node_modules via 'yarn install'
5967
execute(ctx, [node, yarn_js, "install"], quiet = True, environment = {
60-
# postinstall scripts may need to find the node binary
61-
"PATH": "%s:$PATH" % ctx.path(node).dirname,
68+
"PATH": ":".join(install_path),
6269
})
6370

6471
# Run the script and save the stdout to our BUILD file(s)
@@ -93,6 +100,9 @@ yarn_modules = repository_rule(
93100
default = Label("@yarn//:bin/yarn.js"),
94101
single_file = True,
95102
),
103+
# If specififed, augment the PATH environment variable with these
104+
# tools during 'yarn install'.
105+
"install_tools": attr.string_list(),
96106
"package_json": attr.label(
97107
mandatory = False,
98108
allow_files = FileType(["package.json"]),

tests/node-ref/BUILD

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load("@org_pubref_rules_node//node:rules.bzl", "node_binary")
4+
5+
node_binary(
6+
name = "example",
7+
main = "example.js",
8+
deps = [
9+
"@yarn_modules//:_all_",
10+
],
11+
)
12+
13+
sh_test(
14+
name = "example_test",
15+
size = "small",
16+
srcs = ["example_test.sh"],
17+
data = [":example"],
18+
)
19+

tests/node-ref/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Ref Example
2+
3+
This example demonstrates the `install_tools` option.
4+
5+
In this case, `yarn install` of the `ref` package triggers `node-gyp`,
6+
which in this case uses tools in `/bin` and `/usr/bin`. We normally
7+
take these for granted as being on the path but bazel runs the yarn
8+
install script in the bare environment, so we need to specify these
9+
explicitly to build up the appropriate `PATH`.

tests/node-ref/WORKSPACE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
local_repository(
2+
name = "org_pubref_rules_node",
3+
path = "../..",
4+
)
5+
6+
load("@org_pubref_rules_node//node:rules.bzl", "node_repositories", "yarn_modules")
7+
8+
node_repositories()
9+
10+
yarn_modules(
11+
name = "yarn_modules",
12+
deps = {
13+
"ref": "1.3.5",
14+
},
15+
# yarn install of 'ref' triggers node-gyp. This will in turn
16+
# require 'sh' and 'dirname'. Explicitly make these tools
17+
# available during the yarn install step.
18+
install_tools = [
19+
"sh", # /bin
20+
"dirname", # #/usr/bin
21+
],
22+
)

tests/node-ref/example.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const ref = require("ref");
2+
3+
const buf = new Buffer(4)
4+
5+
buf.writeInt32LE(12345, 0)
6+
buf.type = ref.types.int
7+
8+
console.log(buf.deref()) // ← 12345
9+

tests/node-ref/example_test.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set -e
2+
3+
if (./example &) | grep -q '12345'; then
4+
echo "PASS"
5+
else
6+
exit 1
7+
fi

0 commit comments

Comments
 (0)