Created
August 3, 2025 10:23
-
-
Save albertocavalcante/cbb382a52459ba86268d664795667ceb to your computer and use it in GitHub Desktop.
Aspect Go Toolchain Discovery
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
| { | |
| "debug": { | |
| "accessing_toolchains": true, | |
| "ctx_type": "ctx", | |
| "go_toolchain_dir": [ | |
| "actions", | |
| "cross_compile", | |
| "default_goarch", | |
| "default_goos", | |
| "flags", | |
| "name", | |
| "sdk" | |
| ], | |
| "go_toolchain_found": true, | |
| "go_toolchain_type": "ToolchainInfo", | |
| "has_toolchains": true, | |
| "sdk_dir": [ | |
| "experiments", | |
| "go", | |
| "goarch", | |
| "goos", | |
| "headers", | |
| "libs", | |
| "package_list", | |
| "root_file", | |
| "srcs", | |
| "tools", | |
| "version" | |
| ], | |
| "target_kind": "go_binary", | |
| "target_label": "@@//cmd/garf:garf" | |
| }, | |
| "repositories": [ | |
| "rules_go" | |
| ], | |
| "toolchains": { | |
| "@rules_go//go:toolchain": { | |
| "cross_compile": "False", | |
| "sdk_goarch": "arm64", | |
| "sdk_goos": "darwin", | |
| "sdk_label": "unknown", | |
| "sdk_root_file": "<source file ROOT>" | |
| } | |
| } | |
| } |
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
| ToolchainInfo = provider( | |
| fields = { | |
| "toolchains": "Dict of toolchain_type -> resolved toolchain info", | |
| "repositories": "Set of external repository names", | |
| } | |
| ) | |
| def _toolchain_discovery_experiment_aspect_impl(target, ctx): | |
| """Simplified aspect to experiment with toolchain discovery""" | |
| toolchains = {} | |
| repositories = [] | |
| # Collect toolchain info from current target | |
| debug_info = {} | |
| debug_info["has_toolchains"] = hasattr(ctx, "toolchains") | |
| debug_info["target_label"] = str(target.label) | |
| debug_info["ctx_type"] = str(type(ctx)) | |
| debug_info["target_kind"] = ctx.rule.kind if hasattr(ctx, "rule") and hasattr(ctx.rule, "kind") else "unknown" | |
| if hasattr(ctx, "toolchains"): | |
| debug_info["accessing_toolchains"] = True | |
| # Access the Go toolchain directly with bracket notation | |
| go_toolchain = ctx.toolchains["@rules_go//go:toolchain"] | |
| if go_toolchain: | |
| debug_info["go_toolchain_found"] = True | |
| debug_info["go_toolchain_type"] = str(type(go_toolchain)) | |
| # List all attributes available on the toolchain | |
| debug_info["go_toolchain_dir"] = [attr for attr in dir(go_toolchain) if not attr.startswith("_")] | |
| # Extract Go toolchain information | |
| toolchain_info = {} | |
| # Access standard toolchain fields | |
| if hasattr(go_toolchain, "sdk"): | |
| sdk = go_toolchain.sdk | |
| debug_info["sdk_dir"] = [attr for attr in dir(sdk) if not attr.startswith("_")] | |
| toolchain_info["sdk_label"] = str(sdk.label) if hasattr(sdk, "label") else "unknown" | |
| toolchain_info["sdk_root_file"] = str(sdk.root_file) if hasattr(sdk, "root_file") else "unknown" | |
| toolchain_info["sdk_goos"] = str(sdk.goos) if hasattr(sdk, "goos") else "unknown" | |
| toolchain_info["sdk_goarch"] = str(sdk.goarch) if hasattr(sdk, "goarch") else "unknown" | |
| # Access toolchain-specific fields | |
| if hasattr(go_toolchain, "goos"): | |
| toolchain_info["toolchain_goos"] = str(go_toolchain.goos) | |
| if hasattr(go_toolchain, "goarch"): | |
| toolchain_info["toolchain_goarch"] = str(go_toolchain.goarch) | |
| if hasattr(go_toolchain, "cross_compile"): | |
| toolchain_info["cross_compile"] = str(go_toolchain.cross_compile) | |
| if hasattr(go_toolchain, "cgo_enabled"): | |
| toolchain_info["cgo_enabled"] = str(go_toolchain.cgo_enabled) | |
| toolchains["@rules_go//go:toolchain"] = toolchain_info | |
| repositories.append("rules_go") | |
| else: | |
| debug_info["go_toolchain_found"] = False | |
| debug_info["go_toolchain_is_none"] = True | |
| else: | |
| debug_info["accessing_toolchains"] = False | |
| # Write debug info to manifest file always for debugging | |
| label_safe = str(target.label).replace("//", "_").replace(":", "_").replace("@", "_").replace("/", "_") | |
| manifest_file = ctx.actions.declare_file("toolchain_manifest_%s.json" % label_safe) | |
| ctx.actions.write( | |
| output = manifest_file, | |
| content = json.encode({ | |
| "debug": debug_info, | |
| "toolchains": toolchains, | |
| "repositories": repositories, | |
| }), | |
| ) | |
| return [ | |
| ToolchainInfo( | |
| toolchains = toolchains, | |
| repositories = repositories, | |
| ), | |
| OutputGroupInfo( | |
| toolchain_discovery_experiment_aspect = depset([manifest_file]), | |
| ), | |
| DefaultInfo(files = depset([manifest_file])), | |
| ] | |
| toolchain_discovery_experiment_aspect = aspect( | |
| implementation = _toolchain_discovery_experiment_aspect_impl, | |
| attr_aspects = ["*"], | |
| toolchains = [ | |
| "@rules_go//go:toolchain", | |
| ], | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment