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
- 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 committed Sep 20, 2023
1 parent 2f75f30 commit bc8da19
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 22 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
8 changes: 5 additions & 3 deletions distdir_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ DIST_DEPS = {
# Used in src/main/java/com/google/devtools/build/lib/bazel/rules/java/jdk.WORKSPACE.
# Used in src/test/java/com/google/devtools/build/lib/blackbox/framework/blackbox.WORKSAPCE
"rules_cc": {
"archive": "rules_cc-0.0.2.tar.gz",
"sha256": "58bff40957ace85c2de21ebfc72e53ed3a0d33af8cc20abd0ceec55c63be7de2",
"urls": ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.2/rules_cc-0.0.2.tar.gz"],
"archive": "rules_cc-0.0.9.tar.gz",
"sha256": "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
"urls": ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz"],
"used_in": [
"additional_distfiles",
"test_WORKSPACE_files",
],
"package_version": "0.0.9",
"strip_prefix": "rules_cc-0.0.9",
},
"rules_java": {
"archive": "7cf3cefd652008d0a64a419c34c13bdca6c8f178.zip",
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
5 changes: 3 additions & 2 deletions src/test/py/bazel/testdata/runfiles_test/WORKSPACE.mock
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_cc",
sha256 = "58bff40957ace85c2de21ebfc72e53ed3a0d33af8cc20abd0ceec55c63be7de2",
sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
urls = [
"https://github.com/bazelbuild/rules_cc/releases/download/0.0.2/rules_cc-0.0.2.tar.gz",
"https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz",
],
strip_prefix = "rules_cc-0.0.9",
)

0 comments on commit bc8da19

Please sign in to comment.