From 86f734de006c03d1cabb71a1f08cf323cddf358b Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 4 Oct 2022 10:41:33 -0700 Subject: [PATCH] Clean up PackageSpecification stringification This CL gives us a pathway for fixing stringification of package_group's `packages` attribute, so that //foo/bar is printed as "//foo/bar" and not "foo/bar". This affects e.g. `bazel query --output=proto [...]`. This change is a prerequisite to adding support for "public" and "private" string constant package specifiers, since otherwise "public" would be ambiguous with the stringification of //public. PackageSpecification currently has two forms of stringification. The toString() method is unambiguous but omits the leading double slash, while the toStringWithoutRepository() always includes the double slash but never includes the repo name. I factored toString() into asString(boolean includeDoubleSlash) to switch between the old form with and without the fix to include slashes, and made toString() and all current callers pass false for now. I just renamed toStringWithoutRepository to asStringWithoutRepository for symmetry. Both are now clearly documented. In PackageGroupContents, I clarified its behavior w.r.t. duplicates and order, and renamed its member vars for simplicity and in anticipation of storing another type of package spec subclass in a follow-up CL. Also renamed some methods for clarity. Fixed a bug in AllPackagesBeneath where //... parsed in another repo would stringify without the leading "@repo". However, this bug was never exercised since it would require the follow-up CL's behavior of enabling "//..." to parse as AllPackagesBeneath rather than AllPackages (#16323). Added a test for all three forms of stringification to PackageGroupTest. There should be no actual behavioral change enabled in this CL. Work toward #11261. PiperOrigin-RevId: 478828269 Change-Id: Ifc7c08e6e90b89d33bed1ee6a5a97b3a3ac07326 --- .../build/lib/packages/PackageGroup.java | 7 +- .../lib/packages/PackageSpecification.java | 231 +++++++++++------- .../query/output/ProtoOutputFormatter.java | 4 +- .../query/output/XmlOutputFormatter.java | 6 +- .../build/lib/packages/PackageGroupTest.java | 66 ++++- 5 files changed, 219 insertions(+), 95 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/packages/PackageGroup.java b/src/main/java/com/google/devtools/build/lib/packages/PackageGroup.java index c1ee6960fe6fa7..ebad8d7076dafe 100644 --- a/src/main/java/com/google/devtools/build/lib/packages/PackageGroup.java +++ b/src/main/java/com/google/devtools/build/lib/packages/PackageGroup.java @@ -92,8 +92,11 @@ public List