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

Set --preserve-symlinks-main flag #806

Merged
merged 2 commits into from
May 31, 2019
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# If you change the `default_docker_image` version, also change the `cache_key` version
var_1: &default_docker_image circleci/node:10.12
var_2: &browsers_docker_image circleci/node:10.12-browsers
var_3: &cache_key node-0.12-{{ checksum "yarn.lock" }}-{{ checksum "examples/program/yarn.lock" }}-{{ checksum "internal/npm_install/test/package/yarn.lock" }}
var_3: &cache_key node-0.12-{{ checksum "yarn.lock" }}-{{ checksum "examples/program/yarn.lock" }}-{{ checksum "internal/npm_install/test/package/yarn.lock" }}-2
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

cache bust needed as it was dirtied from #777 when changing the tools/npm/bazel_workspaces files


var_4: &init_environment
run:
Expand Down
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