Skip to content

Commit

Permalink
Add incompatible flag to forbid loading the native Protobuf rules
Browse files Browse the repository at this point in the history
RELNOTES: --incompatible_load_proto_rules_from_bzl was added to forbid loading the native proto rules directly. See more on tracking issue bazelbuild#8922
  • Loading branch information
Yannic committed Jul 18, 2019
1 parent ed0f7d0 commit bbe86e8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
public class BazelProtoLibrary implements RuleConfiguredTargetFactory {

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws ActionConflictException {
public ConfiguredTarget create(RuleContext ruleContext)
throws ActionConflictException, RuleErrorException {
ProtoCommon.checkRuleHasValidMigrationTag(ruleContext);
ProtoInfo protoInfo =
ProtoCommon.createProtoInfo(
ruleContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public Metadata getMetadata() {

/*<!-- #BLAZE_RULE (NAME = proto_library, TYPE = LIBRARY, FAMILY = Protocol Buffer) -->
<p>Deprecated. Please use <a href="https://github.com/bazelbuild/rules_proto">
https://github.com/bazelbuild/rules_proto</a> instead.
</p>
<p>Use <code>proto_library</code> to define libraries of protocol buffers
which may be used from multiple languages. A <code>proto_library</code> may be listed
in the <code>deps</code> clause of supported rules, such as <code>java_proto_library</code>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.PathFragment;
Expand Down Expand Up @@ -460,6 +462,28 @@ private static boolean isConfiguredTargetInSamePackage(RuleContext ruleContext,
return ruleContext.getLabel().getPackageIdentifier().equals(source.getPackageIdentifier());
}

public static void checkRuleHasValidMigrationTag(RuleContext ruleContext)
throws RuleErrorException {
if (!ruleContext.getFragment(ProtoConfiguration.class).loadProtoRulesFromBzl()) {
return;
}

if (!hasValidMigrationTag(ruleContext)) {
ruleContext.ruleError(
"The native Protobuf rules are deprecated. Please load "
+ ruleContext.getRule().getRuleClass()
+ " from the rules_proto repository."
+ " See http://github.com/bazelbuild/rules_proto.");
}
}

private static boolean hasValidMigrationTag(RuleContext ruleContext) {
return ruleContext
.attributes()
.get("tags", Type.STRING_LIST)
.contains("__PROTO_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__");
}

/**
* Creates the {@link ProtoInfo} for the {@code proto_library} rule associated with {@code
* ruleContext}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,26 @@ public static class Options extends FragmentOptions {
+ "been superseded by the strip_import_prefix= and import_prefix= attributes")
public boolean disableProtoSourceRoot;

@Option(
name = "incompatible_load_proto_rules_from_bzl",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {
OptionMetadataTag.INCOMPATIBLE_CHANGE,
OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES
},
help =
"If enabled, direct usage of the native Protobuf rules is disabled. Please use "
+ "the Starlark rules instead https://github.com/bazelbuild/rules_proto")
public boolean loadProtoRulesFromBzl;

@Override
public FragmentOptions getHost() {
Options host = (Options) super.getHost();
host.disableLegacyProvider = disableLegacyProvider;
host.disableProtoSourceRoot = disableProtoSourceRoot;
host.loadProtoRulesFromBzl = loadProtoRulesFromBzl;
host.protoCompiler = protoCompiler;
host.protocOpts = protocOpts;
host.experimentalProtoExtraActions = experimentalProtoExtraActions;
Expand Down Expand Up @@ -319,4 +334,8 @@ public boolean doNotUseBuggyImportPath() {
public boolean generatedProtosInVirtualImports() {
return options.generatedProtosInVirtualImports;
}

public boolean loadProtoRulesFromBzl() {
return options.loadProtoRulesFromBzl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ProtoLangToolchain implements RuleConfiguredTargetFactory {
@Override
public ConfiguredTarget create(RuleContext ruleContext)
throws InterruptedException, RuleErrorException, ActionConflictException {
ProtoCommon.checkRuleHasValidMigrationTag(ruleContext);
NestedSetBuilder<Artifact> blacklistedProtos = NestedSetBuilder.stableOrder();
for (TransitiveInfoCollection protos :
ruleContext.getPrerequisites("blacklisted_protos", TARGET)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public Metadata getMetadata() {

/*<!-- #BLAZE_RULE (NAME = proto_lang_toolchain, TYPE = LIBRARY, FAMILY = Protocol Buffer) -->
<p>Deprecated. Please <a href="https://github.com/bazelbuild/rules_proto">
https://github.com/bazelbuild/rules_proto</a> instead.
</p>
<p>
Specifies how a LANG_proto_library rule (e.g., <code>java_proto_library</code>) should invoke the
proto-compiler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;

/** Info object propagating information about protocol buffer sources. */
@SkylarkModule(name = "ProtoInfo", doc = "")
@SkylarkModule(
name = "ProtoInfo",
doc = "Encapsulates information provided by <a href=\""
+ "../../be/protocol-buffer.html#proto_library\">proto_library.</a>"
+ "Please consider using `load(\"@rules_proto//proto:defs.bzl\", \"ProtoInfo\")` "
+ "to load this symbol from <a href=\"https://github.com/bazelbuild/rules_proto.\">"
+ "rules_proto</a>")
public interface ProtoInfoApi<FileT extends FileApi> extends StructApi {
/** Provider class for {@link ProtoInfoApi} objects. */
@SkylarkModule(name = "Provider", documented = false, doc = "")
Expand Down

0 comments on commit bbe86e8

Please sign in to comment.