-
-
Notifications
You must be signed in to change notification settings - Fork 722
Add buildInfo metadata support #4509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jsharpe
wants to merge
3
commits into
bazel-contrib:master
Choose a base branch
from
jsharpe:buildinfo-metadata
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,121
−41
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| filegroup( | ||
| name = "all_rules", | ||
| srcs = glob(["**/*.bzl"]), | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| filegroup( | ||
| name = "all_files", | ||
| testonly = True, | ||
| srcs = glob(["**"]), | ||
| visibility = ["//visibility:public"], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Aspect to collect package_info metadata for buildInfo. | ||
|
|
||
| This aspect traverses Go binary dependencies and collects version information | ||
| from Gazelle-generated package_info targets referenced via the package_metadata | ||
| common attribute (inherited from REPO.bazel default_package_metadata). | ||
|
|
||
| Implementation based on bazel-contrib/supply-chain gather_metadata pattern. | ||
| Currently doesn't use the supply chain tools dep for this as it is not yet | ||
| stable and we still need to support WORKSPACE which the supply-chain tools | ||
| doesn't have support for. | ||
| """ | ||
|
|
||
| load( | ||
| "//go/private:providers.bzl", | ||
| "GoArchive", | ||
| ) | ||
|
|
||
| visibility(["//go/private/..."]) | ||
|
|
||
| BuildInfoMetadata = provider( | ||
| doc = "INTERNAL: Provides dependency version metadata for buildInfo. Do not depend on this provider.", | ||
| fields = { | ||
| "importpaths": "Depset of importpath strings from Go dependencies", | ||
| "metadata_providers": "Depset of PackageInfo providers with version data", | ||
| }, | ||
| ) | ||
|
|
||
| def _buildinfo_aspect_impl(target, ctx): | ||
| """Collects package_info metadata from dependencies. | ||
|
|
||
| This aspect collects version information from package_metadata attributes | ||
| (set via REPO.bazel default_package_metadata in go_repository) and tracks | ||
| importpaths from Go dependencies. The actual version matching is deferred | ||
| to execution time to avoid quadratic memory usage. | ||
|
|
||
| Args: | ||
| target: The target being visited | ||
| ctx: The aspect context | ||
|
|
||
| Returns: | ||
| List containing BuildInfoMetadata provider | ||
| """ | ||
| direct_importpaths = [] | ||
| direct_metadata = [] | ||
| transitive_importpaths = [] | ||
| transitive_metadata = [] | ||
|
|
||
| # Collect importpath from this target if it's a Go target | ||
| if GoArchive in target: | ||
| importpath = target[GoArchive].data.importpath | ||
| if importpath: | ||
| direct_importpaths.append(importpath) | ||
|
|
||
| # Check package_metadata common attribute (Bazel 6+) | ||
| # This is set via REPO.bazel default_package_metadata in go_repository | ||
| if hasattr(ctx.rule.attr, "package_metadata"): | ||
| attr_value = ctx.rule.attr.package_metadata | ||
| if attr_value: | ||
| package_metadata = attr_value if type(attr_value) == type([]) else [attr_value] | ||
|
|
||
| # Store the metadata targets directly for later processing | ||
| direct_metadata.extend(package_metadata) | ||
|
|
||
| # Collect transitive metadata from Go dependencies only | ||
| # Only traverse deps and embed to avoid non-Go dependencies | ||
| for attr_name in ["deps", "embed"]: | ||
| if not hasattr(ctx.rule.attr, attr_name): | ||
| continue | ||
|
|
||
| attr_value = getattr(ctx.rule.attr, attr_name) | ||
| if not attr_value: | ||
| continue | ||
|
|
||
| deps = attr_value if type(attr_value) == type([]) else [attr_value] | ||
| for dep in deps: | ||
| # Collect transitive BuildInfoMetadata | ||
| if BuildInfoMetadata in dep: | ||
| transitive_importpaths.append(dep[BuildInfoMetadata].importpaths) | ||
| transitive_metadata.append(dep[BuildInfoMetadata].metadata_providers) | ||
|
|
||
| # Build depsets (empty depsets are efficient, no need for early return) | ||
| return [BuildInfoMetadata( | ||
| importpaths = depset( | ||
| direct = direct_importpaths, | ||
| transitive = transitive_importpaths, | ||
| ), | ||
| metadata_providers = depset( | ||
| direct = direct_metadata, | ||
| transitive = transitive_metadata, | ||
| ), | ||
| )] | ||
|
|
||
| buildinfo_aspect = aspect( | ||
| doc = "Collects package_info metadata for Go buildInfo", | ||
| implementation = _buildinfo_aspect_impl, | ||
| attr_aspects = ["deps", "embed"], # Only traverse Go dependencies | ||
| provides = [BuildInfoMetadata], | ||
| # Apply to generated targets including package_info | ||
| apply_to_generating_rules = True, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would think that we don't want this (e.g. don't traverse code generators that produce .go files in sources), but I may be missing a use case. |
||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we track info as
depsets (see comment above), we could materialize that into a file at execution time usingTemplateDictorArgs. I can take over that part if you want.