aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2017-03-15 14:23:19 +0000
committerGravatar Yun Peng <pcloudy@google.com>2017-03-16 08:34:44 +0000
commit2d5b58c2ff8d1cfd8086c69d432489e87054c588 (patch)
treebd60595f7aa3ca91f9a06952d2f8f89c3ebd3858 /src/main/java/com/google/devtools
parentb91795f3a94a2e5f004d512a71c79af62d0e8070 (diff)
Windows: update PATH for actions on Windows
If a SpawnAction executes a shell command, we need to fix PATH on Windows so it starts with "<msysroot>/bin;<msysroot>/usr/bin", otherwise bin utils like "find" would be found in the wrong places, e.g. as "c:/windows/system32/find.exe". Fixes https://github.com/bazelbuild/bazel/issues/2676 -- PiperOrigin-RevId: 150188402 MOS_MIGRATED_REVID=150188402
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r--src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java29
1 files changed, 27 insertions, 2 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java
index a09d9ba66a..49656875e0 100644
--- a/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java
+++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java
@@ -25,6 +25,7 @@ import com.google.devtools.build.lib.analysis.config.InvalidConfigurationExcepti
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.util.OS;
import com.google.devtools.build.lib.vfs.PathFragment;
+import javax.annotation.Nullable;
/**
* Bazel-specific configuration fragment.
@@ -78,8 +79,7 @@ public class BazelConfiguration extends Fragment {
@Override
public void setupShellEnvironment(ImmutableMap.Builder<String, String> builder) {
- String path = System.getenv("PATH");
- builder.put("PATH", path == null ? "/bin:/usr/bin" : path);
+ builder.put("PATH", pathOrDefault(System.getenv("PATH"), getShellExecutable()));
String ldLibraryPath = System.getenv("LD_LIBRARY_PATH");
if (ldLibraryPath != null) {
@@ -91,4 +91,29 @@ public class BazelConfiguration extends Fragment {
builder.put("TMPDIR", tmpdir);
}
}
+
+ private static String pathOrDefault(@Nullable String path, @Nullable PathFragment sh) {
+ if (OS.getCurrent() != OS.WINDOWS) {
+ return path == null ? "/bin:/usr/bin" : path;
+ }
+
+ // Attempt to compute the MSYS root (the real Windows path of "/") from `sh`.
+ String newPath = "";
+ if (sh != null && sh.getParentDirectory() != null) {
+ newPath = sh.getParentDirectory().getPathString();
+ if (sh.getParentDirectory().endsWith(new PathFragment("usr/bin"))) {
+ newPath +=
+ ";" + sh.getParentDirectory().getParentDirectory().replaceName("bin").getPathString();
+ } else if (sh.getParentDirectory().endsWith(new PathFragment("bin"))) {
+ newPath +=
+ ";" + sh.getParentDirectory().replaceName("usr").getRelative("bin").getPathString();
+ }
+ newPath = newPath.replace('/', '\\');
+
+ if (path != null) {
+ newPath += ";" + path;
+ }
+ }
+ return newPath;
+ }
}