Skip to content

Commit

Permalink
Fix jq path in :pin runnable
Browse files Browse the repository at this point in the history
Problem reported on #426

The build typically takes place in the user's workspace, so using
`$(location)` (or more correctly, `$(rootpath)`) gives an undesirable
"external/" leading segment. This would be useful if the binary being
run is in the users workspace, but we know our binary is in the same
workspace defining the BUILD file.

This change also uses the runfiles helper since I think that's required
to make this work on Windows, though I only tested on Mac.
  • Loading branch information
Alex Eagle committed Sep 2, 2020
1 parent aed0eb3 commit 5c4bc76
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 5 additions & 6 deletions coursier.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ genrule(
sh_binary(
name = "pin",
srcs = ["pin.sh"],
args = [
"$(location :jq-binary)",
],
data = [
":jq-binary",
],
# Cannot use $(location)/$(rootpath) because we'll be an external repository
# relative to where the build is taking place
args = [repository_name()[1:] + "/jq"],
deps = ["@bazel_tools//tools/bash/runfiles"],
data = [":jq-binary"],
)
"""

Expand Down
14 changes: 12 additions & 2 deletions private/pin.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
#!/usr/bin/env bash

set -euo pipefail
# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -euo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

readonly maven_install_json_loc={maven_install_location}
# This script is run as a `sh_binary`, so ensure we are in the calling workspace before running Bazel.
readonly execution_root=$(cd "$(dirname "$maven_install_json_loc")" && bazel info execution_root)
readonly workspace_name=$(basename "$execution_root")
# `jq` is a platform-specific dependency with an unpredictable path.
readonly jq=$1
readonly jq=$(rlocation $1)
cat <<"RULES_JVM_EXTERNAL_EOF" | "$jq" --sort-keys --indent 4 . - > $maven_install_json_loc
{dependency_tree_json}
RULES_JVM_EXTERNAL_EOF
Expand Down

0 comments on commit 5c4bc76

Please sign in to comment.