diff options
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java | 17 | ||||
-rwxr-xr-x | src/test/shell/bazel/skylark_repository_test.sh | 12 |
2 files changed, 21 insertions, 8 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java index 2817cab15d..22b6f872bb 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java @@ -307,7 +307,7 @@ public class SkylarkRepositoryContext { Location.BUILTIN, "Program argument of which() may not contains a / or a \\ ('" + program + "' given)"); } - for (String p : pathEnv) { + for (String p : getPathEnvironment()) { PathFragment fragment = new PathFragment(p); if (fragment.isAbsolute()) { // We ignore relative path as they don't mean much here (relative to where? the workspace @@ -326,20 +326,23 @@ public class SkylarkRepositoryContext { return Runtime.NONE; } - // This is non final so that test can overwrite it. - private static ImmutableList<String> pathEnv = getPathEnvironment(); + // This is just for test to overwrite the path environment + private static ImmutableList<String> pathEnv = null; @VisibleForTesting static void setPathEnvironment(String... pathEnv) { SkylarkRepositoryContext.pathEnv = ImmutableList.<String>copyOf(pathEnv); } - private static ImmutableList<String> getPathEnvironment() { - String pathEnv = System.getenv("PATH"); - if (pathEnv == null) { + private ImmutableList<String> getPathEnvironment() { + if (pathEnv != null) { + return pathEnv; + } + String pathEnviron = osObject.getEnviron().get("PATH"); + if (pathEnviron == null) { return ImmutableList.of(); } - return ImmutableList.copyOf(pathEnv.split(File.pathSeparator)); + return ImmutableList.copyOf(pathEnviron.split(File.pathSeparator)); } @Override diff --git a/src/test/shell/bazel/skylark_repository_test.sh b/src/test/shell/bazel/skylark_repository_test.sh index 7d49598e2f..7c6cb7b4ee 100755 --- a/src/test/shell/bazel/skylark_repository_test.sh +++ b/src/test/shell/bazel/skylark_repository_test.sh @@ -268,12 +268,22 @@ EOF function test_skylark_repository_which_and_execute() { setup_skylark_repository + bazel info + + # Test we are using the client environment, not the server one + echo "#!/bin/bash" > bin.sh + echo "exit 0" >> bin.sh + chmod +x bin.sh + # Our custom repository rule cat >test.bzl <<EOF def _impl(ctx): bash = ctx.which("bash") if bash == None: fail("Bash not found!") + bin = ctx.which("bin.sh") + if bin == None: + fail("bin.sh not found!") result = ctx.execute([bash, "--version"]) if result.return_code != 0: fail("Non-zero return code from bash: " + result.return_code) @@ -285,7 +295,7 @@ def _impl(ctx): repo = repository_rule(implementation=_impl, local=True) EOF - bazel build @foo//:bar >& $TEST_log || fail "Failed to build" + PATH="${PATH}:${PWD}" bazel build @foo//:bar >& $TEST_log || fail "Failed to build" expect_log "version" } |