Skip to content

Commit b3a8ef0

Browse files
brandjonaiuto
authored andcommitted
package_group: Add "public"/"private" constants, fix "//..." meaning
This CL does three things: 1. It adds the special package specifications "public" and "private, guarded behind the flag `--incompatible_package_group_has_public_syntax`. 2. It fixes the behavior of package specification "//..." to mean "all packages in the current repo" rather than "all packages in any repo", guarded behind the flag `--incompatible_fix_package_group_reporoot_syntax`. 3. It fixes the behavior of `bazel query --output=proto` (and `--output=xml`) so that the `packages` attribute is serialized without dropping the leading `//` from package names -- it is now `"//foo/bar/..."` instead of `"foo/bar/..."`. This behavioral change is guarded behind `--incompatible_package_group_includes_double_slash`. The .bzl `visibility()` builtin always acts as if the first two flags are enabled. Work toward bazelbuild#11261. Work toward bazelbuild#16323. Work toward bazelbuild#16355. Work toward bazelbuild#16391. RELNOTES: Added three `package_group`-related flags: `--incompatible_package_group_includes_double_slash` (bazelbuild#16391), `--incompatible_package_group_has_public_syntax` (bazelbuild#16355), and `--incompatible_fix_package_group_reporoot_syntax` (bazelbuild#16323). With these flags, `package_group` can now easily specify "all packages", "no packages", and "all packages in the current repo". In PackageSpecification: - fromString(), fromStringPositive(): rewrote docstring, added bool args for flags, renamed or removed vars for simplicity, added handling of public/private and flag behavior - AllPackagesBeneath(): fixed asStringWithoutRepository() for case of //..., which wasn't likely to arise before. - normalize order of equals(), hashCode(), etc. - AllPackages: change hash, and change str representation to "public" - Add NoPackages, symmetric to AllPackages - NegativePackageSpecification: don't reuse the delegate's hash as our own hash verbatim In PackageGroupTest: - Move `testEverythingSpecificationWorks` to above `testNegativeEverything`, and made it use "public" syntax in place of "//...". - Add test for "private" syntax. - Add test for flag guarding "public"/"private". - Add tests for old and new "//..." behavior. - Add tests that you can't negate "public" or "private". - Add test for illegal combination of flags. - Add assertions for stringification of "public"/"private". Other changes: - Update XmlOutputFormatterTest and ProtoOutputFormatterTest with test cases for the flag. In the latter case, parameterize a helper to allow setting per-test-case flags. - Add test cases to BzlLoadFunctionTest for bzl visibility accepting the list forms `["public"]`/`["private"]`, and for its behavior being unaffected by the new flags. PiperOrigin-RevId: 479347358 Change-Id: I124ca5406bff15615adaa1256e2d7bef69b9d9a5
1 parent 8eb5352 commit b3a8ef0

File tree

13 files changed

+591
-109
lines changed

13 files changed

+591
-109
lines changed

site/en/concepts/visibility.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ target.
4545
* Package groups use a different syntax for specifying packages. Within a
4646
package group, the forms `"//foo/bar:__pkg__"` and
4747
`"//foo/bar:__subpackages__"` are respectively replaced by `"//foo/bar"`
48-
and `"//foo/bar/..."`.
48+
and `"//foo/bar/..."`. Likewise, `"//visibility:public"` and
49+
`"//visibility:private"` are just `"public"` and `"private"`.
4950

5051
For example, if `//some/package:mytarget` has its `visibility` set to
5152
`[":__subpackages__", "//tests:__pkg__"]`, then it could be used by any target

src/main/java/com/google/devtools/build/docgen/templates/be/functions.vm

+24-6
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,18 @@ not themselves have any visibility protection.</p>
178178

179179
<li>As above, but with a trailing <code>/...</code>. For example, <code>
180180
//foo/...</code> specifies the set of <code>//foo</code> and all its
181-
subpackages. As a special case, <code>//...</code> specifies all
182-
packages in any repository (in other words, public).
181+
subpackages. <code>//...</code> specifies all packages in the current
182+
repository.
183+
184+
<li>The strings <code>public</code> or <code>private</code>, which
185+
respectively specify every package or no package. (This form requires
186+
the flag <code>--incompatible_package_group_has_public_syntax</code> to
187+
be set.)
183188

184189
</ol>
185190

186-
<p>In addition, a package specification may also be prefixed with
187-
<code>-</code> to indicate that it is negated.</p>
191+
<p>In addition, the first two kinds of package specifications may also
192+
be prefixed with <code>-</code> to indicate that they are negated.</p>
188193

189194
<p>The package group contains any package that matches at least one of
190195
its positive specifications and none of its negative specifications
@@ -193,11 +198,24 @@ not themselves have any visibility protection.</p>
193198
subpackages of <code>//foo/tests</code>. (<code>//foo</code> itself is
194199
included while </code>//foo/tests</code> itself is not.)</p>
195200

196-
<p>Aside from <code>//...</code>, there is no way to directly specify
201+
<p>Aside from public visibility, there is no way to directly specify
197202
packages outside the current repository.</p>
198203

199204
<p>If this attribute is missing, it is the same as setting it to an
200-
empty list (in other words, private).
205+
empty list, which is also the same as setting it to a list containing
206+
only <code>private</code>.
207+
208+
<p><i>Note:</i> The specification <code>//...</code> has a legacy behavior
209+
of being the same as <code>public</code>. This behavior is disabled when
210+
<code>--incompatible_fix_package_group_reporoot_syntax</code> is
211+
enabled.</p>
212+
213+
<p><i>Note:</i> As a legacy behavior, when this attribute is serialized as
214+
part of <code>bazel query --output=proto</code> (or
215+
<code>--output=xml</code>), the leading slashes are omitted if
216+
<code>--incompatible_package_group_includes_double_slash</code> is not
217+
enabled. For instance, <code>//pkg/foo/...</code> will output as
218+
<code>\"pkg/foo/...\"</code>.</p>
201219
</td>
202220
</tr>
203221
<tr>

src/main/java/com/google/devtools/build/lib/packages/Package.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -1652,14 +1652,26 @@ Label createLabel(String targetName) throws LabelSyntaxException {
16521652
return Label.create(pkg.getPackageIdentifier(), targetName);
16531653
}
16541654

1655-
/**
1656-
* Adds a package group to the package.
1657-
*/
1658-
void addPackageGroup(String name, Collection<String> packages, Collection<Label> includes,
1659-
EventHandler eventHandler, Location location)
1655+
/** Adds a package group to the package. */
1656+
void addPackageGroup(
1657+
String name,
1658+
Collection<String> packages,
1659+
Collection<Label> includes,
1660+
boolean allowPublicPrivate,
1661+
boolean repoRootMeansCurrentRepo,
1662+
EventHandler eventHandler,
1663+
Location location)
16601664
throws NameConflictException, LabelSyntaxException {
16611665
PackageGroup group =
1662-
new PackageGroup(createLabel(name), pkg, packages, includes, eventHandler, location);
1666+
new PackageGroup(
1667+
createLabel(name),
1668+
pkg,
1669+
packages,
1670+
includes,
1671+
allowPublicPrivate,
1672+
repoRootMeansCurrentRepo,
1673+
eventHandler,
1674+
location);
16631675
Target existing = targets.get(group.getName());
16641676
if (existing != null) {
16651677
throw nameConflict(group, existing);

src/main/java/com/google/devtools/build/lib/packages/PackageGroup.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public PackageGroup(
4747
Package pkg,
4848
Collection<String> packageSpecifications,
4949
Collection<Label> includes,
50+
boolean allowPublicPrivate,
51+
boolean repoRootMeansCurrentRepo,
5052
EventHandler eventHandler,
5153
Location location) {
5254
this.label = label;
@@ -61,7 +63,11 @@ public PackageGroup(
6163
PackageSpecification specification = null;
6264
try {
6365
specification =
64-
PackageSpecification.fromString(label.getRepository(), packageSpecification);
66+
PackageSpecification.fromString(
67+
label.getRepository(),
68+
packageSpecification,
69+
allowPublicPrivate,
70+
repoRootMeansCurrentRepo);
6571
} catch (PackageSpecification.InvalidPackageSpecificationException e) {
6672
errorsFound = true;
6773
eventHandler.handle(

0 commit comments

Comments
 (0)