Skip to content

Commit

Permalink
bzlmod: Allow absence of module() call in MODULE.bazel
Browse files Browse the repository at this point in the history
    (bazelbuild/bazel#13316)

    Not calling module() should be the same as calling module() with no arguments. This was mistakenly disallowed before since the Module autovalue builder didn't have defaults for name and version.

    PiperOrigin-RevId: 385119757
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent 54a0715 commit 46d5c08
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,13 @@ public abstract class Module {
public abstract String getName();

/** The version of the module. Must be empty iff the module has a {@link NonRegistryOverride}. */
public abstract String getVersion();
public abstract Version getVersion();

/**
* The compatibility level of the module, which essentially signifies the "major version" of the
* module in terms of SemVer.
*/
public abstract int getCompatibilityLevel();

/**
* The direct dependencies of this module. The key type is the repo name of the dep, and the value
Expand All @@ -55,7 +61,10 @@ public abstract class Module {

/** Returns a new, empty {@link Builder}. */
public static Builder builder() {
return new AutoValue_Module.Builder();
return new AutoValue_Module.Builder()
.setName("")
.setVersion(Version.EMPTY)
.setCompatibilityLevel(0);
}

/**
Expand All @@ -73,9 +82,14 @@ public Module withDepKeysTransformed(UnaryOperator<ModuleKey> transform) {
/** Builder type for {@link Module}. */
@AutoValue.Builder
public abstract static class Builder {
/** Optional; defaults to the empty string. */
public abstract Builder setName(String value);

public abstract Builder setVersion(String value);
/** Optional; defaults to {@link Version#EMPTY}. */
public abstract Builder setVersion(Version value);

/** Optional; defaults to {@code 0}. */
public abstract Builder setCompatibilityLevel(int value);

public abstract Builder setDeps(ImmutableMap<String, ModuleKey> value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
import com.google.devtools.build.lib.analysis.ServerDirectories;
import com.google.devtools.build.lib.analysis.util.AnalysisMock;
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileValue.RootModuleFileValue;
import com.google.devtools.build.lib.bazel.repository.starlark.StarlarkRepositoryModule;
import com.google.devtools.build.lib.packages.PackageFactory;
import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
Expand Down Expand Up @@ -175,18 +174,17 @@ public void testRootModule() throws Exception {
"bazel_dep(name='C',version='2.0',repo_name='see')",
"single_version_override(module_name='D',version='18')",
"local_path_override(module_name='E',path='somewhere/else')",
"multiple_version_override(module_name='F',versions=['1.0','2.0'])",
"archive_override(module_name='G',urls=['https://hello.com/world.zip'])");
"multiple_version_override(module_name='F',versions=['1.0','2.0'])");
FakeRegistry registry = registryFactory.newFakeRegistry();
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

EvaluationResult<RootModuleFileValue> result =
EvaluationResult<ModuleFileValue> result =
driver.evaluate(ImmutableList.of(ModuleFileValue.keyForRootModule()), evaluationContext);
if (result.hasError()) {
fail(result.getError().toString());
}
RootModuleFileValue rootModuleFileValue = result.get(ModuleFileValue.keyForRootModule());
assertThat(rootModuleFileValue.getModule())
ModuleFileValue moduleFileValue = result.get(ModuleFileValue.keyForRootModule());
assertThat(moduleFileValue.getModule())
.isEqualTo(
Module.builder()
.setName("A")
Expand All @@ -195,22 +193,13 @@ public void testRootModule() throws Exception {
.addDep("B", createModuleKey("B", "1.0"))
.addDep("see", createModuleKey("C", "2.0"))
.build());
assertThat(rootModuleFileValue.getOverrides())
assertThat(moduleFileValue.getOverrides())
.containsExactly(
"D", SingleVersionOverride.create(Version.parse("18"), "", ImmutableList.of(), 0),
"E", LocalPathOverride.create("somewhere/else"),
"F",
MultipleVersionOverride.create(
ImmutableList.of(Version.parse("1.0"), Version.parse("2.0")), ""),
"G",
ArchiveOverride.create(
ImmutableList.of("https://hello.com/world.zip"),
ImmutableList.of(),
"",
"",
0));
assertThat(rootModuleFileValue.getNonRegistryOverrideCanonicalRepoNameLookup())
.containsExactly("E.", "E", "G.", "G");
ImmutableList.of(Version.parse("1.0"), Version.parse("2.0")), ""));
}

@Test
Expand All @@ -221,16 +210,15 @@ public void testRootModule_noModuleFunctionIsOkay() throws Exception {
FakeRegistry registry = registryFactory.newFakeRegistry();
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

EvaluationResult<RootModuleFileValue> result =
EvaluationResult<ModuleFileValue> result =
driver.evaluate(ImmutableList.of(ModuleFileValue.keyForRootModule()), evaluationContext);
if (result.hasError()) {
fail(result.getError().toString());
}
RootModuleFileValue rootModuleFileValue = result.get(ModuleFileValue.keyForRootModule());
assertThat(rootModuleFileValue.getModule())
ModuleFileValue moduleFileValue = result.get(ModuleFileValue.keyForRootModule());
assertThat(moduleFileValue.getModule())
.isEqualTo(Module.builder().addDep("B", createModuleKey("B", "1.0")).build());
assertThat(rootModuleFileValue.getOverrides()).isEmpty();
assertThat(rootModuleFileValue.getNonRegistryOverrideCanonicalRepoNameLookup()).isEmpty();
assertThat(moduleFileValue.getOverrides()).isEmpty();
}

@Test
Expand All @@ -242,7 +230,7 @@ public void testRootModule_badSelfOverride() throws Exception {
FakeRegistry registry = registryFactory.newFakeRegistry();
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

EvaluationResult<RootModuleFileValue> result =
EvaluationResult<ModuleFileValue> result =
driver.evaluate(ImmutableList.of(ModuleFileValue.keyForRootModule()), evaluationContext);
assertThat(result.hasError()).isTrue();
assertThat(result.getError().toString()).contains("invalid override for the root module");
Expand Down

0 comments on commit 46d5c08

Please sign in to comment.