From 42a425fc428d4e3df75c34999575cec70be3f91f Mon Sep 17 00:00:00 2001 From: wreulicke <12907474+wreulicke@users.noreply.github.com> Date: Thu, 10 Sep 2026 18:12:00 +0900 Subject: [PATCH 1/3] feat: add reportLocalModule option --- README.md | 5 +++ cmd/wrapcheck/main.go | 3 +- .../config_reportLocalModule/.wrapcheck.yaml | 1 + .../testdata/config_reportLocalModule/go.mod | 3 ++ .../config_reportLocalModule/local/local.go | 7 ++++ .../testdata/config_reportLocalModule/main.go | 15 +++++++++ .../.wrapcheck.yaml | 1 + .../config_reportLocalModule_true/go.mod | 3 ++ .../local/local.go | 7 ++++ .../config_reportLocalModule_true/main.go | 15 +++++++++ wrapcheck/wrapcheck.go | 33 ++++++++++++++++++- wrapcheck/wrapcheck_test.go | 6 +++- 12 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 wrapcheck/testdata/config_reportLocalModule/.wrapcheck.yaml create mode 100644 wrapcheck/testdata/config_reportLocalModule/go.mod create mode 100644 wrapcheck/testdata/config_reportLocalModule/local/local.go create mode 100644 wrapcheck/testdata/config_reportLocalModule/main.go create mode 100644 wrapcheck/testdata/config_reportLocalModule_true/.wrapcheck.yaml create mode 100644 wrapcheck/testdata/config_reportLocalModule_true/go.mod create mode 100644 wrapcheck/testdata/config_reportLocalModule_true/local/local.go create mode 100644 wrapcheck/testdata/config_reportLocalModule_true/main.go diff --git a/README.md b/README.md index 48cd472..b492106 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,11 @@ ignoreInterfaceRegexps: # ReportInternalErrors determines whether wrapcheck should report errors returned # from inside the package. reportInternalErrors: true + +# ReportLocalModule determines whether wrapcheck should report errors returned +# from packages within the module being analyzed. The package currently being +# analyzed is controlled separately by reportInternalErrors. +reportLocalModule: true ``` ## Usage diff --git a/cmd/wrapcheck/main.go b/cmd/wrapcheck/main.go index 006e398..4f1d53c 100644 --- a/cmd/wrapcheck/main.go +++ b/cmd/wrapcheck/main.go @@ -15,6 +15,7 @@ func main() { viper.AddConfigPath(".") viper.SetDefault("ignoreSigs", wrapcheck.DefaultIgnoreSigs) + viper.SetDefault("reportLocalModule", true) // Read in config, ignore if the file isn't found and use defaults. if err := viper.ReadInConfig(); err != nil { @@ -23,7 +24,7 @@ func main() { } } - var cfg wrapcheck.WrapcheckConfig + cfg := wrapcheck.NewDefaultConfig() if err := viper.Unmarshal(&cfg); err != nil { log.Fatalf("failed to unmarshal config: %v", err) } diff --git a/wrapcheck/testdata/config_reportLocalModule/.wrapcheck.yaml b/wrapcheck/testdata/config_reportLocalModule/.wrapcheck.yaml new file mode 100644 index 0000000..886eb5d --- /dev/null +++ b/wrapcheck/testdata/config_reportLocalModule/.wrapcheck.yaml @@ -0,0 +1 @@ +reportLocalModule: false diff --git a/wrapcheck/testdata/config_reportLocalModule/go.mod b/wrapcheck/testdata/config_reportLocalModule/go.mod new file mode 100644 index 0000000..1aa46f1 --- /dev/null +++ b/wrapcheck/testdata/config_reportLocalModule/go.mod @@ -0,0 +1,3 @@ +module example.com/reportlocal + +go 1.24 diff --git a/wrapcheck/testdata/config_reportLocalModule/local/local.go b/wrapcheck/testdata/config_reportLocalModule/local/local.go new file mode 100644 index 0000000..463427c --- /dev/null +++ b/wrapcheck/testdata/config_reportLocalModule/local/local.go @@ -0,0 +1,7 @@ +package local + +import "errors" + +func Error() error { + return errors.New("failed") +} diff --git a/wrapcheck/testdata/config_reportLocalModule/main.go b/wrapcheck/testdata/config_reportLocalModule/main.go new file mode 100644 index 0000000..98b7248 --- /dev/null +++ b/wrapcheck/testdata/config_reportLocalModule/main.go @@ -0,0 +1,15 @@ +package main + +import "example.com/reportlocal/local" + +func main() { + _ = do() +} + +func do() error { + if err := local.Error(); err != nil { + return err + } + + return nil +} diff --git a/wrapcheck/testdata/config_reportLocalModule_true/.wrapcheck.yaml b/wrapcheck/testdata/config_reportLocalModule_true/.wrapcheck.yaml new file mode 100644 index 0000000..56d0d27 --- /dev/null +++ b/wrapcheck/testdata/config_reportLocalModule_true/.wrapcheck.yaml @@ -0,0 +1 @@ +reportLocalModule: true diff --git a/wrapcheck/testdata/config_reportLocalModule_true/go.mod b/wrapcheck/testdata/config_reportLocalModule_true/go.mod new file mode 100644 index 0000000..1aa46f1 --- /dev/null +++ b/wrapcheck/testdata/config_reportLocalModule_true/go.mod @@ -0,0 +1,3 @@ +module example.com/reportlocal + +go 1.24 diff --git a/wrapcheck/testdata/config_reportLocalModule_true/local/local.go b/wrapcheck/testdata/config_reportLocalModule_true/local/local.go new file mode 100644 index 0000000..463427c --- /dev/null +++ b/wrapcheck/testdata/config_reportLocalModule_true/local/local.go @@ -0,0 +1,7 @@ +package local + +import "errors" + +func Error() error { + return errors.New("failed") +} diff --git a/wrapcheck/testdata/config_reportLocalModule_true/main.go b/wrapcheck/testdata/config_reportLocalModule_true/main.go new file mode 100644 index 0000000..89e3cb9 --- /dev/null +++ b/wrapcheck/testdata/config_reportLocalModule_true/main.go @@ -0,0 +1,15 @@ +package main + +import "example.com/reportlocal/local" + +func main() { + _ = do() +} + +func do() error { + if err := local.Error(); err != nil { + return err // want `error returned from external package is unwrapped` + } + + return nil +} diff --git a/wrapcheck/wrapcheck.go b/wrapcheck/wrapcheck.go index 12cd156..6228612 100644 --- a/wrapcheck/wrapcheck.go +++ b/wrapcheck/wrapcheck.go @@ -87,6 +87,12 @@ type WrapcheckConfig struct { // ReportInternalErrors determines whether wrapcheck should report errors returned // from inside the package. ReportInternalErrors bool `mapstructure:"reportInternalErrors" yaml:"reportInternalErrors"` + + // ReportLocalModule determines whether wrapcheck should report errors returned + // from packages within the module being analyzed. The package currently being + // analyzed is controlled separately by ReportInternalErrors. It defaults to + // true. + ReportLocalModule bool `mapstructure:"reportLocalModule" yaml:"reportLocalModule"` } func NewDefaultConfig() WrapcheckConfig { @@ -95,6 +101,7 @@ func NewDefaultConfig() WrapcheckConfig { IgnoreSigRegexps: []string{}, IgnorePackageGlobs: []string{}, IgnoreInterfaceRegexps: []string{}, + ReportLocalModule: true, } } @@ -298,7 +305,15 @@ func reportUnwrapped( return } - fnSig := pass.TypesInfo.ObjectOf(sel.Sel).String() + fn := pass.TypesInfo.ObjectOf(sel.Sel) + fnSig := fn.String() + + // Packages in the current module are not external packages. Keep this + // separate from ReportInternalErrors, which controls the package currently + // being analyzed. + if !cfg.ReportLocalModule && isFromOtherPackageInLocalModule(pass, fn.Pkg()) { + return + } // Check for ignored signatures if checkSignature(cfg, regexpsSig, fnSig) { @@ -363,6 +378,22 @@ func isFromOtherPkg(pass *analysis.Pass, sel *ast.SelectorExpr, pkgGlobs []glob. return true } +// isFromOtherPackageInLocalModule reports whether pkg belongs to the module +// currently being analyzed, but is not the package currently being analyzed. +// Package paths are hierarchical, so the module path must be followed by a +// slash when checking subpackages. +func isFromOtherPackageInLocalModule(pass *analysis.Pass, pkg *types.Package) bool { + if pass.Module == nil || pass.Module.Path == "" || pass.Pkg == nil || pkg == nil { + return false + } + + if pkg.Path() == pass.Pkg.Path() { + return false + } + + return pkg.Path() == pass.Module.Path || strings.HasPrefix(pkg.Path(), pass.Module.Path+"/") +} + // prevErrAssign traverses the AST of a file looking for the most recent // assignment to an error declaration which is specified by the returnIdent // identifier. diff --git a/wrapcheck/wrapcheck_test.go b/wrapcheck/wrapcheck_test.go index 1f76747..6100352 100644 --- a/wrapcheck/wrapcheck_test.go +++ b/wrapcheck/wrapcheck_test.go @@ -16,6 +16,10 @@ import ( // analysis tests to skip this directory due to needing explicit tests. const skipfile = "analysistest_skip" +func TestNewDefaultConfig(t *testing.T) { + assert.True(t, NewDefaultConfig().ReportLocalModule) +} + func TestAnalyzer(t *testing.T) { // Load the dirs under ./testdata p, err := filepath.Abs("./testdata") @@ -48,7 +52,7 @@ func TestAnalyzer(t *testing.T) { configFile, err := os.ReadFile(configPath) assert.NoError(t, err) - var config WrapcheckConfig + config := NewDefaultConfig() assert.NoError(t, yaml.Unmarshal(configFile, &config)) analysistest.Run(t, dirPath, NewAnalyzer(config)) } else { From ed9a84322de656185e2b46900735561fbb8c3a4f Mon Sep 17 00:00:00 2001 From: wreulicke <12907474+wreulicke@users.noreply.github.com> Date: Thu, 15 Oct 2026 10:30:08 +0900 Subject: [PATCH 2/3] refactor: rename reportLocalModule to ignoreLocalModule Default to false and revert the main.go change so that a user's ignoreSigs keeps replacing the defaults. Co-Authored-By: Claude Sonnet 5.5 --- README.md | 8 ++++---- .../config_ignoreLocalModule/.wrapcheck.yaml | 1 + wrapcheck/testdata/config_ignoreLocalModule/go.mod | 3 +++ .../local/local.go | 0 .../main.go | 2 +- .../config_ignoreLocalModule_false/.wrapcheck.yaml | 1 + .../testdata/config_ignoreLocalModule_false/go.mod | 3 +++ .../local/local.go | 0 .../main.go | 2 +- .../config_reportLocalModule/.wrapcheck.yaml | 1 - wrapcheck/testdata/config_reportLocalModule/go.mod | 3 --- .../config_reportLocalModule_true/.wrapcheck.yaml | 1 - .../testdata/config_reportLocalModule_true/go.mod | 3 --- wrapcheck/wrapcheck.go | 13 ++++++------- wrapcheck/wrapcheck_test.go | 6 +----- 15 files changed, 21 insertions(+), 26 deletions(-) create mode 100644 wrapcheck/testdata/config_ignoreLocalModule/.wrapcheck.yaml create mode 100644 wrapcheck/testdata/config_ignoreLocalModule/go.mod rename wrapcheck/testdata/{config_reportLocalModule => config_ignoreLocalModule}/local/local.go (100%) rename wrapcheck/testdata/{config_reportLocalModule => config_ignoreLocalModule}/main.go (76%) create mode 100644 wrapcheck/testdata/config_ignoreLocalModule_false/.wrapcheck.yaml create mode 100644 wrapcheck/testdata/config_ignoreLocalModule_false/go.mod rename wrapcheck/testdata/{config_reportLocalModule_true => config_ignoreLocalModule_false}/local/local.go (100%) rename wrapcheck/testdata/{config_reportLocalModule_true => config_ignoreLocalModule_false}/main.go (82%) delete mode 100644 wrapcheck/testdata/config_reportLocalModule/.wrapcheck.yaml delete mode 100644 wrapcheck/testdata/config_reportLocalModule/go.mod delete mode 100644 wrapcheck/testdata/config_reportLocalModule_true/.wrapcheck.yaml delete mode 100644 wrapcheck/testdata/config_reportLocalModule_true/go.mod diff --git a/README.md b/README.md index b492106..8038f5b 100644 --- a/README.md +++ b/README.md @@ -73,10 +73,10 @@ ignoreInterfaceRegexps: # from inside the package. reportInternalErrors: true -# ReportLocalModule determines whether wrapcheck should report errors returned -# from packages within the module being analyzed. The package currently being -# analyzed is controlled separately by reportInternalErrors. -reportLocalModule: true +# IgnoreLocalModule determines whether wrapcheck should ignore errors returned +# from other packages within the module being analyzed. The package currently +# being analyzed is controlled separately by reportInternalErrors. +ignoreLocalModule: false ``` ## Usage diff --git a/wrapcheck/testdata/config_ignoreLocalModule/.wrapcheck.yaml b/wrapcheck/testdata/config_ignoreLocalModule/.wrapcheck.yaml new file mode 100644 index 0000000..0b55f04 --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule/.wrapcheck.yaml @@ -0,0 +1 @@ +ignoreLocalModule: true diff --git a/wrapcheck/testdata/config_ignoreLocalModule/go.mod b/wrapcheck/testdata/config_ignoreLocalModule/go.mod new file mode 100644 index 0000000..e65d38b --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule/go.mod @@ -0,0 +1,3 @@ +module example.com/ignorelocal + +go 1.24 diff --git a/wrapcheck/testdata/config_reportLocalModule/local/local.go b/wrapcheck/testdata/config_ignoreLocalModule/local/local.go similarity index 100% rename from wrapcheck/testdata/config_reportLocalModule/local/local.go rename to wrapcheck/testdata/config_ignoreLocalModule/local/local.go diff --git a/wrapcheck/testdata/config_reportLocalModule/main.go b/wrapcheck/testdata/config_ignoreLocalModule/main.go similarity index 76% rename from wrapcheck/testdata/config_reportLocalModule/main.go rename to wrapcheck/testdata/config_ignoreLocalModule/main.go index 98b7248..16c1dd7 100644 --- a/wrapcheck/testdata/config_reportLocalModule/main.go +++ b/wrapcheck/testdata/config_ignoreLocalModule/main.go @@ -1,6 +1,6 @@ package main -import "example.com/reportlocal/local" +import "example.com/ignorelocal/local" func main() { _ = do() diff --git a/wrapcheck/testdata/config_ignoreLocalModule_false/.wrapcheck.yaml b/wrapcheck/testdata/config_ignoreLocalModule_false/.wrapcheck.yaml new file mode 100644 index 0000000..b8902a0 --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule_false/.wrapcheck.yaml @@ -0,0 +1 @@ +ignoreLocalModule: false diff --git a/wrapcheck/testdata/config_ignoreLocalModule_false/go.mod b/wrapcheck/testdata/config_ignoreLocalModule_false/go.mod new file mode 100644 index 0000000..e65d38b --- /dev/null +++ b/wrapcheck/testdata/config_ignoreLocalModule_false/go.mod @@ -0,0 +1,3 @@ +module example.com/ignorelocal + +go 1.24 diff --git a/wrapcheck/testdata/config_reportLocalModule_true/local/local.go b/wrapcheck/testdata/config_ignoreLocalModule_false/local/local.go similarity index 100% rename from wrapcheck/testdata/config_reportLocalModule_true/local/local.go rename to wrapcheck/testdata/config_ignoreLocalModule_false/local/local.go diff --git a/wrapcheck/testdata/config_reportLocalModule_true/main.go b/wrapcheck/testdata/config_ignoreLocalModule_false/main.go similarity index 82% rename from wrapcheck/testdata/config_reportLocalModule_true/main.go rename to wrapcheck/testdata/config_ignoreLocalModule_false/main.go index 89e3cb9..b9bf230 100644 --- a/wrapcheck/testdata/config_reportLocalModule_true/main.go +++ b/wrapcheck/testdata/config_ignoreLocalModule_false/main.go @@ -1,6 +1,6 @@ package main -import "example.com/reportlocal/local" +import "example.com/ignorelocal/local" func main() { _ = do() diff --git a/wrapcheck/testdata/config_reportLocalModule/.wrapcheck.yaml b/wrapcheck/testdata/config_reportLocalModule/.wrapcheck.yaml deleted file mode 100644 index 886eb5d..0000000 --- a/wrapcheck/testdata/config_reportLocalModule/.wrapcheck.yaml +++ /dev/null @@ -1 +0,0 @@ -reportLocalModule: false diff --git a/wrapcheck/testdata/config_reportLocalModule/go.mod b/wrapcheck/testdata/config_reportLocalModule/go.mod deleted file mode 100644 index 1aa46f1..0000000 --- a/wrapcheck/testdata/config_reportLocalModule/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module example.com/reportlocal - -go 1.24 diff --git a/wrapcheck/testdata/config_reportLocalModule_true/.wrapcheck.yaml b/wrapcheck/testdata/config_reportLocalModule_true/.wrapcheck.yaml deleted file mode 100644 index 56d0d27..0000000 --- a/wrapcheck/testdata/config_reportLocalModule_true/.wrapcheck.yaml +++ /dev/null @@ -1 +0,0 @@ -reportLocalModule: true diff --git a/wrapcheck/testdata/config_reportLocalModule_true/go.mod b/wrapcheck/testdata/config_reportLocalModule_true/go.mod deleted file mode 100644 index 1aa46f1..0000000 --- a/wrapcheck/testdata/config_reportLocalModule_true/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module example.com/reportlocal - -go 1.24 diff --git a/wrapcheck/wrapcheck.go b/wrapcheck/wrapcheck.go index 6228612..c0a2c7f 100644 --- a/wrapcheck/wrapcheck.go +++ b/wrapcheck/wrapcheck.go @@ -88,11 +88,11 @@ type WrapcheckConfig struct { // from inside the package. ReportInternalErrors bool `mapstructure:"reportInternalErrors" yaml:"reportInternalErrors"` - // ReportLocalModule determines whether wrapcheck should report errors returned - // from packages within the module being analyzed. The package currently being - // analyzed is controlled separately by ReportInternalErrors. It defaults to - // true. - ReportLocalModule bool `mapstructure:"reportLocalModule" yaml:"reportLocalModule"` + // IgnoreLocalModule determines whether wrapcheck should ignore errors returned + // from other packages within the module being analyzed. The package currently + // being analyzed is controlled separately by ReportInternalErrors. It defaults + // to false. + IgnoreLocalModule bool `mapstructure:"ignoreLocalModule" yaml:"ignoreLocalModule"` } func NewDefaultConfig() WrapcheckConfig { @@ -101,7 +101,6 @@ func NewDefaultConfig() WrapcheckConfig { IgnoreSigRegexps: []string{}, IgnorePackageGlobs: []string{}, IgnoreInterfaceRegexps: []string{}, - ReportLocalModule: true, } } @@ -311,7 +310,7 @@ func reportUnwrapped( // Packages in the current module are not external packages. Keep this // separate from ReportInternalErrors, which controls the package currently // being analyzed. - if !cfg.ReportLocalModule && isFromOtherPackageInLocalModule(pass, fn.Pkg()) { + if cfg.IgnoreLocalModule && isFromOtherPackageInLocalModule(pass, fn.Pkg()) { return } diff --git a/wrapcheck/wrapcheck_test.go b/wrapcheck/wrapcheck_test.go index 6100352..1f76747 100644 --- a/wrapcheck/wrapcheck_test.go +++ b/wrapcheck/wrapcheck_test.go @@ -16,10 +16,6 @@ import ( // analysis tests to skip this directory due to needing explicit tests. const skipfile = "analysistest_skip" -func TestNewDefaultConfig(t *testing.T) { - assert.True(t, NewDefaultConfig().ReportLocalModule) -} - func TestAnalyzer(t *testing.T) { // Load the dirs under ./testdata p, err := filepath.Abs("./testdata") @@ -52,7 +48,7 @@ func TestAnalyzer(t *testing.T) { configFile, err := os.ReadFile(configPath) assert.NoError(t, err) - config := NewDefaultConfig() + var config WrapcheckConfig assert.NoError(t, yaml.Unmarshal(configFile, &config)) analysistest.Run(t, dirPath, NewAnalyzer(config)) } else { From 274529d624a794578d048293f5ccfdc432e494c7 Mon Sep 17 00:00:00 2001 From: wreulicke <12907474+wreulicke@users.noreply.github.com> Date: Thu, 15 Oct 2026 10:31:40 +0900 Subject: [PATCH 3/3] revert: restore main.go config initialization Co-Authored-By: Claude Sonnet 5.5 --- cmd/wrapcheck/main.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/wrapcheck/main.go b/cmd/wrapcheck/main.go index 4f1d53c..006e398 100644 --- a/cmd/wrapcheck/main.go +++ b/cmd/wrapcheck/main.go @@ -15,7 +15,6 @@ func main() { viper.AddConfigPath(".") viper.SetDefault("ignoreSigs", wrapcheck.DefaultIgnoreSigs) - viper.SetDefault("reportLocalModule", true) // Read in config, ignore if the file isn't found and use defaults. if err := viper.ReadInConfig(); err != nil { @@ -24,7 +23,7 @@ func main() { } } - cfg := wrapcheck.NewDefaultConfig() + var cfg wrapcheck.WrapcheckConfig if err := viper.Unmarshal(&cfg); err != nil { log.Fatalf("failed to unmarshal config: %v", err) }