From 08abab01b41b4970a3b987bfd6e1810453e44ae4 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 23 Nov 2026 12:58:33 -0600 Subject: [PATCH 1/3] fix(lint-baseline): check testdata modules when a go.work exists --- README.md | 3 ++- internal/baseline/loaded.go | 44 ++++++++++++++++++++++++++------ internal/baseline/loaded_test.go | 4 +++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4facaf3..80a5252 100644 --- a/README.md +++ b/README.md @@ -123,7 +123,8 @@ with several modules, either run the action once per module with `other-modules: skip`, or keep the default to reject changes outside the selected module. It accepts unimported `testdata` files, including files in fixture modules -with no packages in the Go package list. The Linux lint job does not lint +with no packages in the Go package list, unless a `go.work` file sits in or +above the selected module. The Linux lint job does not lint files that only load on Windows or macOS. The full-tree dead-code check runs last. It fails on any `unused` or diff --git a/internal/baseline/loaded.go b/internal/baseline/loaded.go index 7e7c810..ed70194 100644 --- a/internal/baseline/loaded.go +++ b/internal/baseline/loaded.go @@ -163,14 +163,9 @@ func (loaded loadedGo) otherModuleReason(dir string) (string, error) { if loaded.skipOtherModules { return "", nil } - if loaded.listedModules != nil && isTestdataPath(dir) { - physical, err := physicalPath(filepath.Join(loaded.root, dir)) - if err != nil { - return "", fmt.Errorf("resolve testdata module directory: %w", err) - } - if !loaded.listedModules[physical] { - return "", nil - } + exempt, err := loaded.unimportedTestdata(dir) + if err != nil || exempt { + return "", err } return fmt.Sprintf( "belongs to module %q, which this run does not lint", @@ -178,6 +173,39 @@ func (loaded loadedGo) otherModuleReason(dir string) (string, error) { ), nil } +// unimportedTestdata reports a testdata module that no listed package belongs +// to. go list runs with GOWORK=off, so a go.work could build such a module +// unseen; any go.work turns the exemption off. +func (loaded loadedGo) unimportedTestdata(dir string) (bool, error) { + if loaded.listedModules == nil || !isTestdataPath(dir) { + return false, nil + } + workspace, err := hasWorkspace(loaded.tree, filepath.FromSlash(loaded.moduleDir)) + if err != nil || workspace { + return false, err + } + physical, err := physicalPath(filepath.Join(loaded.root, dir)) + if err != nil { + return false, fmt.Errorf("resolve testdata module directory: %w", err) + } + return !loaded.listedModules[physical], nil +} + +func hasWorkspace(root *os.Root, dir string) (bool, error) { + for dir = filepath.Clean(dir); ; dir = filepath.Dir(dir) { + _, err := root.Lstat(filepath.Join(dir, "go.work")) + if err == nil { + return true, nil + } + if !errors.Is(err, os.ErrNotExist) { + return false, fmt.Errorf("stat go.work: %w", err) + } + if dir == "." { + return false, nil + } + } +} + func isTestdataPath(name string) bool { return slices.Contains(strings.Split(filepath.ToSlash(name), "/"), "testdata") } diff --git a/internal/baseline/loaded_test.go b/internal/baseline/loaded_test.go index 9a9864d..6bd1646 100644 --- a/internal/baseline/loaded_test.go +++ b/internal/baseline/loaded_test.go @@ -215,6 +215,10 @@ func TestDirectiveTestdataModule(t *testing.T) { moduleDir, ) assertDirectives(t, input, want+failed, 1) + + input.args[len(input.args)-1] = emptyList + directiveFixture(t, root, "go.work", "go 1.26\n\nuse (\n\t.\n\t./"+moduleDir+"\n)\n") + assertDirectives(t, input, want+failed, 1) } func TestDirectiveAddedSymlink(t *testing.T) { From abc63ed55de125e3d85f47ba70e131806307a5ff Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 23 Nov 2026 13:00:21 -0600 Subject: [PATCH 2/3] fix(lint-baseline): look for go.work anywhere in the repository --- README.md | 4 ++-- internal/baseline/loaded.go | 34 +++++++++++++++++++++----------- internal/baseline/loaded_test.go | 3 ++- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 80a5252..90b2a9a 100644 --- a/README.md +++ b/README.md @@ -123,8 +123,8 @@ with several modules, either run the action once per module with `other-modules: skip`, or keep the default to reject changes outside the selected module. It accepts unimported `testdata` files, including files in fixture modules -with no packages in the Go package list, unless a `go.work` file sits in or -above the selected module. The Linux lint job does not lint +with no packages in the Go package list, unless the repository contains a +`go.work` file. The Linux lint job does not lint files that only load on Windows or macOS. The full-tree dead-code check runs last. It fails on any `unused` or diff --git a/internal/baseline/loaded.go b/internal/baseline/loaded.go index ed70194..0f33619 100644 --- a/internal/baseline/loaded.go +++ b/internal/baseline/loaded.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "io" + "io/fs" "os" "path/filepath" "slices" @@ -174,13 +175,13 @@ func (loaded loadedGo) otherModuleReason(dir string) (string, error) { } // unimportedTestdata reports a testdata module that no listed package belongs -// to. go list runs with GOWORK=off, so a go.work could build such a module -// unseen; any go.work turns the exemption off. +// to. go list runs with GOWORK=off, so a go.work anywhere in the repo could +// build such a module unseen; any go.work turns the exemption off. func (loaded loadedGo) unimportedTestdata(dir string) (bool, error) { if loaded.listedModules == nil || !isTestdataPath(dir) { return false, nil } - workspace, err := hasWorkspace(loaded.tree, filepath.FromSlash(loaded.moduleDir)) + workspace, err := hasWorkspace(loaded.tree) if err != nil || workspace { return false, err } @@ -191,19 +192,28 @@ func (loaded loadedGo) unimportedTestdata(dir string) (bool, error) { return !loaded.listedModules[physical], nil } -func hasWorkspace(root *os.Root, dir string) (bool, error) { - for dir = filepath.Clean(dir); ; dir = filepath.Dir(dir) { - _, err := root.Lstat(filepath.Join(dir, "go.work")) - if err == nil { - return true, nil +var errWorkspaceFound = errors.New("go.work found") + +func hasWorkspace(root *os.Root) (bool, error) { + err := fs.WalkDir(root.FS(), ".", func(_ string, entry fs.DirEntry, err error) error { + if err != nil { + return err } - if !errors.Is(err, os.ErrNotExist) { - return false, fmt.Errorf("stat go.work: %w", err) + if entry.Name() == "go.work" { + return errWorkspaceFound } - if dir == "." { - return false, nil + if entry.IsDir() && entry.Name() == ".git" { + return fs.SkipDir } + return nil + }) + if errors.Is(err, errWorkspaceFound) { + return true, nil + } + if err != nil { + return false, fmt.Errorf("find go.work: %w", err) } + return false, nil } func isTestdataPath(name string) bool { diff --git a/internal/baseline/loaded_test.go b/internal/baseline/loaded_test.go index 6bd1646..2193a49 100644 --- a/internal/baseline/loaded_test.go +++ b/internal/baseline/loaded_test.go @@ -217,7 +217,8 @@ func TestDirectiveTestdataModule(t *testing.T) { assertDirectives(t, input, want+failed, 1) input.args[len(input.args)-1] = emptyList - directiveFixture(t, root, "go.work", "go 1.26\n\nuse (\n\t.\n\t./"+moduleDir+"\n)\n") + workspace := "go 1.26\n\nuse ../" + moduleDir + "\n" + directiveFixture(t, root, "tools/go.work", workspace) assertDirectives(t, input, want+failed, 1) } From e22e772b5490ea14a037b01c0638d01927c8d1ab Mon Sep 17 00:00:00 2001 From: Samuel K Date: Mon, 23 Nov 2026 13:01:53 -0600 Subject: [PATCH 3/3] fix(lint-baseline): ignore go.work directories and walk once --- internal/baseline/loaded.go | 22 ++++++++++++++++++++-- internal/baseline/loaded_test.go | 5 +++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/internal/baseline/loaded.go b/internal/baseline/loaded.go index 0f33619..8a71296 100644 --- a/internal/baseline/loaded.go +++ b/internal/baseline/loaded.go @@ -20,9 +20,17 @@ type loadedGo struct { files, allowed map[string]bool listedModules map[string]bool imports map[packageImport]bool + workspace *workspaceSearch skipOtherModules bool } +// workspaceSearch remembers whether the tree has a go.work, so the walk runs +// at most once per check. +type workspaceSearch struct { + done, found bool + err error +} + type packageImport struct{ importer, imported string } type listedPackage struct { @@ -60,6 +68,7 @@ func readLoaded(tree *os.Root, rules directiveRules) (loadedGo, error) { loaded.files, loaded.allowed = map[string]bool{}, map[string]bool{} loaded.listedModules = map[string]bool{} loaded.imports = map[packageImport]bool{} + loaded.workspace = &workspaceSearch{} return loaded, errors.Join(loaded.decode(file), file.Close()) } @@ -181,7 +190,7 @@ func (loaded loadedGo) unimportedTestdata(dir string) (bool, error) { if loaded.listedModules == nil || !isTestdataPath(dir) { return false, nil } - workspace, err := hasWorkspace(loaded.tree) + workspace, err := loaded.hasWorkspace() if err != nil || workspace { return false, err } @@ -194,12 +203,21 @@ func (loaded loadedGo) unimportedTestdata(dir string) (bool, error) { var errWorkspaceFound = errors.New("go.work found") +func (loaded loadedGo) hasWorkspace() (bool, error) { + search := loaded.workspace + if !search.done { + search.found, search.err = hasWorkspace(loaded.tree) + search.done = true + } + return search.found, search.err +} + func hasWorkspace(root *os.Root) (bool, error) { err := fs.WalkDir(root.FS(), ".", func(_ string, entry fs.DirEntry, err error) error { if err != nil { return err } - if entry.Name() == "go.work" { + if entry.Name() == "go.work" && !entry.IsDir() { return errWorkspaceFound } if entry.IsDir() && entry.Name() == ".git" { diff --git a/internal/baseline/loaded_test.go b/internal/baseline/loaded_test.go index 2193a49..97dd4dd 100644 --- a/internal/baseline/loaded_test.go +++ b/internal/baseline/loaded_test.go @@ -217,6 +217,11 @@ func TestDirectiveTestdataModule(t *testing.T) { assertDirectives(t, input, want+failed, 1) input.args[len(input.args)-1] = emptyList + if err := os.MkdirAll(filepath.Join(root, "notes", "go.work"), 0o750); err != nil { + t.Fatal(err) + } + assertDirectives(t, input, passed, 0) + workspace := "go 1.26\n\nuse ../" + moduleDir + "\n" directiveFixture(t, root, "tools/go.work", workspace) assertDirectives(t, input, want+failed, 1)