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
MOS_MIGRATED_REVID=150188402
@@ -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;
+ }
}
