fix(go): honor build constraints when scanning package imports - #10
Merged
Conversation
ScanPackageImports parsed every .go file in a package directory, so a generator script kept next to library code under `//go:build ignore` (or `none`) contributed edges too. Such scripts are package main and routinely import the package they sit in (bor's core/mkalloc.go imports core), which the graph then reported as a self-import cycle and failed every PR touching that package. Filter ParseDir through go/build's MatchFile so only files the Go build would compile for the host platform are scanned, matching what the compiler actually links. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
cffls
marked this pull request as ready for review
September 2, 2026 23:02
pratikspatil024
approved these changes
Sep 3, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
ScanPackageImportsparsed every.gofile in a changed package's directory withparser.ParseDir, which ignores build constraints. A generator script kept next to library code under//go:build ignore///go:build noneispackage mainand typically imports the package it lives in — bor'score/mkalloc.goimportsgithub.com/ethereum/go-ethereum/core— so the graph gained acore -> coreedge anddetectCyclesreported a circular dependency. Any PR touching packagecoreroot in bor currently fails the Quality metrics gate on this (see 0xPolygon/bor#2383); PR #2373 only passed because it happened to rewritemkalloc.go.The scanner now filters
ParseDirthroughgo/build'sContext.MatchFile, so only files the Go build would compile for the host platform contribute edges — the graph reflects what the compiler actually links._testpackage and external-import handling is unchanged. One consequence worth knowing: files gated onGOOS/GOARCHare evaluated for the host running diffguard, so a cycle that exists only for another platform's files would not be reported on CI's Linux runners.Executed tests
make build,make test— all packages pass.TestScanPackageImports_HonorsBuildConstraints(table over//go:build ignoreand//go:build none): the generator script no longer produces a self-import edge; legitimate internal edges are still reported.go run ./cmd/diffguard -base origin/main .on this change: complexity, sizes, dependency structure, churn, dead code all PASS; mutation 100% (3/3).-base origin/develop, core/blockstm, core: exclude base-code senders from V2 nonce pre-compute; harden witness parity harness bor#2383): Dependency Structure goes from1 cycles [FAIL]to0 cycles [PASS]; all other sections unchanged.Rollout notes
Tooling only. bor's CI installs
github.com/0xPolygon/diffguard/cmd/diffguard@latest, so once this lands (and, if the module is versioned by tags, a new tag is cut) a plain re-run of the Quality metrics job on affected PRs passes with no changes to bor.🤖 Generated with Claude Code