RELNOTES[INC]: Bazel warns if a cc rule's includes attribute points out of third_party.
I'm confused that Bazel has the concept of third_party, but as long as it does, let's exploit it.
--
MOS_MIGRATED_REVID=119779306
diff --git a/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java b/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java
index ed2ad30..18e69a2 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/RuleClass.java
@@ -105,7 +105,7 @@
public static final Function<? super Rule, Map<String, Label>> NO_EXTERNAL_BINDINGS =
Functions.<Map<String, Label>>constant(ImmutableMap.<String, Label>of());
- private static final PathFragment THIRD_PARTY_PREFIX = new PathFragment("third_party");
+ public static final PathFragment THIRD_PARTY_PREFIX = new PathFragment("third_party");
/**
* A constraint for the package name of the Rule instances.
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
index 69763d4..979c645 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java
@@ -28,6 +28,7 @@
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.packages.BuildType;
+import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.rules.apple.Platform;
import com.google.devtools.build.lib.rules.cpp.CcToolchainFeatures.FeatureConfiguration;
import com.google.devtools.build.lib.rules.cpp.CppConfiguration.DynamicMode;
@@ -427,6 +428,16 @@
+ packageFragment
+ "'. This will be an error in the future");
// TODO(janakr): Add a link to a page explaining the problem and fixes?
+ } else if (!packageFragment.startsWith(RuleClass.THIRD_PARTY_PREFIX)) {
+ ruleContext.attributeWarning(
+ "includes",
+ "'"
+ + includesAttr
+ + "' resolves to '"
+ + includesPath
+ + "' not in '"
+ + RuleClass.THIRD_PARTY_PREFIX
+ + "'. This will be an error in the future");
}
result.add(includesPath);
result.add(ruleContext.getConfiguration().getGenfilesFragment().getRelative(includesPath));
diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonConfiguredTargetTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonConfiguredTargetTest.java
index abf2116..ca26134 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonConfiguredTargetTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CcCommonConfiguredTargetTest.java
@@ -506,31 +506,47 @@
@Test
public void testCcLibraryUplevelIncludesWarned() throws Exception {
checkWarning(
- "uplevel",
+ "third_party/uplevel",
"lib",
// message:
- "in includes attribute of cc_library rule //uplevel:lib: '../bar' resolves to 'bar' not "
- + "below the relative path of its package 'uplevel'. This will be an error in the "
- + "future",
+ "in includes attribute of cc_library rule //third_party/uplevel:lib: '../bar' resolves to "
+ + "'third_party/bar' not below the relative path of its package 'third_party/uplevel'. "
+ + "This will be an error in the future",
// build file:
+ "licenses(['unencumbered'])",
"cc_library(name = 'lib',",
" srcs = ['foo.cc'],",
" includes = ['../bar'])");
}
@Test
- public void testCcLibraryRootIncludesError() throws Exception {
- checkError(
- "root",
+ public void testCcLibraryNonThirdPartyIncludesWarned() throws Exception {
+ checkWarning(
+ "topdir",
"lib",
// message:
- "in includes attribute of cc_library rule //root:lib: '..' resolves to the workspace root, "
- + "which would allow this rule and all of its transitive dependents to include any "
- + "file in your workspace. Please include only what you need",
+ "in includes attribute of cc_library rule //topdir:lib: './' resolves to 'topdir' not "
+ + "in 'third_party'. This will be an error in the future",
// build file:
"cc_library(name = 'lib',",
" srcs = ['foo.cc'],",
- " includes = ['..'])");
+ " includes = ['./'])");
+ }
+
+ @Test
+ public void testCcLibraryRootIncludesError() throws Exception {
+ checkError(
+ "third_party/root",
+ "lib",
+ // message:
+ "in includes attribute of cc_library rule //third_party/root:lib: '../..' resolves to the "
+ + "workspace root, which would allow this rule and all of its transitive dependents to "
+ + "include any file in your workspace. Please include only what you need",
+ // build file:
+ "licenses(['unencumbered'])",
+ "cc_library(name = 'lib',",
+ " srcs = ['foo.cc'],",
+ " includes = ['../..'])");
}
@Test