Skip to content

Commit

Permalink
Set --preserve-symlinks-main flag along with --preserve-symlinks flag…
Browse files Browse the repository at this point in the history
… when node version is 10.2.0 or greater (#806)
  • Loading branch information
gregmagolan authored and alexeagle committed May 31, 2019
1 parent bd052d3 commit 5759d07
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/node/generate_build_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function generateBuildFile() {
package(default_visibility = ["//visibility:public"])
exports_files([
"run_npm.sh.template",
"bin/node_args.sh",
"bin/node_repo_args.sh",
"${NODE_DIR}/bin/node",
"bin/node${binaryExt}",
"bin/npm${binaryExt}",
Expand Down
2 changes: 1 addition & 1 deletion internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ _NODEJS_EXECUTABLE_ATTRS = {
allow_files = True,
),
"_repository_args": attr.label(
default = Label("@nodejs//:bin/node_args.sh"),
default = Label("@nodejs//:bin/node_repo_args.sh"),
allow_single_file = True,
),
"_source_map_support_files": attr.label_list(
Expand Down
38 changes: 28 additions & 10 deletions internal/node/node_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ See https://docs.bazel.build/versions/master/skylark/repository_rules.html
"""

load("//internal/common:check_bazel_version.bzl", "check_bazel_version")
load("//internal/common:check_version.bzl", "check_version")
load("//internal/common:os_name.bzl", "os_name")
load("//internal/npm_install:npm_install.bzl", "yarn_install")
load("//third_party/github.com/bazelbuild/bazel-skylib:lib/paths.bzl", "paths")
Expand Down Expand Up @@ -201,11 +202,11 @@ def _prepare_node(repository_ctx):
repository_ctx.attr.vendored_node.workspace_root,
repository_ctx.attr.vendored_node.package,
repository_ctx.attr.vendored_node.name,
"bin/npm" if not is_windows else "node_modules/npm/bin/npm-cli.js",
"lib/node_modules/npm/bin/npm-cli.js" if not is_windows else "node_modules/npm/bin/npm-cli.js",
] if f])
else:
node_exec = "{}/bin/node".format(NODE_DIR) if not is_windows else "{}/node.exe".format(NODE_DIR)
npm_script = "{}/bin/npm".format(NODE_DIR) if not is_windows else "{}/node_modules/npm/bin/npm-cli.js".format(NODE_DIR)
npm_script = "{}/lib/node_modules/npm/bin/npm-cli.js".format(NODE_DIR) if not is_windows else "{}/node_modules/npm/bin/npm-cli.js".format(NODE_DIR)
if repository_ctx.attr.vendored_yarn:
yarn_script = "/".join([f for f in [
"../../..",
Expand All @@ -227,6 +228,20 @@ def _prepare_node(repository_ctx):
if not repository_ctx.attr.preserve_symlinks:
print("\nWARNING: The preserve_symlinks option is deprecated and will go away in the future.\n")

if repository_ctx.attr.preserve_symlinks:
# --preserve-symlinks-main flag added in node 10.2.0
# See https://nodejs.org/api/cli.html#cli_preserve_symlinks_main
preserve_symlinks_main_support = check_version(repository_ctx.attr.node_version, "10.2.0")
if preserve_symlinks_main_support:
node_args = "--preserve-symlinks --preserve-symlinks-main"
node_repo_args = "\"--node_options=--preserve-symlinks --node_options=--preserve-symlinks-main\""
else:
node_args = "--preserve-symlinks"
node_repo_args = "--node_options=--preserve-symlinks"
else:
node_args = ""
node_repo_args = ""

# The entry points for node for osx/linux and windows
if not is_windows:
# Sets PATH and runs the application
Expand All @@ -236,29 +251,28 @@ def _prepare_node(repository_ctx):
set -e
{get_script_dir}
export PATH="$SCRIPT_DIR":$PATH
exec "$SCRIPT_DIR/{node}" "$@"
exec "$SCRIPT_DIR/{node}" {args} "$@"
""".format(
get_script_dir = GET_SCRIPT_DIR,
node = node_exec_relative,
args = node_args,
))
else:
# Sets PATH for node, npm & yarn and run user script
repository_ctx.file("bin/node.cmd", content = """
@echo off
SET SCRIPT_DIR=%~dp0
SET PATH=%SCRIPT_DIR%;%PATH%
CALL "%SCRIPT_DIR%\\{node}" %*
""".format(node = node_exec_relative))
CALL "%SCRIPT_DIR%\\{node}" {args} %*
""".format(node = node_exec_relative, args = node_args))

# Shell script to set repository arguments for node used by nodejs_binary & nodejs_test launcher
# TODO(gregmagolan): add --node_options=--preserve-symlinks-main as well if preserve_symlinks is True &
# node version >= 10.2.0
repository_ctx.file("bin/node_args.sh", content = """#!/usr/bin/env bash
repository_ctx.file("bin/node_repo_args.sh", content = """#!/usr/bin/env bash
# Immediately exit if any command fails.
set -e
# Generated by node_repositories.bzl
export NODE_REPOSITORY_ARGS={}
""".format("--node_options=--preserve-symlinks" if repository_ctx.attr.preserve_symlinks else ""), executable = True)
""".format(node_repo_args), executable = True)

# The entry points for npm for osx/linux and windows
# Runs npm using appropriate node entry point
Expand Down Expand Up @@ -515,9 +529,13 @@ def node_repositories(
when you manually run the package manager, e.g. with
`bazel run @nodejs//:yarn` or `bazel run @nodejs//:npm install`.
If you use bazel-managed dependencies, you can omit this attribute.
node_version: optional; the specific version of NodeJS to install.
node_version: optional; the specific version of NodeJS to install or, if
vendored_node is specified, the vendored version of node.
yarn_version: optional; the specific version of Yarn to install.
vendored_node: optional; the local path to a pre-installed NodeJS runtime.
If set then also set node_version to the version that of node that is vendored.
Bazel will automatically turn on features such as --preserve-symlinks-main if they
are supported by the node version being used.
vendored_yarn: optional; the local path to a pre-installed yarn tool.
node_repositories: optional; custom list of node repositories to use.
yarn_repositories: optional; custom list of yarn repositories to use.
Expand Down

0 comments on commit 5759d07

Please sign in to comment.