From e383505f42b32a87b3a055ac84f903e68f956b33 Mon Sep 17 00:00:00 2001 From: Paul Cody Johnston Date: Sun, 11 Feb 2018 20:17:32 -0700 Subject: [PATCH] Add `install_tools` option to yarn_modules rule (#60) --- node/internal/yarn_modules.bzl | 14 ++++++++++++-- tests/node-ref/BUILD | 19 +++++++++++++++++++ tests/node-ref/README.md | 9 +++++++++ tests/node-ref/WORKSPACE | 22 ++++++++++++++++++++++ tests/node-ref/example.js | 9 +++++++++ tests/node-ref/example_test.sh | 7 +++++++ 6 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tests/node-ref/BUILD create mode 100644 tests/node-ref/README.md create mode 100644 tests/node-ref/WORKSPACE create mode 100644 tests/node-ref/example.js create mode 100755 tests/node-ref/example_test.sh diff --git a/node/internal/yarn_modules.bzl b/node/internal/yarn_modules.bzl index 0d460fe..0b5b2af 100644 --- a/node/internal/yarn_modules.bzl +++ b/node/internal/yarn_modules.bzl @@ -55,10 +55,17 @@ def _yarn_modules_impl(ctx): execute(ctx, ["cp", parse_yarn_lock_js, "internal/parse_yarn_lock.js"]) execute(ctx, ["cp", yarn_js, "yarn.js"]) + install_path = [node.dirname] + for tool in ctx.attr.install_tools: + tool_path = ctx.which(tool) + if not tool_path: + fail("Required install tool '%s' is not in the PATH" % tool, "install_tools") + install_path.append(tool_path.dirname) + install_path.append("$PATH") + # Build node_modules via 'yarn install' execute(ctx, [node, yarn_js, "install"], quiet = True, environment = { - # postinstall scripts may need to find the node binary - "PATH": "%s:$PATH" % ctx.path(node).dirname, + "PATH": ":".join(install_path), }) # Run the script and save the stdout to our BUILD file(s) @@ -93,6 +100,9 @@ yarn_modules = repository_rule( default = Label("@yarn//:bin/yarn.js"), single_file = True, ), + # If specififed, augment the PATH environment variable with these + # tools during 'yarn install'. + "install_tools": attr.string_list(), "package_json": attr.label( mandatory = False, allow_files = FileType(["package.json"]), diff --git a/tests/node-ref/BUILD b/tests/node-ref/BUILD new file mode 100644 index 0000000..65189ec --- /dev/null +++ b/tests/node-ref/BUILD @@ -0,0 +1,19 @@ +package(default_visibility = ["//visibility:public"]) + +load("@org_pubref_rules_node//node:rules.bzl", "node_binary") + +node_binary( + name = "example", + main = "example.js", + deps = [ + "@yarn_modules//:_all_", + ], +) + +sh_test( + name = "example_test", + size = "small", + srcs = ["example_test.sh"], + data = [":example"], +) + diff --git a/tests/node-ref/README.md b/tests/node-ref/README.md new file mode 100644 index 0000000..380a473 --- /dev/null +++ b/tests/node-ref/README.md @@ -0,0 +1,9 @@ +# Ref Example + +This example demonstrates the `install_tools` option. + +In this case, `yarn install` of the `ref` package triggers `node-gyp`, +which in this case uses tools in `/bin` and `/usr/bin`. We normally +take these for granted as being on the path but bazel runs the yarn +install script in the bare environment, so we need to specify these +explicitly to build up the appropriate `PATH`. diff --git a/tests/node-ref/WORKSPACE b/tests/node-ref/WORKSPACE new file mode 100644 index 0000000..7046f51 --- /dev/null +++ b/tests/node-ref/WORKSPACE @@ -0,0 +1,22 @@ +local_repository( + name = "org_pubref_rules_node", + path = "../..", +) + +load("@org_pubref_rules_node//node:rules.bzl", "node_repositories", "yarn_modules") + +node_repositories() + +yarn_modules( + name = "yarn_modules", + deps = { + "ref": "1.3.5", + }, + # yarn install of 'ref' triggers node-gyp. This will in turn + # require 'sh' and 'dirname'. Explicitly make these tools + # available during the yarn install step. + install_tools = [ + "sh", # /bin + "dirname", # #/usr/bin + ], +) diff --git a/tests/node-ref/example.js b/tests/node-ref/example.js new file mode 100644 index 0000000..1ff0f29 --- /dev/null +++ b/tests/node-ref/example.js @@ -0,0 +1,9 @@ +const ref = require("ref"); + +const buf = new Buffer(4) + +buf.writeInt32LE(12345, 0) +buf.type = ref.types.int + +console.log(buf.deref()) // ← 12345 + diff --git a/tests/node-ref/example_test.sh b/tests/node-ref/example_test.sh new file mode 100755 index 0000000..b643148 --- /dev/null +++ b/tests/node-ref/example_test.sh @@ -0,0 +1,7 @@ +set -e + +if (./example &) | grep -q '12345'; then + echo "PASS" +else + exit 1 +fi