diff --git a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java index 9d6919cf83c10d..967ac115f213f5 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java @@ -104,6 +104,7 @@ public class CommandEnvironment { private final ImmutableList commandExtensions; private final ImmutableList.Builder responseExtensions = ImmutableList.builder(); private final Consumer shutdownReasonConsumer; + private final CommandLinePathFactory commandLinePathFactory; private OutputService outputService; private TopDownActionCache topDownActionCache; @@ -266,6 +267,9 @@ public void exit(AbruptExitException exception) { repoEnvFromOptions.put(name, value); } } + + this.commandLinePathFactory = + CommandLinePathFactory.create(runtime.getFileSystem(), directories); } private Path computeWorkingDirectory(CommonCommandOptions commandOptions) @@ -840,4 +844,8 @@ public ImmutableList getResponseExtensions() { public void addResponseExtensions(Iterable extensions) { responseExtensions.addAll(extensions); } + + public CommandLinePathFactory getCommandLinePathFactory() { + return commandLinePathFactory; + } } diff --git a/src/main/java/com/google/devtools/build/lib/runtime/CommandLinePathFactory.java b/src/main/java/com/google/devtools/build/lib/runtime/CommandLinePathFactory.java index e39bf074b2d91c..074bda748af98d 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/CommandLinePathFactory.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/CommandLinePathFactory.java @@ -13,10 +13,12 @@ // limitations under the License. package com.google.devtools.build.lib.runtime; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Splitter; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; +import com.google.devtools.build.lib.analysis.BlazeDirectories; import com.google.devtools.build.lib.vfs.FileSystem; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; @@ -44,11 +46,27 @@ public final class CommandLinePathFactory { private final FileSystem fileSystem; private final ImmutableMap roots; + @VisibleForTesting public CommandLinePathFactory(FileSystem fileSystem, ImmutableMap roots) { this.fileSystem = Preconditions.checkNotNull(fileSystem); this.roots = Preconditions.checkNotNull(roots); } + static CommandLinePathFactory create(FileSystem fileSystem, BlazeDirectories directories) { + Preconditions.checkNotNull(fileSystem); + Preconditions.checkNotNull(directories); + + ImmutableMap.Builder wellKnownRoots = ImmutableMap.builder(); + + // This is necessary because some tests don't have a workspace set. + Path workspace = directories.getWorkspace(); + if (workspace != null) { + wellKnownRoots.put("workspace", workspace); + } + + return new CommandLinePathFactory(fileSystem, wellKnownRoots.build()); + } + /** Creates a {@link Path}. */ public Path create(Map env, String value) throws IOException { Preconditions.checkNotNull(env);