Skip to content

Add support for replace directive in go.mod files#4673

Open
uttam282005 wants to merge 11 commits intoaboutcode-org:developfrom
uttam282005:support-replace-directive
Open

Add support for replace directive in go.mod files#4673
uttam282005 wants to merge 11 commits intoaboutcode-org:developfrom
uttam282005:support-replace-directive

Conversation

@uttam282005
Copy link
Copy Markdown
Contributor

Fixes #3492

  1. Parsing Replace Directives (go_mod.py)
  • Added handle_replace_directive() function to parse and categorize replacements:
  • Parses both inline and block-style replace statements
  • Distinguishes between local path and remote replacements
  • Stores original module in exclude list (as it's being replaced)
  • Stores replacement in require list (remote) or local_replacements (local)
  1. Local Replacement Resolution (golang.py)
  • Added resolve_local_replacements() method that:
  • Traverses the codebase to locate local replacement targets
  • Resolves relative paths (e.g., ../pkg/) to absolute paths
  • Extracts package metadata from target's go.mod file
  • Creates resolved dependency entries with full package information

3. Assembly Integration

Modified BaseGoModuleHandler.assemble() to:

  • Resolve local replacements after package assembly
  • Ensure resolved dependencies are included in final output

This PR is inspired by the work done by @shravankshenoy and maintainer reviews on PR #3693.

Tasks

  • Reviewed contribution guidelines
  • PR is descriptively titled 📑 and links the original issue above 🔗
  • Tests pass -- look for a green checkbox ✔️ a few minutes after opening your PR
    Run tests locally to check for errors.
  • Commits are in uniquely-named feature branch and has no merge conflicts 📁
  • Updated documentation pages (if applicable)
  • Updated CHANGELOG.rst (if applicable)

Copilot AI review requested due to automatic review settings January 10, 2026 16:04
@uttam282005 uttam282005 force-pushed the support-replace-directive branch from dc7c152 to 0b389bf Compare January 10, 2026 16:05
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for parsing replace directives in Go module files (go.mod), enabling ScanCode to properly handle both local path and remote package replacements commonly used in Go projects.

Changes:

  • Added parsing logic for replace directives in both inline and block-style formats
  • Implemented resolution of local replacement paths to extract package metadata from target go.mod files
  • Enhanced package data model to track local replacements and their resolved dependencies

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
src/packagedcode/go_mod.py Added replace directive parsing functions, regex patterns, and local_replacements tracking in GoModule class
src/packagedcode/golang.py Added resolve_local_replacements method to resolve local paths and integrate resolved dependencies into package data
tests/packagedcode/test_golang.py Added two new test cases for milvus and gopls projects with replace directives
tests/packagedcode/data/golang/gomod/milvus/go.mod New test fixture with mixed local and remote replace directives
tests/packagedcode/data/golang/gomod/milvus/output.expected.json Expected parsing output for milvus test
tests/packagedcode/data/golang/gomod/gopls/go.mod New test fixture with local replace directive
tests/packagedcode/data/golang/gomod/gopls/output.expected.json Expected parsing output for gopls test
tests/packagedcode/data/golang/gomod/opencensus-service/output.expected.json Updated expected output to include replace directive results

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/packagedcode/go_mod.py
Comment thread src/packagedcode/go_mod.py
Comment thread src/packagedcode/golang.py Outdated
Comment thread src/packagedcode/go_mod.py
Comment thread src/packagedcode/golang.py Outdated
Comment thread src/packagedcode/golang.py Outdated
Comment thread src/packagedcode/go_mod.py
Comment thread src/packagedcode/golang.py
Comment thread src/packagedcode/golang.py Outdated
Comment thread src/packagedcode/go_mod.py
@uttam282005 uttam282005 marked this pull request as draft January 12, 2026 18:44
uttam282005 and others added 11 commits January 13, 2026 19:44
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
Signed-off-by: uttam282005 <uttam282005@gmail.com>
@uttam282005 uttam282005 force-pushed the support-replace-directive branch from 4da4fd1 to d6a0252 Compare January 13, 2026 14:14
@uttam282005 uttam282005 marked this pull request as ready for review January 13, 2026 14:17
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.

Support directives in go.mod

2 participants