Skip to content

fix(go): honor build constraints when scanning package imports - #10

Merged
cffls merged 1 commit into
mainfrom
jerry/go-deps-build-constraints
Sep 3, 2026
Merged

fix(go): honor build constraints when scanning package imports#10
cffls merged 1 commit into
mainfrom
jerry/go-deps-build-constraints

Conversation

@cffls

@cffls cffls commented Sep 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

ScanPackageImports parsed every .go file in a changed package's directory with parser.ParseDir, which ignores build constraints. A generator script kept next to library code under //go:build ignore / //go:build none is package main and typically imports the package it lives in — bor's core/mkalloc.go imports github.com/ethereum/go-ethereum/core — so the graph gained a core -> core edge and detectCycles reported a circular dependency. Any PR touching package core root in bor currently fails the Quality metrics gate on this (see 0xPolygon/bor#2383); PR #2373 only passed because it happened to rewrite mkalloc.go.

The scanner now filters ParseDir through go/build's Context.MatchFile, so only files the Go build would compile for the host platform contribute edges — the graph reflects what the compiler actually links. _test package and external-import handling is unchanged. One consequence worth knowing: files gated on GOOS/GOARCH are 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.
  • New TestScanPackageImports_HonorsBuildConstraints (table over //go:build ignore and //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).
  • Patched binary against the bor PR diff (-base origin/develop, core/blockstm, core: exclude base-code senders from V2 nonce pre-compute; harden witness parity harness bor#2383): Dependency Structure goes from 1 cycles [FAIL] to 0 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

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
cffls marked this pull request as ready for review September 2, 2026 23:02
@cffls
cffls merged commit 5f04c86 into main Sep 3, 2026
6 checks passed
@cffls
cffls deleted the jerry/go-deps-build-constraints branch September 3, 2026 02:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants