Skip to content

Commit

Permalink
[6.4.0] Inject builtin modules at the end of the MODULE.bazel file (#…
Browse files Browse the repository at this point in the history
…19573)

- This will ensure toolchains registered from bazel_dep in users's
MODULE.bazel take priority over the ones registered in `bazel_tools`.
- If the root module already has an override for a builtin module, then
the default builtin module override is ignored.

Context: bazelbuild/rules_swift#1106

RELNOTES: None
PiperOrigin-RevId: 566969573
Change-Id: I0b207cb24f34c1d2906123787216bba59ce5d442
  • Loading branch information
meteorcloudy authored Sep 21, 2023
1 parent 2f75f30 commit 654a04d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bazel_dep(name = "blake3", version = "1.3.3")
bazel_dep(name = "zlib", version = "1.2.13")

# The following are required when building without WORKSPACE SUFFIX
bazel_dep(name = "rules_cc", version = "0.0.2")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_java", version = "5.5.0")
bazel_dep(name = "rules_proto", version = "4.0.0")

Expand Down
2 changes: 1 addition & 1 deletion src/MODULE.tools
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module(name = "bazel_tools")

bazel_dep(name = "rules_cc", version = "0.0.2")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "rules_java", version = "5.5.0")
bazel_dep(name = "rules_license", version = "0.0.3")
bazel_dep(name = "rules_proto", version = "4.0.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class ModuleFileGlobals {
private boolean hadNonModuleCall = false;
private final boolean ignoreDevDeps;
private final InterimModule.Builder module;
private final ImmutableMap<String, NonRegistryOverride> builtinModules;
private final Map<String, DepSpec> deps = new LinkedHashMap<>();
private final List<ModuleExtensionUsageBuilder> extensionUsageBuilders = new ArrayList<>();
private final Map<String, ModuleOverride> overrides = new HashMap<>();
Expand All @@ -80,21 +81,7 @@ public ModuleFileGlobals(
boolean ignoreDevDeps) {
module = InterimModule.builder().setKey(key).setRegistry(registry);
this.ignoreDevDeps = ignoreDevDeps;
if (ModuleKey.ROOT.equals(key)) {
overrides.putAll(builtinModules);
}
for (String builtinModule : builtinModules.keySet()) {
if (key.getName().equals(builtinModule)) {
// The built-in module does not depend on itself.
continue;
}
deps.put(builtinModule, DepSpec.create(builtinModule, Version.EMPTY, -1));
try {
addRepoNameUsage(builtinModule, "as a built-in dependency", Location.BUILTIN);
} catch (EvalException e) {
throw new IllegalStateException(e);
}
}
this.builtinModules = builtinModules;
}

@AutoValue
Expand Down Expand Up @@ -985,6 +972,26 @@ public void localPathOverride(String moduleName, String path) throws EvalExcepti
}

public InterimModule buildModule() throws EvalException {
// Add builtin modules as default deps of the current module.
for (String builtinModule : builtinModules.keySet()) {
if (module.getKey().getName().equals(builtinModule)) {
// The built-in module does not depend on itself.
continue;
}
deps.put(builtinModule, DepSpec.create(builtinModule, Version.EMPTY, -1));
try {
addRepoNameUsage(builtinModule, "as a built-in dependency", Location.BUILTIN);
} catch (EvalException e) {
throw new EvalException(
e.getMessage()
+ String.format(
", '%s' is a built-in dependency and cannot be used by any 'bazel_dep' or"
+ " 'use_repo' directive",
builtinModule),
e);
}
}
// Build module extension usages and the rest of the module.
var extensionUsages = ImmutableList.<ModuleExtensionUsage>builder();
for (var extensionUsageBuilder : extensionUsageBuilders) {
extensionUsages.add(extensionUsageBuilder.buildUsage());
Expand All @@ -997,6 +1004,12 @@ public InterimModule buildModule() throws EvalException {
}

public ImmutableMap<String, ModuleOverride> buildOverrides() {
// Add overrides for builtin modules if there is no existing override for them.
if (ModuleKey.ROOT.equals(module.getKey())) {
for (String moduleName : builtinModules.keySet()) {
overrides.putIfAbsent(moduleName, builtinModules.get(moduleName));
}
}
return ImmutableMap.copyOf(overrides);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,30 @@ public void testRootModule_badSelfOverride() throws Exception {
assertThat(result.getError().toString()).contains("invalid override for the root module");
}

@Test
public void testRootModule_overrideBuiltinModule() throws Exception {
setUpWithBuiltinModules(
ImmutableMap.of(
"bazel_tools",
LocalPathOverride.create(
rootDirectory.getRelative("bazel_tools_original").getPathString())));
scratch.file(
rootDirectory.getRelative("MODULE.bazel").getPathString(),
"module(name='aaa')",
"local_path_override(module_name='bazel_tools',path='./bazel_tools_new')");
FakeRegistry registry = registryFactory.newFakeRegistry("/foo");
ModuleFileFunction.REGISTRIES.set(differencer, ImmutableList.of(registry.getUrl()));

EvaluationResult<RootModuleFileValue> result =
evaluator.evaluate(
ImmutableList.of(ModuleFileValue.KEY_FOR_ROOT_MODULE), evaluationContext);
ModuleOverride bazelToolsOverride =
result.get(ModuleFileValue.KEY_FOR_ROOT_MODULE).getOverrides().get("bazel_tools");
assertThat(bazelToolsOverride).isInstanceOf(LocalPathOverride.class);
assertThat((LocalPathOverride) bazelToolsOverride)
.isEqualTo(LocalPathOverride.create("./bazel_tools_new"));
}

@Test
public void forgotVersion() throws Exception {
FakeRegistry registry = registryFactory.newFakeRegistry("/foo");
Expand Down

0 comments on commit 654a04d

Please sign in to comment.