feat: support DROP sentinel in gomod replace to remove orphaned requires#1086
feat: support DROP sentinel in gomod replace to remove orphaned requires#1086chrischangcode wants to merge 1 commit into
Conversation
0069362 to
aa75591
Compare
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
This PR adds support for dropping module requirements from go.mod via the existing replace directive mechanism, using a special sentinel value "DROP" as the replacement target to convert entries into -droprequire= arguments for go mod edit.
Changes:
- Introduces a
DropRequireSentinelconstant and logic ingomodEditArgsto convert replace directives targeting"DROP"into-droprequire=arguments. - Updates documentation for
gomodEditArgsto describe the new drop behavior. - Adds comprehensive test cases covering the drop-require feature, including case-insensitivity, mixed scenarios, and error handling.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| preprocess.go | Adds the DropRequireSentinel constant, updates gomodEditArgs to handle the drop sentinel, and adds documentation. |
| generator_gomod_test.go | Adds table-driven tests for the new drop-require functionality. |
|
Why isn't |
|
Ok I think I understand now. |
Agreed! But yeah upstream go.mod required the deprecated package. https://github.com/kubernetes/kube-state-metrics/blob/release-2.16/go.mod#L241 I also think it'd be possible to add logic to calculate the replaces with more context of the overall dependency graph for more successful PRs. |
aa75591 to
70cf052
Compare
Add a dedicated Drop field to GomodEdits that emits go mod edit -droprequire
arguments. This handles cases where a sub-module has been absorbed into its
parent module in a newer version (e.g. google.golang.org/grpc/stats/opentelemetry
merged into google.golang.org/grpc at v1.79.0).
Spec syntax:
sources:
my-source:
generate:
- gomod:
edits:
replace:
- google.golang.org/grpc => google.golang.org/grpc@v1.79.3
drop:
- google.golang.org/grpc/stats/opentelemetry
Addresses reviewer feedback to use a dedicated field rather than overloading
the Replace field with a DROP sentinel value.
Signed-off-by: Chris Chang <chrischang@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
70cf052 to
ecedb84
Compare
Problem
When a Go sub-module gets absorbed into its parent module in a newer version, upgrading the parent via a
replacedirective leaves an orphanedrequireingo.modthat can no longer be resolved.Real-world example:
google.golang.org/grpc/stats/opentelemetrywas a separate module (with its owngo.mod) up through grpc v1.68.x. Starting with grpc v1.79.0, it was merged into the maingoogle.golang.org/grpcmodule — the code still exists as a package, but there is no longer a separatego.modat that path.When a CVE fix upgrades
google.golang.org/grpcto v1.79.3 via a replace directive,go mod tidyfails because it cannot resolve the orphanedrequire google.golang.org/grpc/stats/opentelemetry v0.0.0-...— no module exists at that path in the newer version.Current workaround
Spec authors must create a source patch that adds a stub
go.modfile and a local directory replace pointing to it. This works but adds maintenance burden.See Azure/dalec-build-defs#9887 for an example.
Solution
Add a dedicated
Dropfield toGomodEditsthat emitsgo mod edit -droprequire=<module>arguments. The subsequentgo mod tidythen cleanly resolves the dependency graph.Spec syntax
Implementation
spec.go: AddedDrop []stringfield toGomodEditsstruct with documentation.preprocess.go: UpdatedgomodEditArgs()to iterateDropentries and emit-droprequire=arguments.generator_gomod_test.go: Added tests covering: basic drop, multiple drops, mixed drop+replace, empty-path error, and nil-edits passthrough.gomod-patch.sh: No changes needed — the shell script already iterates edit args line-by-line and passes each togo mod edit, which natively supports-droprequire=.Why a dedicated
Dropfield?Per reviewer feedback, a dedicated field is preferable to overloading
Replacewith a sentinel value:drop:is unambiguous; readers don't need to know about magic values[]stringfor module paths, no struct neededVerified
Tested locally against
kubernetes/kube-state-metricsv2.16.0:All existing tests pass (
go test ./...).