Skip to content

Commit 8ab83f9

Browse files
committed
Include module file globals in builtin.proto
This allows consumers of the proto to learn about globals only available in `MODULE.bazel` under a new `ApiContext`. Also adds a smoke test to verify that common symbols are contained in the proto for each API context.
1 parent 2e68211 commit 8ab83f9

File tree

6 files changed

+93
-2
lines changed

6 files changed

+93
-2
lines changed

src/main/java/com/google/devtools/build/docgen/ApiExporter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ public static void main(String[] args) {
375375
builtins, symbols.getGlobals(), globalToDoc, typeNameToConstructor, ApiContext.ALL);
376376
appendGlobals(
377377
builtins, symbols.getBzlGlobals(), globalToDoc, typeNameToConstructor, ApiContext.BZL);
378+
appendGlobals(
379+
builtins,
380+
symbols.getModuleFileGlobals(),
381+
globalToDoc,
382+
typeNameToConstructor,
383+
ApiContext.MODULE);
378384
appendNativeRules(builtins, symbols.getNativeRules());
379385
writeBuiltins(options.outputFile, builtins);
380386

src/main/java/com/google/devtools/build/docgen/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ java_binary(
7777
srcs = ["ApiExporter.java"],
7878
main_class = "com.google.devtools.build.docgen.ApiExporter",
7979
runtime_deps = [
80+
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution",
8081
"//src/main/java/com/google/devtools/build/lib/bazel/repository",
8182
"//src/main/java/net/starlark/java/syntax",
8283
],

src/main/java/com/google/devtools/build/docgen/SymbolFamilies.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class SymbolFamilies {
3939
// Mappings between Starlark names and Starlark entities generated from the fakebuildapi.
4040
private final ImmutableMap<String, Object> globals;
4141
private final ImmutableMap<String, Object> bzlGlobals;
42+
private final ImmutableMap<String, Object> moduleFileGlobals;
4243

4344
public SymbolFamilies(
4445
StarlarkDocExpander expander,
@@ -53,7 +54,8 @@ public SymbolFamilies(
5354
IllegalAccessException,
5455
BuildEncyclopediaDocException,
5556
ClassNotFoundException,
56-
IOException {
57+
IOException,
58+
InstantiationException {
5759
ConfiguredRuleClassProvider configuredRuleClassProvider = createRuleClassProvider(provider);
5860
this.nativeRules =
5961
ImmutableList.copyOf(
@@ -66,6 +68,7 @@ public SymbolFamilies(
6668
denyList));
6769
this.globals = Starlark.UNIVERSE;
6870
this.bzlGlobals = collectBzlGlobals(configuredRuleClassProvider);
71+
this.moduleFileGlobals = collectModuleFileGlobals();
6972
this.allDocPages = StarlarkDocumentationCollector.getAllDocPages(expander);
7073
}
7174

@@ -92,6 +95,14 @@ public Map<String, Object> getBzlGlobals() {
9295
return bzlGlobals;
9396
}
9497

98+
/*
99+
* Returns a mapping between Starlark names and Starlark entities that are available only in MODULE.bazel
100+
* files.
101+
*/
102+
public Map<String, Object> getModuleFileGlobals() {
103+
return moduleFileGlobals;
104+
}
105+
95106
// Returns a mapping between type names and module/type documentation.
96107
public ImmutableMap<Category, ImmutableList<StarlarkDocPage>> getAllDocPages() {
97108
return allDocPages;
@@ -132,10 +143,25 @@ private List<RuleDocumentation> collectNativeRules(
132143
}
133144

134145
private ConfiguredRuleClassProvider createRuleClassProvider(String classProvider)
135-
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException,
146+
throws NoSuchMethodException,
147+
InvocationTargetException,
148+
IllegalAccessException,
136149
ClassNotFoundException {
137150
Class<?> providerClass = Class.forName(classProvider);
138151
Method createMethod = providerClass.getMethod("create");
139152
return (ConfiguredRuleClassProvider) createMethod.invoke(null);
140153
}
154+
155+
private ImmutableMap<String, Object> collectModuleFileGlobals()
156+
throws ClassNotFoundException,
157+
NoSuchMethodException,
158+
InvocationTargetException,
159+
InstantiationException,
160+
IllegalAccessException {
161+
Class<?> moduleFileGlobals =
162+
Class.forName("com.google.devtools.build.lib.bazel.bzlmod.ModuleFileGlobals");
163+
ImmutableMap.Builder<String, Object> moduleFileEnv = ImmutableMap.builder();
164+
Starlark.addMethods(moduleFileEnv, moduleFileGlobals.getConstructor().newInstance());
165+
return moduleFileEnv.buildOrThrow();
166+
}
141167
}

src/main/protobuf/builtin.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ enum ApiContext {
5757
ALL = 0;
5858
BZL = 1;
5959
BUILD = 2;
60+
MODULE = 3;
6061
}
6162

6263
// Generic representation for a Starlark object. If the object is callable

src/test/java/com/google/devtools/build/lib/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,15 @@ test_suite(
149149
name = "others",
150150
tags = ["-" + n for n in TEST_SUITES],
151151
)
152+
153+
java_test(
154+
name = "BuiltinProtoSmokeTest",
155+
srcs = ["BuiltinProtoSmokeTest.java"],
156+
data = ["//src/main/java/com/google/devtools/build/lib:gen_api_proto"],
157+
env = {"BUILTIN_PROTO": "$(rlocationpath //src/main/java/com/google/devtools/build/lib:gen_api_proto)"},
158+
deps = [
159+
"//src/main/protobuf:builtin_java_proto",
160+
"//third_party:truth",
161+
"@bazel_tools//tools/java/runfiles",
162+
],
163+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.google.devtools.build.lib;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
import static java.util.stream.Collectors.toMap;
5+
6+
import com.google.devtools.build.docgen.builtin.BuiltinProtos;
7+
import com.google.devtools.build.runfiles.Runfiles;
8+
import java.io.BufferedInputStream;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import org.junit.BeforeClass;
14+
import org.junit.Test;
15+
import org.junit.runner.RunWith;
16+
import org.junit.runners.JUnit4;
17+
18+
@RunWith(JUnit4.class)
19+
public final class BuiltinProtoSmokeTest {
20+
static BuiltinProtos.Builtins builtins;
21+
22+
@BeforeClass
23+
public static void loadProto() throws IOException {
24+
Path protoPath =
25+
Path.of(Runfiles.preload().unmapped().rlocation(System.getenv("BUILTIN_PROTO")));
26+
try (InputStream inputStream = Files.newInputStream(protoPath);
27+
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
28+
builtins = BuiltinProtos.Builtins.parseFrom(bufferedInputStream);
29+
}
30+
}
31+
32+
@Test
33+
public void hasGlobalCallableFromEachApiContext() {
34+
assertThat(
35+
builtins.getGlobalList().stream()
36+
.filter(BuiltinProtos.Value::hasCallable)
37+
.filter(global -> !global.getCallable().getParamList().isEmpty())
38+
.collect(toMap(BuiltinProtos.Value::getName, BuiltinProtos.Value::getApiContext)))
39+
.containsAtLeast(
40+
"range", BuiltinProtos.ApiContext.ALL,
41+
"glob", BuiltinProtos.ApiContext.BUILD,
42+
"DefaultInfo", BuiltinProtos.ApiContext.BZL,
43+
"bazel_dep", BuiltinProtos.ApiContext.MODULE);
44+
}
45+
}

0 commit comments

Comments
 (0)