Skip to content

Commit c5bc34e

Browse files
tjgqYannicckolli5
authored
Add CommandLinePathFactory to CommandEnvironment (bazelbuild#15971)
Progress on bazelbuild#15856 Closes bazelbuild#15905. PiperOrigin-RevId: 462369707 Change-Id: If8dd493227b67dd4ffe30c9e0af4d8f71de4fb9f Co-authored-by: Yannic Bonenberger <[email protected]> Co-authored-by: Chenchu K <[email protected]>
1 parent 96d23d3 commit c5bc34e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java

+8
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ public class CommandEnvironment {
104104
private final ImmutableList<Any> commandExtensions;
105105
private final ImmutableList.Builder<Any> responseExtensions = ImmutableList.builder();
106106
private final Consumer<String> shutdownReasonConsumer;
107+
private final CommandLinePathFactory commandLinePathFactory;
107108

108109
private OutputService outputService;
109110
private TopDownActionCache topDownActionCache;
@@ -266,6 +267,9 @@ public void exit(AbruptExitException exception) {
266267
repoEnvFromOptions.put(name, value);
267268
}
268269
}
270+
271+
this.commandLinePathFactory =
272+
CommandLinePathFactory.create(runtime.getFileSystem(), directories);
269273
}
270274

271275
private Path computeWorkingDirectory(CommonCommandOptions commandOptions)
@@ -840,4 +844,8 @@ public ImmutableList<Any> getResponseExtensions() {
840844
public void addResponseExtensions(Iterable<Any> extensions) {
841845
responseExtensions.addAll(extensions);
842846
}
847+
848+
public CommandLinePathFactory getCommandLinePathFactory() {
849+
return commandLinePathFactory;
850+
}
843851
}

src/main/java/com/google/devtools/build/lib/runtime/CommandLinePathFactory.java

+18
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
// limitations under the License.
1414
package com.google.devtools.build.lib.runtime;
1515

16+
import com.google.common.annotations.VisibleForTesting;
1617
import com.google.common.base.Preconditions;
1718
import com.google.common.base.Splitter;
1819
import com.google.common.base.Strings;
1920
import com.google.common.collect.ImmutableMap;
21+
import com.google.devtools.build.lib.analysis.BlazeDirectories;
2022
import com.google.devtools.build.lib.vfs.FileSystem;
2123
import com.google.devtools.build.lib.vfs.Path;
2224
import com.google.devtools.build.lib.vfs.PathFragment;
@@ -44,11 +46,27 @@ public final class CommandLinePathFactory {
4446
private final FileSystem fileSystem;
4547
private final ImmutableMap<String, Path> roots;
4648

49+
@VisibleForTesting
4750
public CommandLinePathFactory(FileSystem fileSystem, ImmutableMap<String, Path> roots) {
4851
this.fileSystem = Preconditions.checkNotNull(fileSystem);
4952
this.roots = Preconditions.checkNotNull(roots);
5053
}
5154

55+
static CommandLinePathFactory create(FileSystem fileSystem, BlazeDirectories directories) {
56+
Preconditions.checkNotNull(fileSystem);
57+
Preconditions.checkNotNull(directories);
58+
59+
ImmutableMap.Builder<String, Path> wellKnownRoots = ImmutableMap.builder();
60+
61+
// This is necessary because some tests don't have a workspace set.
62+
Path workspace = directories.getWorkspace();
63+
if (workspace != null) {
64+
wellKnownRoots.put("workspace", workspace);
65+
}
66+
67+
return new CommandLinePathFactory(fileSystem, wellKnownRoots.build());
68+
}
69+
5270
/** Creates a {@link Path}. */
5371
public Path create(Map<String, String> env, String value) throws IOException {
5472
Preconditions.checkNotNull(env);

0 commit comments

Comments
 (0)