From b3a8ef0d211305bcfaeccd1058e7b5db780415ae Mon Sep 17 00:00:00 2001 From: Googler Date: Thu, 6 Oct 2022 10:22:52 -0700 Subject: [PATCH] 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 #11261. Work toward #16323. Work toward #16355. Work toward #16391. RELNOTES: Added three `package_group`-related flags: `--incompatible_package_group_includes_double_slash` (#16391), `--incompatible_package_group_has_public_syntax` (#16355), and `--incompatible_fix_package_group_reporoot_syntax` (#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 --- site/en/concepts/visibility.md | 3 +- .../build/docgen/templates/be/functions.vm | 30 ++- .../devtools/build/lib/packages/Package.java | 24 +- .../build/lib/packages/PackageGroup.java | 8 +- .../lib/packages/PackageSpecification.java | 247 +++++++++++++----- .../lib/packages/StarlarkNativeModule.java | 13 +- .../semantics/BuildLanguageOptions.java | 35 +++ .../lib/query2/common/CommonQueryOptions.java | 11 + .../query/output/ProtoOutputFormatter.java | 5 +- .../query/output/XmlOutputFormatter.java | 5 +- .../PackageGroupStaticInitializationTest.java | 7 +- .../build/lib/packages/PackageGroupTest.java | 205 +++++++++++++-- .../lib/skyframe/BzlLoadFunctionTest.java | 107 ++++++++ 13 files changed, 591 insertions(+), 109 deletions(-) diff --git a/site/en/concepts/visibility.md b/site/en/concepts/visibility.md index 89efcfbd6d4ba3..596e838628ec60 100644 --- a/site/en/concepts/visibility.md +++ b/site/en/concepts/visibility.md @@ -45,7 +45,8 @@ target. * Package groups use a different syntax for specifying packages. Within a package group, the forms `"//foo/bar:__pkg__"` and `"//foo/bar:__subpackages__"` are respectively replaced by `"//foo/bar"` - and `"//foo/bar/..."`. + and `"//foo/bar/..."`. Likewise, `"//visibility:public"` and + `"//visibility:private"` are just `"public"` and `"private"`. For example, if `//some/package:mytarget` has its `visibility` set to `[":__subpackages__", "//tests:__pkg__"]`, then it could be used by any target diff --git a/src/main/java/com/google/devtools/build/docgen/templates/be/functions.vm b/src/main/java/com/google/devtools/build/docgen/templates/be/functions.vm index 164ece718cf14d..c8c64fd2aea3aa 100644 --- a/src/main/java/com/google/devtools/build/docgen/templates/be/functions.vm +++ b/src/main/java/com/google/devtools/build/docgen/templates/be/functions.vm @@ -178,13 +178,18 @@ not themselves have any visibility protection.

  • As above, but with a trailing /.... For example, //foo/... specifies the set of //foo and all its - subpackages. As a special case, //... specifies all - packages in any repository (in other words, public). + subpackages. //... specifies all packages in the current + repository. + +
  • The strings public or private, which + respectively specify every package or no package. (This form requires + the flag --incompatible_package_group_has_public_syntax to + be set.) -

    In addition, a package specification may also be prefixed with - - to indicate that it is negated.

    +

    In addition, the first two kinds of package specifications may also + be prefixed with - to indicate that they are negated.

    The package group contains any package that matches at least one of its positive specifications and none of its negative specifications @@ -193,11 +198,24 @@ not themselves have any visibility protection.

    subpackages of //foo/tests. (//foo itself is included while //foo/tests itself is not.)

    -

    Aside from //..., there is no way to directly specify +

    Aside from public visibility, there is no way to directly specify packages outside the current repository.

    If this attribute is missing, it is the same as setting it to an - empty list (in other words, private). + empty list, which is also the same as setting it to a list containing + only private. + +

    Note: The specification //... has a legacy behavior + of being the same as public. This behavior is disabled when + --incompatible_fix_package_group_reporoot_syntax is + enabled.

    + +

    Note: As a legacy behavior, when this attribute is serialized as + part of bazel query --output=proto (or + --output=xml), the leading slashes are omitted if + --incompatible_package_group_includes_double_slash is not + enabled. For instance, //pkg/foo/... will output as + \"pkg/foo/...\".

    diff --git a/src/main/java/com/google/devtools/build/lib/packages/Package.java b/src/main/java/com/google/devtools/build/lib/packages/Package.java index f9dec2a7979f18..a73a3d38f20d15 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/Package.java +++ b/src/main/java/com/google/devtools/build/lib/packages/Package.java @@ -1652,14 +1652,26 @@ Label createLabel(String targetName) throws LabelSyntaxException { return Label.create(pkg.getPackageIdentifier(), targetName); } - /** - * Adds a package group to the package. - */ - void addPackageGroup(String name, Collection packages, Collection