Skip to content

Commit

Permalink
Support yarn_install & npm_install bazelBin additionalAttributes
Browse files Browse the repository at this point in the history
…for generated nodejs_binary targets for package.json `bin` files (#649)
  • Loading branch information
gregmagolan authored Apr 3, 2019
1 parent 079501a commit 6931c75
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 1 deletion.
9 changes: 9 additions & 0 deletions e2e/bazel_bin/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Print test logs for failed tests
test --test_output=errors

# Enable debugging tests with --config=debug
test:debug --test_arg=--node_options=--inspect-brk --test_output=streamed --test_strategy=exclusive --test_timeout=9999 --nocache_test_results

# Turn off legacy external runfiles
run --nolegacy_external_runfiles
test --nolegacy_external_runfiles
8 changes: 8 additions & 0 deletions e2e/bazel_bin/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sh_test(
name = "test",
srcs = ["test.sh"],
data = [
"@bazel_tools//tools/bash/runfiles",
"@npm//testy/bin:testy",
],
)
32 changes: 32 additions & 0 deletions e2e/bazel_bin/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

workspace(name = "e2e_bazel_bin")

local_repository(
name = "build_bazel_rules_nodejs",
path = "../../dist/build_bazel_rules_nodejs/release",
)

load("@build_bazel_rules_nodejs//:defs.bzl", "yarn_install")

yarn_install(
name = "npm",
data = [
"@e2e_bazel_bin//:npm/testy/index.js",
"@e2e_bazel_bin//:npm/testy/package.json",
],
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
)
1 change: 1 addition & 0 deletions e2e/bazel_bin/npm/testy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(`Hello ${process.env['some_env']}`);
14 changes: 14 additions & 0 deletions e2e/bazel_bin/npm/testy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "testy",
"version": "0.0.1",
"bin": {
"testy": "./index.js"
},
"bazelBin": {
"testy": {
"additionalAttributes": {
"configuration_env_vars": "[\"some_env\"]"
}
}
}
}
9 changes: 9 additions & 0 deletions e2e/bazel_bin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"private": true,
"dependencies": {
"testy": "file:./npm/testy"
},
"scripts": {
"test": "bazel test :test --define=some_env=world"
}
}
35 changes: 35 additions & 0 deletions e2e/bazel_bin/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
set -e

# --- begin runfiles.bash initialization ---
# Source the runfiles library:
# https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash
# The runfiles library defines rlocation, which is a platform independent function
# used to lookup the runfiles locations. This code snippet is needed at the top
# of scripts that use rlocation to lookup the location of runfiles.bash and source it
if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
if [[ -f "$0.runfiles_manifest" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
elif [[ -f "$0.runfiles/MANIFEST" ]]; then
export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
export RUNFILES_DIR="$0.runfiles"
fi
fi
if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
"$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
else
echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
exit 1
fi
# --- end runfiles.bash initialization ---

readonly OUT=$($(rlocation "npm/node_modules/testy/testy__bin"))

if [ "$OUT" != "Hello world" ]; then
echo "Expected output 'Hello world' but was $OUT"
exit 1
fi
2 changes: 2 additions & 0 deletions e2e/bazel_bin/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
19 changes: 18 additions & 1 deletion internal/npm_install/generate_build_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -711,12 +711,29 @@ filegroup(

if (pkg._executables) {
for (const [name, path] of pkg._executables.entries()) {
// Handle additionalAttributes of format:
// ```
// "bazelBin": {
// "ngc-wrapped": {
// "additionalAttributes": {
// "configuration_env_vars": "[\"compile\"]"
// }
// },
// ```
let additionalAttributes = '';
if (pkg.bazelBin && pkg.bazelBin[name] && pkg.bazelBin[name].additionalAttributes) {
const attrs = pkg.bazelBin[name].additionalAttributes;
for (const attrName of Object.keys(attrs)) {
const attrValue = attrs[attrName];
additionalAttributes += `\n ${attrName} = ${attrValue},`;
}
}
result += `# Wire up the \`bin\` entry \`${name}\`
nodejs_binary(
name = "${name}__bin",
entry_point = "${pkg._dir}/${path}",
install_source_map_support = False,
data = [":${pkg._name}__pkg"],
data = [":${pkg._name}__pkg"],${additionalAttributes}
)
`;
Expand Down

0 comments on commit 6931c75

Please sign in to comment.