Skip to content

Commit

Permalink
bzlmod: Add --registry flag
Browse files Browse the repository at this point in the history
(#13316)

This flag is repeatable and used by ModuleFileFunction to determine where to look for modules.

This is not yet testable with an integration test since this code path isn't hooked into production yet (we'll probably need a flag for that).

PiperOrigin-RevId: 383385942
  • Loading branch information
Wyverald authored and copybara-github committed Jul 7, 2021
1 parent 87325e5 commit 05265a4
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/google/devtools/build/lib/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
"//src/main/java/com/google/devtools/build/lib/analysis:blaze_directories",
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_configuration",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:registry",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution",
"//src/main/java/com/google/devtools/build/lib/bazel/commands",
"//src/main/java/com/google/devtools/build/lib/bazel/repository",
"//src/main/java/com/google/devtools/build/lib/bazel/repository/cache",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.bazel.bzlmod.DiscoveryFunction;
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileFunction;
import com.google.devtools.build.lib.bazel.bzlmod.RegistryFactory;
import com.google.devtools.build.lib.bazel.bzlmod.RegistryFactoryImpl;
import com.google.devtools.build.lib.bazel.bzlmod.SelectionFunction;
import com.google.devtools.build.lib.bazel.commands.FetchCommand;
import com.google.devtools.build.lib.bazel.commands.SyncCommand;
import com.google.devtools.build.lib.bazel.repository.LocalConfigPlatformFunction;
Expand Down Expand Up @@ -87,6 +92,7 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
Expand All @@ -98,6 +104,10 @@ public class BazelRepositoryModule extends BlazeModule {
// Default location (relative to output user root) of the repository cache.
public static final String DEFAULT_CACHE_LOCATION = "cache/repos/v1";

// Default list of registries.
public static final ImmutableList<String> DEFAULT_REGISTRIES =
ImmutableList.of("https://bcr.bazel.build/");

// A map of repository handlers that can be looked up by rule class name.
private final ImmutableMap<String, RepositoryFunction> repositoryHandlers;
private final AtomicBoolean isFetch = new AtomicBoolean(false);
Expand All @@ -115,6 +125,7 @@ public class BazelRepositoryModule extends BlazeModule {
private Optional<RootedPath> resolvedFileReplacingWorkspace = Optional.empty();
private Set<String> outputVerificationRules = ImmutableSet.of();
private FileSystem filesystem;
private List<String> registries;
// We hold the precomputed value of the managed directories here, so that the dependency
// on WorkspaceFileValue is not registered for each FileStateValue.
private final ManagedDirectoriesKnowledgeImpl managedDirectoriesKnowledge;
Expand Down Expand Up @@ -202,6 +213,13 @@ public void workspaceInit(
managedDirectoriesKnowledge,
BazelSkyframeExecutorConstants.EXTERNAL_PACKAGE_HELPER);
builder.addSkyFunction(SkyFunctions.REPOSITORY_DIRECTORY, repositoryDelegatorFunction);
RegistryFactory registryFactory =
new RegistryFactoryImpl(new HttpDownloader(), clientEnvironmentSupplier);
builder.addSkyFunction(
SkyFunctions.MODULE_FILE,
new ModuleFileFunction(registryFactory, directories.getWorkspace()));
builder.addSkyFunction(SkyFunctions.DISCOVERY, new DiscoveryFunction());
builder.addSkyFunction(SkyFunctions.SELECTION, new SelectionFunction());
filesystem = runtime.getFileSystem();
}

Expand Down Expand Up @@ -335,6 +353,12 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
overrides = ImmutableMap.of();
}

if (repoOptions.registries != null && !repoOptions.registries.isEmpty()) {
registries = repoOptions.registries;
} else {
registries = DEFAULT_REGISTRIES;
}

if (!Strings.isNullOrEmpty(repoOptions.repositoryHashFile)) {
Path hashFile;
if (env.getWorkspace() != null) {
Expand Down Expand Up @@ -394,7 +418,8 @@ public ImmutableList<Injected> getPrecomputedValues() {
RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY),
PrecomputedValue.injected(
RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_CONFIGURING,
RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY));
RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY),
PrecomputedValue.injected(ModuleFileFunction.REGISTRIES, registries));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ java_library(
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//src/main/java/com/google/devtools/common/options",
"//src/main/java/net/starlark/java/eval",
"//src/main/java/net/starlark/java/syntax",
"//third_party:apache_commons_compress",
"//third_party:auto_value",
"//third_party:flogger",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public class RepositoryOptions extends OptionsBase {
+ "as argument requests the cache to be disabled.")
public PathFragment experimentalRepositoryCache;

@Option(
name = "registry",
defaultValue = "null",
allowMultiple = true,
documentationCategory = OptionDocumentationCategory.BAZEL_CLIENT_OPTIONS,
effectTags = {OptionEffectTag.CHANGES_INPUTS},
help =
"Specifies the registries to use to locate Bazel module dependencies. The order is"
+ " important: modules will be looked up in earlier registries first, and only fall"
+ " back to later registries when they're missing from the earlier ones.")
public List<String> registries;

@Option(
name = "experimental_repository_cache_hardlinks",
defaultValue = "false",
Expand Down

0 comments on commit 05265a4

Please sign in to comment.