Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 64 additions & 8 deletions internal/baseline/loaded.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"slices"
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
}

Expand Down Expand Up @@ -163,21 +173,67 @@ 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",
filepath.ToSlash(dir),
), 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")
}
Expand Down
10 changes: 10 additions & 0 deletions internal/baseline/loaded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down