diff options
author | Dmitry Lomov <dslomov@google.com> | 2016-02-17 09:50:03 +0000 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2016-02-17 09:55:44 +0000 |
commit | 8efc3ef049381c18addbdeca0cdbfe7b2b870301 (patch) | |
tree | cb12e8beb9d4aeac27117bd0663dfb8d9d5ec26e /src/main/java/com | |
parent | 2fc8f97bf66a3b12dd751d265640b6b5494b2a7b (diff) |
Use absolute paths in ProcessBuilder invocations.
Needed for #276.
--
MOS_MIGRATED_REVID=114838538
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/shell/Command.java | 28 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/worker/Worker.java | 10 |
2 files changed, 32 insertions, 6 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) { diff --git a/src/main/java/com/google/devtools/build/lib/worker/Worker.java b/src/main/java/com/google/devtools/build/lib/worker/Worker.java index 8ad0a75c7f..cb8d4923cc 100644 --- a/src/main/java/com/google/devtools/build/lib/worker/Worker.java +++ b/src/main/java/com/google/devtools/build/lib/worker/Worker.java @@ -19,6 +19,7 @@ import com.google.devtools.build.lib.events.Reporter; import com.google.devtools.build.lib.util.Preconditions; import com.google.devtools.build.lib.vfs.Path; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -58,8 +59,15 @@ final class Worker { int workerId = pidCounter.getAndIncrement(); Path logFile = logDir.getRelative("worker-" + workerId + "-" + key.getMnemonic() + ".log"); + String[] command = key.getArgs().toArray(new String[0]); + + // Follows the logic of {@link com.google.devtools.build.lib.shell.Command}. + File executable = new File(command[0]); + if (!executable.isAbsolute() && executable.getParent() != null) { + command[0] = new File(key.getWorkDir().getPathFile(), command[0]).getAbsolutePath(); + } ProcessBuilder processBuilder = - new ProcessBuilder(key.getArgs().toArray(new String[0])) + new ProcessBuilder(command) .directory(key.getWorkDir().getPathFile()) .redirectError(Redirect.appendTo(logFile.getPathFile())); processBuilder.environment().putAll(key.getEnv()); |