Skip to content

Treat the module as the main module with -main - #44

Closed
logica0419 wants to merge 7 commits into
bobg:mainfrom
logica0419:follow-replace-directives
Closed

logica0419 wants to merge 7 commits into
bobg:mainfrom
logica0419:follow-replace-directives

Conversation

@logica0419

@logica0419 logica0419 commented Oct 31, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #43

With -main, scanDeps() now parses go.mod with modfile.Parse() and follows replace directives.
Default stays ParseLax().

@logica0419
logica0419 force-pushed the follow-replace-directives branch from 2475ed2 to 3ec0d75 Compare October 31, 2026 16:31
Signed-off-by: Takuto Nagami <logica0419@gmail.com>
Signed-off-by: Takuto Nagami <logica0419@gmail.com>
Signed-off-by: Takuto Nagami <logica0419@gmail.com>
Signed-off-by: Takuto Nagami <logica0419@gmail.com>
@logica0419
logica0419 force-pushed the follow-replace-directives branch from 3ec0d75 to 8aff22d Compare October 31, 2026 16:37

@bobg bobg left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you for another high-quality contribution! This time I have a couple of changes to request.

First, according to the docs for modfile, the choice of Parse or ParseLax depends mainly on whether the module in question is the "main" one or not, and applies to exclude directives as well (and possibly others), not just replace directives. As such, it seems to me that the option we want is not whether to handle replace directives but, more broadly, whether to treat the module as the "main" one. (Default: false.) How do you feel about revising this PR to do that?

Second, you've added logic that chooses between a call to scanDep and scanLocalDep. Could you recast that as a choice between using a local and non-local implementation of the depScanner interface?

Comment thread deps.go Outdated
if err := s.scanDep(r.Mod); err != nil {

mod := r.Mod
if !s.IgnoreReplace && len(f.Replace) > 0 {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please avoid deeply nested if conditions. This is better:

if s.IgnoreReplace {
  continue
}
if len(f.Replace) == 0 {
  continue
}
rep := findReplace(f.Replace, mod)
if rep == nil {
  continue
}
...etc...

Signed-off-by: Takuto Nagami <logica0419@gmail.com>
Signed-off-by: Takuto Nagami <logica0419@gmail.com>
@logica0419

logica0419 commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator Author

@bobg
Sorry for the late response.
I've addressed all the requested changes. Please take a look when you have time! ;)

As such, it seems to me that the option we want is not whether to handle replace directives but, more broadly, whether to treat the module as the "main" one. (Default: false.) How do you feel about revising this PR to do that?

I wasn't familiar with the criteria for choosing between Parse and ParseLax, so I looked into them.
With that, I think your suggestion makes a lot of sense. Thanks for pointing this out!

@logica0419 logica0419 changed the title Follow replace directives when scanning dependencies Treat the module as the main module with -main Nov 12, 2026
@bobg
bobg requested a balanced review from Copilot November 12, 2026 15:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Regression tests cover only local-directory replacements, leaving replacement path/version selection, resulting Go minimums, and exact-version versus wildcard precedence unverified.

1 open issue
What changed in this PR

Adds opt-in main-module handling to mingo, addressing #43 by honoring replace directives during dependency scanning while preserving the default behavior.

Changes:

  • Adds the CLI -main flag and library Scanner.Main option.
  • Resolves replacements to local directories or alternate module versions.
  • Documents the option and adds regression tests for local replacements and default behavior.
File Description
testdata/​issue43/​main.go Adds a fixture importing the replaced dependency.
testdata/​issue43/​localdep/​go.mod Declares the local replacement module.
testdata/​issue43/​localdep/​dep.go Provides the local dependency package.
testdata/​issue43/​go.mod Configures the local replacement fixture.
scan.go Adds the Scanner.Main option.
regression_test.go Tests local replacement handling and default behavior.
Readme.md Documents -main and replacement behavior.
deps.go Parses and resolves replacements in main-module mode.
cmd/​mingo/​main.go Connects the -main flag to the scanner.

Review effort: Balanced

Comment thread deps.go
eff.Version = mv.Version
}

return eff, s.depScanner
Signed-off-by: Takuto Nagami <logica0419@gmail.com>

@bobg bobg left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

My turn to apologize for the delay in reviewing these latest changes. I've been trying to figure out what's been nagging at me about this PR.

I realized it's the way that this replace logic - while perfectly fine on its own, as near as I can tell - is only a piece of the full dependency-resolution process that the real Go toolchain follows. To do it the right way requires also paying attention to exclude directives, any go.work file that may exist, etc.

I'm not aware of any standard(ish) library that does all this, but I do know of a command that does: go list -deps -json. I have taken the liberty of rewriting Mingo's dependency-scanning logic in terms of go list at #45.

As such I think this PR is now obsolete; my apologies, and my thanks, for your contribution and patience. Can I further prevail upon you to take a look at the new PR?

@bobg bobg closed this Nov 18, 2026
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.

scanDeps ignores replace directives

3 participants