From eaeb97307f794bca7816d5227e6085a218fa18aa Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Wed, 10 Apr 2019 17:17:45 +0200 Subject: [PATCH 1/2] multirun(), command(): export RUNFILES_* envvars Fixes the bug in https://github.com/bazelbuild/bazel/pull/7774 --- multirun/def.bzl | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/multirun/def.bzl b/multirun/def.bzl index edd8c6e..d711d97 100644 --- a/multirun/def.bzl +++ b/multirun/def.bzl @@ -4,10 +4,36 @@ _CONTENT_PREFIX = """#!/usr/bin/env bash set -euo pipefail +# --- begin runfiles.bash initialization --- +# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). +set -euo pipefail +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 --- + +# Export RUNFILES_* envvars (and a couple more) for subprocesses. +runfiles_export_envvars + """ def _multirun_impl(ctx): - runfiles = ctx.runfiles() + runfiles = ctx.runfiles().merge(ctx.attr._bash_runfiles[DefaultInfo].default_runfiles) content = [_CONTENT_PREFIX] for command in ctx.attr.commands: @@ -45,6 +71,9 @@ _multirun = rule( doc = "Targets to run in specified order", cfg = "target", ), + "_bash_runfiles": attr.label( + default = Label("@bazel_tools//tools/bash/runfiles"), + ), }, executable = True, ) @@ -59,7 +88,8 @@ def multirun(**kwargs): ) def _command_impl(ctx): - runfiles = ctx.runfiles() + runfiles = ctx.runfiles().merge(ctx.attr._bash_runfiles[DefaultInfo].default_runfiles) + defaultInfo = ctx.attr.command[DefaultInfo] default_runfiles = defaultInfo.default_runfiles @@ -118,6 +148,9 @@ _command = rule( doc = "Target to run", cfg = "target", ), + "_bash_runfiles": attr.label( + default = Label("@bazel_tools//tools/bash/runfiles"), + ), }, executable = True, ) From 5a6cec1d2b4a49bf8fcc128760532c72068973a2 Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Wed, 10 Apr 2019 17:21:57 +0200 Subject: [PATCH 2/2] Remove unnecessary 'set -euo pipefail' --- multirun/def.bzl | 2 -- 1 file changed, 2 deletions(-) diff --git a/multirun/def.bzl b/multirun/def.bzl index d711d97..1879103 100644 --- a/multirun/def.bzl +++ b/multirun/def.bzl @@ -2,8 +2,6 @@ load("@bazel_skylib//lib:shell.bzl", "shell") _CONTENT_PREFIX = """#!/usr/bin/env bash -set -euo pipefail - # --- begin runfiles.bash initialization --- # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). set -euo pipefail