Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support yarn_install & npm_install bazelBin additionalAttributes #649

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like we also need to document this somewhere, maybe just start a wiki page about how to publish a package to NPM that works under Bazel?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was thinking that as well. Either a WIKI or add it to DEVELOPING.md and link from the main README.md.

// ```
// "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