Skip to content

Commit

Permalink
Throw an error when the root module specifies overrides on nonexisten…
Browse files Browse the repository at this point in the history
…t modules

Fixes bazelbuild#17125.

RELNOTES: Bazel now throws an error if the root module specifies overrides on nonexistent modules.
PiperOrigin-RevId: 571854500
Change-Id: Idc68f3968cfc05a953349ec9194bae531de6c33c
  • Loading branch information
Wyverald authored and copybara-github committed Oct 9, 2023
1 parent 771f165 commit 7a6df02
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.devtools.build.lib.analysis.BlazeVersionInfo;
import com.google.devtools.build.lib.bazel.BazelVersion;
import com.google.devtools.build.lib.bazel.bzlmod.InterimModule.DepSpec;
Expand All @@ -44,6 +46,7 @@
import com.google.devtools.build.skyframe.SkyframeLookupResult;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -119,6 +122,8 @@ private static Selection.Result discoverAndSelect(Environment env, RootModuleFil
return null;
}

verifyAllOverridesAreOnExistentModules(initialDepGraph, root.getOverrides());

Selection.Result selectionResult;
try (SilentCloseable c = Profiler.instance().profile(ProfilerTask.BZLMOD, "selection")) {
selectionResult = Selection.run(initialDepGraph, root.getOverrides());
Expand Down Expand Up @@ -152,6 +157,23 @@ private static Selection.Result discoverAndSelect(Environment env, RootModuleFil
return selectionResult;
}

private static void verifyAllOverridesAreOnExistentModules(
ImmutableMap<ModuleKey, InterimModule> initialDepGraph,
ImmutableMap<String, ModuleOverride> overrides)
throws BazelModuleResolutionFunctionException {
ImmutableSet<String> existentModules =
initialDepGraph.values().stream().map(InterimModule::getName).collect(toImmutableSet());
Set<String> nonexistentModules = Sets.difference(overrides.keySet(), existentModules);
if (!nonexistentModules.isEmpty()) {
throw new BazelModuleResolutionFunctionException(
ExternalDepsException.withMessage(
Code.BAD_MODULE,
"the root module specifies overrides on nonexistent module(s): %s",
Joiner.on(", ").join(nonexistentModules)),
Transience.PERSISTENT);
}
}

private static void verifyRootModuleDirectDepsAreAccurate(
InterimModule discoveredRootModule,
InterimModule resolvedRootModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,40 @@ public void testYankedVersionSideEffects_differentCompatibilityLevel() throws Ex
+ " compatibility level 3 which is different");
assertDoesNotContainEvent("hello from yanked version");
}

@Test
public void overrideOnNonexistentModule() throws Exception {
scratch.file(
rootDirectory.getRelative("MODULE.bazel").getPathString(),
"module(name='mod', version='1.0')",
"bazel_dep(name = 'a', version = '1.0')",
"bazel_dep(name = 'b', version = '1.1')",
"local_path_override(module_name='d', path='whatevs')");

FakeRegistry registry =
registryFactory
.newFakeRegistry("/bar")
.addModule(
createModuleKey("a", "1.0"),
"module(name='a', version='1.0')",
"bazel_dep(name='b', version='1.0')")
.addModule(createModuleKey("c", "1.0"), "module(name='c', version='1.0')")
.addModule(createModuleKey("c", "1.1"), "module(name='c', version='1.1')")
.addModule(
createModuleKey("b", "1.0"),
"module(name='b', version='1.0')",
"bazel_dep(name='c', version='1.1')")
.addModule(
createModuleKey("b", "1.1"),
"module(name='b', version='1.1')",
"bazel_dep(name='c', version='1.0')");

ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));
EvaluationResult<BazelModuleResolutionValue> result =
evaluator.evaluate(ImmutableList.of(BazelModuleResolutionValue.KEY), evaluationContext);

assertThat(result.hasError()).isTrue();
assertThat(result.getError().toString())
.contains("the root module specifies overrides on nonexistent module(s): d");
}
}

0 comments on commit 7a6df02

Please sign in to comment.