Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Bzlmod globals in builtin.proto #21929

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
32 changes: 30 additions & 2 deletions src/main/java/com/google/devtools/build/docgen/SymbolFamilies.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
import com.google.devtools.build.lib.util.Classpath.ClassPathException;
import com.google.devtools.build.skydoc.fakebuildapi.FakeStarlarkNativeModuleApi;
import com.google.devtools.build.skydoc.fakebuildapi.repository.FakeRepositoryModule;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -39,6 +40,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 All @@ -53,7 +55,8 @@ public SymbolFamilies(
IllegalAccessException,
BuildEncyclopediaDocException,
ClassNotFoundException,
IOException {
IOException,
InstantiationException {
ConfiguredRuleClassProvider configuredRuleClassProvider = createRuleClassProvider(provider);
this.nativeRules =
ImmutableList.copyOf(
Expand All @@ -66,6 +69,7 @@ public SymbolFamilies(
denyList));
this.globals = Starlark.UNIVERSE;
this.bzlGlobals = collectBzlGlobals(configuredRuleClassProvider);
this.moduleFileGlobals = collectModuleFileGlobals();
this.allDocPages = StarlarkDocumentationCollector.getAllDocPages(expander);
}

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

/*
* Returns a mapping between Starlark names and Starlark 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 All @@ -103,6 +115,7 @@ private ImmutableMap<String, Object> collectBzlGlobals(ConfiguredRuleClassProvid
// annotations, whereas the real "native" object is just a bare struct.
ImmutableMap.Builder<String, Object> env = ImmutableMap.builder();
env.put("native", new FakeStarlarkNativeModuleApi());
Starlark.addMethods(env, new FakeRepositoryModule(ImmutableList.of()));
for (Map.Entry<String, Object> entry :
provider.getBazelStarlarkEnvironment().getUninjectedBuildBzlEnv().entrySet()) {
if (entry.getKey().equals("native")) {
Expand Down Expand Up @@ -132,10 +145,25 @@ 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,
NoSuchMethodException,
InvocationTargetException,
InstantiationException,
IllegalAccessException {
Class<?> moduleFileGlobals =
Class.forName("com.google.devtools.build.lib.bazel.bzlmod.ModuleFileGlobals");
ImmutableMap.Builder<String, Object> moduleFileEnv = ImmutableMap.builder();
Starlark.addMethods(moduleFileEnv, moduleFileGlobals.getConstructor().newInstance());
return moduleFileEnv.buildOrThrow();
}
}
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,46 @@
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 wellKnownCallablesAreDocumented() {
assertThat(
builtins.getGlobalList().stream()
.filter(BuiltinProtos.Value::hasCallable)
.filter(global -> !global.getCallable().getParamList().isEmpty())
.collect(toMap(BuiltinProtos.Value::getName, BuiltinProtos.Value::getApiContext)))
.containsAtLeast(
"range", BuiltinProtos.ApiContext.ALL,
"glob", BuiltinProtos.ApiContext.BUILD,
"DefaultInfo", BuiltinProtos.ApiContext.BZL,
"module_extension", BuiltinProtos.ApiContext.BZL,
"bazel_dep", BuiltinProtos.ApiContext.MODULE);
}
}