Skip to content

Commit

Permalink
proto_lang_toolchain: Add original sources of ProtoInfo to blackliste…
Browse files Browse the repository at this point in the history
…dProtos

Fixes bazelbuild#10484

Closes bazelbuild#10493.

PiperOrigin-RevId: 289411825
  • Loading branch information
Yannic committed Jan 15, 2020
1 parent b74cf30 commit 66c6012
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 27 deletions.
Expand Up @@ -276,7 +276,7 @@ private static class Impl {
private boolean areSrcsBlacklisted() {
return !new ProtoSourceFileBlacklist(
ruleContext, getProtoToolchainProvider().blacklistedProtos())
.checkSrcs(protoInfo.getOriginalDirectProtoSources(), "cc_proto_library");
.checkSrcs(protoInfo.getOriginalTransitiveProtoSources(), "cc_proto_library");
}

private FeatureConfiguration getFeatureConfiguration() {
Expand Down
Expand Up @@ -127,6 +127,19 @@ private static NestedSet<Artifact> computeTransitiveProtoSources(
return result.build();
}

private static NestedSet<Artifact> computeTransitiveOriginalProtoSources(
RuleContext ruleContext, ImmutableList<Artifact> originalProtoSources) {
NestedSetBuilder<Artifact> result = NestedSetBuilder.naiveLinkOrder();

result.addAll(originalProtoSources);

for (ProtoInfo dep : ruleContext.getPrerequisites("deps", Mode.TARGET, ProtoInfo.PROVIDER)) {
result.addTransitive(dep.getOriginalTransitiveProtoSources());
}

return result.build();
}

static NestedSet<Artifact> computeDependenciesDescriptorSets(RuleContext ruleContext) {
NestedSetBuilder<Artifact> result = NestedSetBuilder.stableOrder();

Expand Down Expand Up @@ -462,6 +475,8 @@ public static ProtoInfo createProtoInfo(

NestedSet<Artifact> transitiveProtoSources =
computeTransitiveProtoSources(ruleContext, library.getSources());
NestedSet<Artifact> transitiveOriginalProtoSources =
computeTransitiveOriginalProtoSources(ruleContext, directProtoSources);
NestedSet<String> transitiveProtoSourceRoots =
computeTransitiveProtoSourceRoots(ruleContext, library.getSourceRoot());

Expand Down Expand Up @@ -491,8 +506,8 @@ public static ProtoInfo createProtoInfo(
ProtoInfo protoInfo =
new ProtoInfo(
library.getSources(),
directProtoSources,
library.getSourceRoot(),
transitiveOriginalProtoSources,
transitiveProtoSources,
transitiveProtoSourceRoots,
strictImportableProtosForDependents,
Expand Down
Expand Up @@ -54,7 +54,7 @@ public Provider() {
public static final String LEGACY_SKYLARK_NAME = "proto";

private final ImmutableList<Artifact> directProtoSources;
private final ImmutableList<Artifact> originalDirectProtoSources;
private final NestedSet<Artifact> originalTransitiveProtoSources;
private final String directProtoSourceRoot;
private final NestedSet<Artifact> transitiveProtoSources;
private final NestedSet<String> transitiveProtoSourceRoots;
Expand All @@ -71,8 +71,8 @@ public Provider() {
@AutoCodec.Instantiator
public ProtoInfo(
ImmutableList<Artifact> directProtoSources,
ImmutableList<Artifact> originalDirectProtoSources,
String directProtoSourceRoot,
NestedSet<Artifact> originalTransitiveProtoSources,
NestedSet<Artifact> transitiveProtoSources,
NestedSet<String> transitiveProtoSourceRoots,
NestedSet<Artifact> strictImportableProtoSourcesForDependents,
Expand All @@ -86,7 +86,7 @@ public ProtoInfo(
Location location) {
super(PROVIDER, location);
this.directProtoSources = directProtoSources;
this.originalDirectProtoSources = originalDirectProtoSources;
this.originalTransitiveProtoSources = originalTransitiveProtoSources;
this.directProtoSourceRoot = directProtoSourceRoot;
this.transitiveProtoSources = transitiveProtoSources;
this.transitiveProtoSourceRoots = transitiveProtoSourceRoots;
Expand All @@ -103,17 +103,18 @@ public ProtoInfo(

/**
* The proto source files that are used in compiling this {@code proto_library}.
*
* <p>Different from {@link #getOriginalDirectProtoSources()} when a virtual import root is used.
*/
@Override
public ImmutableList<Artifact> getDirectProtoSources() {
return directProtoSources;
}

/** The proto sources of the {@code proto_library} declaring this provider. */
public ImmutableList<Artifact> getOriginalDirectProtoSources() {
return originalDirectProtoSources;
/**
* The non-virtual transitive proto source files. Different from {@link
* #getTransitiveProtoSources()} if a transitive dependency has {@code import_prefix} or the like.
*/
public NestedSet<Artifact> getOriginalTransitiveProtoSources() {
return originalTransitiveProtoSources;
}

/** The source root of the current library. */
Expand Down
Expand Up @@ -44,8 +44,10 @@ public ConfiguredTarget create(RuleContext ruleContext)
ProtoInfo protoInfo = protos.get(ProtoInfo.PROVIDER);
// TODO(cushon): it would be nice to make this mandatory and stop adding files to build too
if (protoInfo != null) {
blacklistedProtos.addTransitive(protoInfo.getTransitiveProtoSources());
blacklistedProtos.addTransitive(protoInfo.getOriginalTransitiveProtoSources());
} else {
// Only add files from FileProvider if |protos| is not a proto_library to avoid adding
// the descriptor_set of proto_library to the list of blacklisted files.
blacklistedProtos.addTransitive(protos.getProvider(FileProvider.class).getFilesToBuild());
}
}
Expand Down
Expand Up @@ -65,8 +65,8 @@ private ProtoInfo protoInfo(
NestedSet<Pair<Artifact, String>> exportedProtos) {
return new ProtoInfo(
directProtos,
directProtos,
"",
/* directProtoSourceRoot= */ "",
transitiveProtos,
transitiveProtos,
transitiveProtoSourceRoots,
/* strictImportableProtosForDependents */ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
Expand Down
Expand Up @@ -39,10 +39,27 @@ public void setUp() throws Exception {
invalidatePackages();
}

private void validateProtoLangToolchain(ProtoLangToolchainProvider toolchain) throws Exception {
assertThat(toolchain.commandLine()).isEqualTo("cmd-line");
assertThat(toolchain.pluginExecutable().getExecutable().getRootRelativePathString())
.isEqualTo("third_party/x/plugin");

TransitiveInfoCollection runtimes = toolchain.runtime();
assertThat(runtimes.getLabel())
.isEqualTo(Label.parseAbsolute("//third_party/x:runtime", ImmutableMap.of()));

assertThat(prettyArtifactNames(toolchain.blacklistedProtos()))
.containsExactly(
"third_party/x/metadata.proto",
"third_party/x/descriptor.proto",
"third_party/x/any.proto");
}

@Test
public void protoToolchain() throws Exception {
scratch.file(
"x/BUILD",
"third_party/x/BUILD",
"licenses(['unencumbered'])",
"cc_binary(name = 'plugin', srcs = ['plugin.cc'])",
"cc_library(name = 'runtime', srcs = ['runtime.cc'])",
"filegroup(name = 'descriptors', srcs = ['metadata.proto', 'descriptor.proto'])",
Expand All @@ -52,29 +69,82 @@ public void protoToolchain() throws Exception {
scratch.file(
"foo/BUILD",
TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
"licenses(['unencumbered'])",
"proto_lang_toolchain(",
" name = 'toolchain',",
" command_line = 'cmd-line',",
" plugin = '//x:plugin',",
" runtime = '//x:runtime',",
" blacklisted_protos = ['//x:blacklist']",
" plugin = '//third_party/x:plugin',",
" runtime = '//third_party/x:runtime',",
" blacklisted_protos = ['//third_party/x:blacklist']",
")");

update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());

ProtoLangToolchainProvider toolchain =
getConfiguredTarget("//foo:toolchain").getProvider(ProtoLangToolchainProvider.class);
validateProtoLangToolchain(
getConfiguredTarget("//foo:toolchain").getProvider(ProtoLangToolchainProvider.class));
}

assertThat(toolchain.commandLine()).isEqualTo("cmd-line");
assertThat(toolchain.pluginExecutable().getExecutable().getRootRelativePathString())
.isEqualTo("x/plugin");
@Test
public void protoToolchainBlacklistProtoLibraries() throws Exception {
scratch.file(
"third_party/x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"licenses(['unencumbered'])",
"cc_binary(name = 'plugin', srcs = ['plugin.cc'])",
"cc_library(name = 'runtime', srcs = ['runtime.cc'])",
"proto_library(name = 'descriptors', srcs = ['metadata.proto', 'descriptor.proto'])",
"proto_library(name = 'any', srcs = ['any.proto'], strip_import_prefix = '/third_party')");

TransitiveInfoCollection runtimes = toolchain.runtime();
assertThat(runtimes.getLabel())
.isEqualTo(Label.parseAbsolute("//x:runtime", ImmutableMap.of()));
scratch.file(
"foo/BUILD",
TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
"proto_lang_toolchain(",
" name = 'toolchain',",
" command_line = 'cmd-line',",
" plugin = '//third_party/x:plugin',",
" runtime = '//third_party/x:runtime',",
" blacklisted_protos = ['//third_party/x:descriptors', '//third_party/x:any']",
")");

assertThat(prettyArtifactNames(toolchain.blacklistedProtos()))
.containsExactly("x/metadata.proto", "x/descriptor.proto", "x/any.proto");
update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());

validateProtoLangToolchain(
getConfiguredTarget("//foo:toolchain").getProvider(ProtoLangToolchainProvider.class));
}

@Test
public void protoToolchainMixedBlacklist() throws Exception {
scratch.file(
"third_party/x/BUILD",
TestConstants.LOAD_PROTO_LIBRARY,
"licenses(['unencumbered'])",
"cc_binary(name = 'plugin', srcs = ['plugin.cc'])",
"cc_library(name = 'runtime', srcs = ['runtime.cc'])",
"proto_library(name = 'metadata', srcs = ['metadata.proto'])",
"proto_library(",
" name = 'descriptor',",
" srcs = ['descriptor.proto'],",
" strip_import_prefix = '/third_party')",
"filegroup(name = 'any', srcs = ['any.proto'])");

scratch.file(
"foo/BUILD",
TestConstants.LOAD_PROTO_LANG_TOOLCHAIN,
"proto_lang_toolchain(",
" name = 'toolchain',",
" command_line = 'cmd-line',",
" plugin = '//third_party/x:plugin',",
" runtime = '//third_party/x:runtime',",
" blacklisted_protos = [",
" '//third_party/x:metadata',",
" '//third_party/x:descriptor',",
" '//third_party/x:any']",
")");

update(ImmutableList.of("//foo:toolchain"), false, 1, true, new EventBus());

validateProtoLangToolchain(
getConfiguredTarget("//foo:toolchain").getProvider(ProtoLangToolchainProvider.class));
}

@Test
Expand Down

0 comments on commit 66c6012

Please sign in to comment.