Skip to content

Commit

Permalink
Add CommandLinePathFactory to CommandEnvironment (#15971)
Browse files Browse the repository at this point in the history
Progress on #15856

Closes #15905.

PiperOrigin-RevId: 462369707
Change-Id: If8dd493227b67dd4ffe30c9e0af4d8f71de4fb9f

Co-authored-by: Yannic Bonenberger <[email protected]>
Co-authored-by: Chenchu K <[email protected]>
  • Loading branch information
3 people authored Jul 25, 2022
1 parent 96d23d3 commit c5bc34e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public class CommandEnvironment {
private final ImmutableList<Any> commandExtensions;
private final ImmutableList.Builder<Any> responseExtensions = ImmutableList.builder();
private final Consumer<String> shutdownReasonConsumer;
private final CommandLinePathFactory commandLinePathFactory;

private OutputService outputService;
private TopDownActionCache topDownActionCache;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -840,4 +844,8 @@ public ImmutableList<Any> getResponseExtensions() {
public void addResponseExtensions(Iterable<Any> extensions) {
responseExtensions.addAll(extensions);
}

public CommandLinePathFactory getCommandLinePathFactory() {
return commandLinePathFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -44,11 +46,27 @@ public final class CommandLinePathFactory {
private final FileSystem fileSystem;
private final ImmutableMap<String, Path> roots;

@VisibleForTesting
public CommandLinePathFactory(FileSystem fileSystem, ImmutableMap<String, Path> 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<String, Path> 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<String, String> env, String value) throws IOException {
Preconditions.checkNotNull(env);
Expand Down

0 comments on commit c5bc34e

Please sign in to comment.