diff --git a/README.md b/README.md index 4facaf3..90b2a9a 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 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 7e7c810..8a71296 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" @@ -19,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 { @@ -59,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()) } @@ -163,14 +173,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 +183,57 @@ 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 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 := loaded.hasWorkspace() + 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 +} + +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" && !entry.IsDir() { + return errWorkspaceFound + } + 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 { 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..97dd4dd 100644 --- a/internal/baseline/loaded_test.go +++ b/internal/baseline/loaded_test.go @@ -215,6 +215,16 @@ func TestDirectiveTestdataModule(t *testing.T) { moduleDir, ) 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) } func TestDirectiveAddedSymlink(t *testing.T) {