Skip to content

fix(go): handle replace directives in go list -m all parsing#468

Merged
ruromero merged 3 commits into
guacsec:mainfrom
a-oren:TC-4359
May 12, 2026
Merged

fix(go): handle replace directives in go list -m all parsing#468
ruromero merged 3 commits into
guacsec:mainfrom
a-oren:TC-4359

Conversation

@a-oren

@a-oren a-oren commented May 12, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fix go list -m all parsing in GoModulesProvider.getFinalPackagesVersionsForModule() to handle Go replace directives (5-part format: name v1 => replacement v2)
  • Extract parseModuleVersions() method mirroring JS client PR build(deps): bump org.pitest:pitest-maven from 1.25.1 to 1.25.4 #505
  • Add replace directive test fixture to go_mod_light_no_ignore and update expected SBOM

Implements TC-4359

Test plan

  • All 18 Go module tests pass (all 6 parameterized folders, both stack and component analysis)
  • go_mod_no_path fixture with no-op replace still passes
  • MVS-related tests pass (Test_Golang_MvS_Logic_Disabled, Test_Golang_MvS_Enabled_Preserves_All_Transitive_Dependencies)
  • mvn spotless:apply passes (code formatted)
  • mvn verify passes (only pre-existing Python env failures)

🤖 Generated with Claude Code

Summary by Sourcery

Handle Go module replace directives when resolving final package versions from go list -m all output.

Bug Fixes:

  • Correct resolution of final Go module versions by supporting replace directive lines in go list -m all output.

Enhancements:

  • Extract shared parseModuleVersions helper to parse go list -m all output into a module-to-version map.

Tests:

  • Extend Go module test fixtures with a replace directive example and update the expected SBOM for stack analysis to cover the new behavior.

The stream filter in getFinalPackagesVersionsForModule() only accepted
2-part lines from `go list -m all`, silently dropping replace directive
lines (5-part format: name v1 => replacement v2). This caused replaced
modules to be absent from the MVS version map, resulting in incorrect
SBOM versions.

Extract parseModuleVersions() to handle both standard and replace
directive lines, mirroring the JS client fix (PR guacsec#505).

Implements TC-4359

Assisted-by: Claude Code
@sourcery-ai

sourcery-ai Bot commented May 12, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Refactors Go module version parsing into a dedicated helper that correctly handles go list -m all output including replace directives, and updates Go module test fixtures and expected SBOM to cover the new behavior.

File-Level Changes

Change Details Files
Refactor and harden parsing of go list -m all output, including support for replace directives.
  • Replace inline stream-based parsing in getFinalPackagesVersionsForModule with a call to a new parseModuleVersions helper.
  • Implement parseModuleVersions to trim lines, split on spaces, accept both 2-part lines and 5-part replace directive lines, and map original module names to their final resolved versions.
  • Ensure merge behavior in the collector keeps the last seen version when duplicate module keys appear.
src/main/java/io/github/guacsec/trustifyda/providers/GoModulesProvider.java
Extend Go module test fixture to include a replace directive and update expected SBOM output accordingly.
  • Add a replace gopkg.in/yaml.v3 v3.0.1 => gopkg.in/yaml.v3 v3.0.0 directive to the go_mod_light_no_ignore fixture go.mod.
  • Adjust the corresponding expected_sbom_stack_analysis.json to reflect the resolved version from the replace directive.
src/test/resources/tst_manifests/golang/go_mod_light_no_ignore/go.mod
src/test/resources/tst_manifests/golang/go_mod_light_no_ignore/expected_sbom_stack_analysis.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The parseModuleVersions logic currently uses line.split(" "), which will produce empty tokens with multiple spaces and won't handle tab-separated go list -m all output well; consider splitting on \s+ instead to robustly handle varying whitespace.
  • To make parseModuleVersions more resilient, consider skipping blank/comment lines explicitly before splitting and perhaps guarding against malformed lines rather than relying solely on the parts.length filter.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `parseModuleVersions` logic currently uses `line.split(" ")`, which will produce empty tokens with multiple spaces and won't handle tab-separated `go list -m all` output well; consider splitting on `\s+` instead to robustly handle varying whitespace.
- To make `parseModuleVersions` more resilient, consider skipping blank/comment lines explicitly before splitting and perhaps guarding against malformed lines rather than relying solely on the `parts.length` filter.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@a-oren
a-oren requested a review from Strum355 May 12, 2026 07:38

@ruromero ruromero left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that splitting on \s+ it's worth implementing.
The blank lines suggestion should already be handled implicitly.
You can also add a unit test for the parseModuleVersions

Use regex whitespace splitting instead of single space to handle
tabs and multiple spaces in go list -m all output. Add unit tests
covering standard lines, replace directives, varied whitespace,
blank lines, malformed input, and duplicate handling.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@a-oren

a-oren commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

Verification Report for TC-4359 (commit 8628bff)

Check Result Details
Review Feedback PASS 2 reviews (sourcery-ai bot, ruromero). Feedback addressed: \s+ splitting and unit tests added in commit 8628bff
Root-Cause Investigation N/A No sub-tasks created
Scope Containment WARN Out-of-scope: GoModulesParseModuleVersionsTest.java (added per reviewer request). Task-listed go.sum and expected_sbom_component_analysis.json not modified (not needed — go.sum doesn't exist in fixture, component analysis doesn't reference yaml.v3)
Diff Size PASS 112 additions, 10 deletions, 4 files — proportionate to task scope
Commit Traceability WARN 1 of 3 commits missing explicit TC-4359 reference (commit 8628bff)
Sensitive Patterns PASS No sensitive patterns detected
CI Status PASS All 45 checks pass (build, unit tests, integration tests across ubuntu/macos/windows)
Acceptance Criteria PASS 5/5 criteria met
Test Quality WARN 8 test functions share same structure (input→parse→assert) — parameterization candidate per CONVENTIONS.md @ParameterizedTest @MethodSource pattern. No doc comments on test functions
Verification Commands PASS CI ran full mvn verify; all checks pass

Acceptance Criteria Detail

  • go list -m all lines with replace directives parsed — parseModuleVersions() handles 4+ parts with =>
  • Replaced modules appear with post-replace version — yaml.v3 shows v3.0.0 (not v3.0.1) in expected SBOM
  • Existing test suite passes — all 45 CI checks green
  • Test fixture exercises replace directive — go.mod has replace gopkg.in/yaml.v3 v3.0.1 => gopkg.in/yaml.v3 v3.0.0
  • Feature parity with JS client PR build(deps): bump org.pitest:pitest-maven from 1.25.1 to 1.25.4 #505 — same parseModuleVersions approach

Overall: WARN

Out-of-scope test file added per reviewer request. One commit missing explicit issue reference. Test functions could be parameterized per project conventions. No blocking issues.


This comment was AI-generated by sdlc-workflow/verify-pr v0.5.11.

@a-oren
a-oren requested a review from ruromero May 12, 2026 12:36

@ruromero ruromero left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ruromero
ruromero merged commit 421b4f1 into guacsec:main May 12, 2026
45 checks passed
@a-oren
a-oren deleted the TC-4359 branch June 4, 2026 06:48
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