aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/shell
diff options
context:
space:
mode:
authorGravatar Dmitry Lomov <dslomov@google.com>2016-02-17 09:50:03 +0000
committerGravatar Damien Martin-Guillerez <dmarting@google.com>2016-02-17 09:55:44 +0000
commit8efc3ef049381c18addbdeca0cdbfe7b2b870301 (patch)
treecb12e8beb9d4aeac27117bd0663dfb8d9d5ec26e /src/main/java/com/google/devtools/build/lib/shell
parent2fc8f97bf66a3b12dd751d265640b6b5494b2a7b (diff)
Use absolute paths in ProcessBuilder invocations.
Needed for #276. -- MOS_MIGRATED_REVID=114838538
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/shell')
-rw-r--r--src/main/java/com/google/devtools/build/lib/shell/Command.java28
1 files changed, 23 insertions, 5 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/shell/Command.java b/src/main/java/com/google/devtools/build/lib/shell/Command.java
index 8d9cffc838..794be920e9 100644
--- a/src/main/java/com/google/devtools/build/lib/shell/Command.java
+++ b/src/main/java/com/google/devtools/build/lib/shell/Command.java
@@ -178,10 +178,20 @@ public final class Command {
/**
* Creates a new {@link Command} for the given command line elements. The
- * command line is executed exactly as given, without a shell. The given
- * environment variables and working directory are used in subsequent
+ * command line is executed without a shell.
+ *
+ * The given environment variables and working directory are used in subsequent
* calls to {@link #execute()}.
*
+ * This command treats the 0-th element of {@code commandLineElement}
+ * (the name of an executable to run) specially.
+ * <ul>
+ * <li>If it is an absolute path, it is used as it</li>
+ * <li>If it is a single file name, the PATH lookup is performed</li>
+ * <li>If it is a relative path that is not a single file name, the command will attempt to
+ * execute the the binary at that path relative to {@code workingDirectory}.</li>
+ * </ul>
+ *
* @param commandLineElements elements of raw command line to execute
* @param environmentVariables environment variables to replace JVM's
* environment variables; may be null
@@ -189,12 +199,20 @@ public final class Command {
* working directory is used
* @throws IllegalArgumentException if commandLine is null or empty
*/
- public Command(final String[] commandLineElements,
- final Map<String, String> environmentVariables,
- final File workingDirectory) {
+ public Command(
+ String[] commandLineElements,
+ final Map<String, String> environmentVariables,
+ final File workingDirectory) {
if (commandLineElements == null || commandLineElements.length == 0) {
throw new IllegalArgumentException("command line is null or empty");
}
+
+ File executable = new File(commandLineElements[0]);
+ if (!executable.isAbsolute() && executable.getParent() != null) {
+ commandLineElements = commandLineElements.clone();
+ commandLineElements[0] = new File(workingDirectory, commandLineElements[0]).getAbsolutePath();
+ }
+
this.processBuilder =
new ProcessBuilder(commandLineElements);
if (environmentVariables != null) {