fix: implement MainModuleOnly filter correctly in ShouldInclude()

- Added IsMainModule field to PackageInfo to track main module membership
- Updated buildPackageStore() to populate IsMainModule for all packages
- Modified ShouldInclude() to check MainModuleOnly parameter
- Now correctly filters out packages from submodules when --main-only flag is used

The MainModuleOnly parameter was previously defined but never actually used
in the filtering logic, making the --main-only flag ineffective.
This commit is contained in:
hailaz
2026-01-09 16:15:06 +08:00
parent 375b094d37
commit b7323a59ee

View File

@ -56,6 +56,7 @@ type PackageInfo struct {
Imports []string // Direct imports of this package
IsStdLib bool // Standard library marker (from go list)
IsModuleRoot bool // Is this the root package of its module
IsMainModule bool // Is this package from the main module (not a submodule)
}
// FilterOptions represents filtering criteria for dependency analysis.
@ -320,6 +321,11 @@ func (opts *FilterOptions) Normalize(modulePrefix string) error {
// ShouldInclude determines if a package should be included based on filter options.
func (opts *FilterOptions) ShouldInclude(pkg *PackageInfo) bool {
// Filter by main module only (exclude submodules)
if opts.MainModuleOnly && !pkg.IsMainModule {
return false
}
// Filter by kind
switch pkg.Kind {
case KindStdLib:
@ -633,10 +639,11 @@ func (a *analyzer) buildPackageStore() *PackageStore {
// Convert go packages to PackageInfo
for path, goPkg := range a.packages {
pkgInfo := &PackageInfo{
ImportPath: path,
ModulePath: goPkg.Module.Path,
IsStdLib: goPkg.Standard,
Imports: goPkg.Imports,
ImportPath: path,
ModulePath: goPkg.Module.Path,
IsStdLib: goPkg.Standard,
Imports: goPkg.Imports,
IsMainModule: a.isMainModulePackage(path),
}
pkgInfo.Kind = store.identifyPackageKind(pkgInfo)
store.packages[path] = pkgInfo