Skip to content

Commit

Permalink
Include module file globals in builtin.proto
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
fmeum committed Apr 8, 2024
1 parent 2e68211 commit 1cf8845
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ public static void main(String[] args) {
builtins, symbols.getGlobals(), globalToDoc, typeNameToConstructor, ApiContext.ALL);
appendGlobals(
builtins, symbols.getBzlGlobals(), globalToDoc, typeNameToConstructor, ApiContext.BZL);
appendGlobals(
builtins,
symbols.getModuleFileGlobals(),
globalToDoc,
typeNameToConstructor,
ApiContext.MODULE);
appendNativeRules(builtins, symbols.getNativeRules());
writeBuiltins(options.outputFile, builtins);

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/docgen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ java_binary(
srcs = ["ApiExporter.java"],
main_class = "com.google.devtools.build.docgen.ApiExporter",
runtime_deps = [
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution",
"//src/main/java/com/google/devtools/build/lib/bazel/repository",
"//src/main/java/net/starlark/java/syntax",
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.
package com.google.devtools.build.docgen;

import static com.google.common.collect.ImmutableMap.toImmutableMap;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.docgen.StarlarkDocumentationProcessor.Category;
Expand All @@ -26,6 +28,7 @@
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.Starlark;

/**
Expand All @@ -39,6 +42,7 @@ public class SymbolFamilies {
// Mappings between Starlark names and Starlark entities generated from the fakebuildapi.
private final ImmutableMap<String, Object> globals;
private final ImmutableMap<String, Object> bzlGlobals;
private final ImmutableMap<String, Object> moduleFileGlobals;

public SymbolFamilies(
StarlarkDocExpander expander,
Expand Down Expand Up @@ -66,6 +70,7 @@ public SymbolFamilies(
denyList));
this.globals = Starlark.UNIVERSE;
this.bzlGlobals = collectBzlGlobals(configuredRuleClassProvider);
this.moduleFileGlobals = collectModuleFileGlobals();
this.allDocPages = StarlarkDocumentationCollector.getAllDocPages(expander);
}

Expand All @@ -92,6 +97,14 @@ public Map<String, Object> getBzlGlobals() {
return bzlGlobals;
}

/*
* Returns a mapping between Starlark names and Starkark entities that are available only in MODULE.bazel
* files.
*/
public Map<String, Object> getModuleFileGlobals() {
return moduleFileGlobals;
}

// Returns a mapping between type names and module/type documentation.
public ImmutableMap<Category, ImmutableList<StarlarkDocPage>> getAllDocPages() {
return allDocPages;
Expand Down Expand Up @@ -132,10 +145,19 @@ private List<RuleDocumentation> collectNativeRules(
}

private ConfiguredRuleClassProvider createRuleClassProvider(String classProvider)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException,
throws NoSuchMethodException,
InvocationTargetException,
IllegalAccessException,
ClassNotFoundException {
Class<?> providerClass = Class.forName(classProvider);
Method createMethod = providerClass.getMethod("create");
return (ConfiguredRuleClassProvider) createMethod.invoke(null);
}

private ImmutableMap<String, Object> collectModuleFileGlobals() throws ClassNotFoundException {
Class<?> moduleFileGlobals =
Class.forName("com.google.devtools.build.lib.bazel.bzlmod.ModuleFileGlobals");
return Starlark.getMethodAnnotations(moduleFileGlobals).values().stream()
.collect(toImmutableMap(StarlarkMethod::name, entry -> entry));
}
}
1 change: 1 addition & 0 deletions src/main/protobuf/builtin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ enum ApiContext {
ALL = 0;
BZL = 1;
BUILD = 2;
MODULE = 3;
}

// Generic representation for a Starlark object. If the object is callable
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,15 @@ test_suite(
name = "others",
tags = ["-" + n for n in TEST_SUITES],
)

java_test(
name = "BuiltinProtoSmokeTest",
srcs = ["BuiltinProtoSmokeTest.java"],
data = ["//src/main/java/com/google/devtools/build/lib:gen_api_proto"],
env = {"BUILTIN_PROTO": "$(rlocationpath //src/main/java/com/google/devtools/build/lib:gen_api_proto)"},
deps = [
"//src/main/protobuf:builtin_java_proto",
"//third_party:truth",
"@bazel_tools//tools/java/runfiles",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.google.devtools.build.lib;

import static com.google.common.truth.Truth.assertThat;
import static java.util.stream.Collectors.toMap;

import com.google.devtools.build.docgen.builtin.BuiltinProtos;
import com.google.devtools.build.runfiles.Runfiles;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class BuiltinProtoSmokeTest {
static BuiltinProtos.Builtins builtins;

@BeforeClass
public static void loadProto() throws IOException {
Path protoPath =
Path.of(Runfiles.preload().unmapped().rlocation(System.getenv("BUILTIN_PROTO")));
try (InputStream inputStream = Files.newInputStream(protoPath);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
builtins = BuiltinProtos.Builtins.parseFrom(bufferedInputStream);
}
}

@Test
public void hasGlobalFromEachApiContext() {
assertThat(
builtins.getGlobalList().stream()
.collect(toMap(BuiltinProtos.Value::getName, BuiltinProtos.Value::getApiContext)))
.containsAtLeast(
"range", BuiltinProtos.ApiContext.ALL,
"glob", BuiltinProtos.ApiContext.BUILD,
"DefaultInfo", BuiltinProtos.ApiContext.BZL,
"bazel_dep", BuiltinProtos.ApiContext.MODULE);
}
}

0 comments on commit 1cf8845

Please sign in to comment.