Skip to content
Merged
13 changes: 13 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ jobs:
# failure. Otherwise a broken install would restore the silent
# skip and the suite would stay green over the guard it disabled.
echo "PROCODER_PHP_PLUGIN_REQUIRED=1" >> "$GITHUB_ENV"
- name: golangci-lint, so the real-binary lint test runs instead of skipping
# TestGolangciFindsARealProblemInANestedModule is the only test that
# checks procoder's golangci-lint flags and path handling against the
# real tool; the stub tests cannot catch a flag it rejects. Pinned
# from the same file the gate reads. Linux only, once.
if: runner.os == 'Linux'
run: |
eval "$(grep -E '^[A-Z][A-Z0-9_]*=' .github/tool-versions.env)"
cd /tmp
curl -sSfL "https://github.com/golangci/golangci-lint/releases/download/v${GOLANGCI_LINT}/golangci-lint-${GOLANGCI_LINT}-linux-amd64.tar.gz" | tar xz
sudo install -m 0755 "golangci-lint-${GOLANGCI_LINT}-linux-amd64/golangci-lint" /usr/local/bin/golangci-lint
golangci-lint --version
echo "PROCODER_GOLANGCI_REQUIRED=1" >> "$GITHUB_ENV"
- run: go test ./internal/...
- name: the binary builds
run: go build ./cmd/procoder
Expand Down
13 changes: 10 additions & 3 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,12 @@ merely parsed. A linter that could not run is BLOCKING: a green gate has
to mean the code was checked, not that the machine was empty. Go repositories without a golangci config get Procoder's curated
baseline (standard set plus gosec, gocritic, errorlint, unparam,
copyloopvar, nilerr) — the repo's own golangci config always wins,
whichever of `.golangci.yml`/`.yaml`/`.toml`/`.json` it uses. Report
by default; `[lint] policy = "block"` makes findings block.
whichever of `.golangci.yml`/`.yaml`/`.toml`/`.json` it uses. golangci-lint runs
once per Go module, from the module's directory: a changed file in a nested
module (its own `go.mod`, such as a `bench/` with `replace => ../`) is linted
as that module, and a repository config at the root is passed to a nested
module that has none. A `go.work` at the root keeps the single run from the
root. Report by default; `[lint] policy = "block"` makes findings block.

`--types` adds the type-checker where the canonical linter does not
compile the code: `tsc --noEmit` for TypeScript (grouped under each
Expand Down Expand Up @@ -647,7 +651,10 @@ so a failure is attributed to the test the framework failed rather than
to a line that merely looks like one; a package that failed outside any
test (a build error, a crash) is named by package, and each Go failure
carries a bounded excerpt of its own output — the assertion, panic or
build error — under the FAIL line, in the gate's finding as well. `--coverage` adds the
build error — under the FAIL line, in the gate's finding as well. When the
narrowed packages sit in a nested Go module (its own `go.mod`), `go test` runs
once per module from that module's directory, and the detail is prefixed with
the module; with no narrowed paths it covers the root module only. `--coverage` adds the
percentage where the runner measures it natively (Go; pytest with
pytest-cov); a number is reported, never enforced. With
`--name <pattern>` narrows the run to matching tests — `-run` for Go,
Expand Down
7 changes: 7 additions & 0 deletions docs/domains.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ complexity check alike. Its lock is machine-wide, so a run from another
worktree or agent makes this one wait instead of failing. The lint
deadline still bounds the wait.

Go files are grouped by their nearest `go.mod` and golangci-lint runs once
per module, from the module's directory, with package patterns relative to
it; findings are mapped back to repo-root paths. golangci-lint resolves
packages against the module it runs in, so a file in a nested module named
from the root used to fail as NOT checked ("main module ... does not contain
package"). A `go.work` at the root keeps one run from the root.

The commit gate runs SAST and blocks on findings in the files the commit
carries. semgrep is pointed at the tree and narrowed with one `--include`
per changed file, never handed the files as targets — naming files
Expand Down
82 changes: 82 additions & 0 deletions internal/gomod/gomod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Package gomod answers which Go module owns a file. golangci-lint and
// `go test` both resolve packages against the module of their working
// directory, so a file in a nested module (its own go.mod, e.g. a bench/
// with `replace => ../`) cannot be named from the repository root: the run
// fails with "main module ... does not contain package". Callers group
// files by module and run once per group from the module's directory.
package gomod

import (
"os"
"path/filepath"
"strings"
)

func exists(p string) bool {
_, err := os.Stat(p)
return err == nil
}

// Workspace reports whether root carries a go.work. A workspace resolves
// every member module from the root, so such a repository needs no split.
func Workspace(root string) bool {
return exists(filepath.Join(root, "go.work"))
}

// ModuleDir is the directory of the go.mod nearest to p, ascending from p
// when it is a directory (the gate passes package directories) or from its
// parent when it is a file, and stopping at root. A path under no go.mod, or
// outside root, belongs to root: the repository is then taken to be one
// module, as it was before nested modules were understood. A go.mod under a
// testdata or vendor directory is ignored, as the go command ignores it, so
// a file there belongs to the enclosing real module.
func ModuleDir(root, p string) string {
rel, err := filepath.Rel(root, p)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return root
}
dir := p
if info, err := os.Stat(p); err != nil || !info.IsDir() {
dir = filepath.Dir(p)
}
for {
if exists(filepath.Join(dir, "go.mod")) && !ignoredDir(root, dir) {
return dir
}
parent := filepath.Dir(dir)
if dir == root || parent == dir {
return root
}
dir = parent
}
}

// ignoredDir reports whether dir sits at or under a testdata or vendor
// directory below root.
func ignoredDir(root, dir string) bool {
rel, err := filepath.Rel(root, dir)
if err != nil {
return false
}
for _, part := range strings.Split(filepath.ToSlash(rel), "/") {
if part == "testdata" || part == "vendor" {
return true
}
}
return false
}

// Group splits absolute paths by owning module directory. Under a go.work
// at root everything stays one group at root.
func Group(root string, paths []string) map[string][]string {
out := map[string][]string{}
work := Workspace(root)
for _, p := range paths {
dir := root
if !work {
dir = ModuleDir(root, p)
}
out[dir] = append(out[dir], p)
}
return out
}
64 changes: 64 additions & 0 deletions internal/gomod/gomod_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package gomod

import (
"os"
"path/filepath"
"testing"
)

func layout(t *testing.T, files ...string) string {
t.Helper()
root, err := filepath.EvalSymlinks(t.TempDir())
if err != nil {
t.Fatal(err)
}
for _, f := range files {
p := filepath.Join(root, filepath.FromSlash(f))
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte("x\n"), 0o644); err != nil {
t.Fatal(err)
}
}
return root
}

// proved by: start the ascent at filepath.Dir(p) for a directory too —
// sub (a package directory at the nested module's root) resolves to the
// root module, and the "dir input" row fails.
func TestModuleDir(t *testing.T) {
root := layout(t, "go.mod", "a/a.go", "sub/go.mod", "sub/main.go", "sub/x/x.go",
"sub/testdata/go.mod", "sub/testdata/t.go", "vendor/go.mod", "vendor/v/v.go")
sub := filepath.Join(root, "sub")
for name, c := range map[string]struct{ in, want string }{
"file in root module": {filepath.Join(root, "a", "a.go"), root},
"file in module root": {filepath.Join(sub, "main.go"), sub},
"file below module root": {filepath.Join(sub, "x", "x.go"), sub},
"dir input is its own start": {sub, sub},
"dir input below": {filepath.Join(sub, "x"), sub},
"outside root": {filepath.Join(filepath.Dir(root), "elsewhere", "e.go"), root},
"testdata go.mod ignored": {filepath.Join(sub, "testdata", "t.go"), sub},
"vendor go.mod ignored": {filepath.Join(root, "vendor", "v", "v.go"), root},
"no go.mod anywhere": {filepath.Join(root, "nomod", "z.go"), root},
} {
if got := ModuleDir(root, c.in); got != c.want {
t.Errorf("%s: ModuleDir(%s) = %s, want %s", name, c.in, got, c.want)
}
}
}

// proved by: drop the go.work branch of Group — the workspace splits in two.
func TestGroup(t *testing.T) {
root := layout(t, "go.mod", "a/a.go", "sub/go.mod", "sub/x/x.go")
a, b := filepath.Join(root, "a", "a.go"), filepath.Join(root, "sub", "x", "x.go")
if g := Group(root, []string{a, b}); len(g) != 2 {
t.Errorf("want a group per module, got %v", g)
}
if err := os.WriteFile(filepath.Join(root, "go.work"), []byte("go 1.22\n"), 0o644); err != nil {
t.Fatal(err)
}
if g := Group(root, []string{a, b}); len(g) != 1 || len(g[root]) != 2 {
t.Errorf("a go.work repo is one group at the root, got %v", g)
}
}
105 changes: 105 additions & 0 deletions internal/lint/gomod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package lint

import (
"os"
"path/filepath"
"sort"
"strings"
"sync"
)

// golangciConfigNames are the config files golangci-lint itself accepts.
var golangciConfigNames = []string{".golangci.yml", ".golangci.yaml", ".golangci.toml", ".golangci.json"}

// sortedKeys gives the module directories in a stable order, so the order
// of findings does not depend on map iteration.
func sortedKeys(m map[string][]string) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}

// goPatterns are the package patterns for files, relative to modDir:
// "./" for the module root, "./x" for a subpackage. Sorted and unique.
func goPatterns(modDir string, files []string) []string {
seen := map[string]bool{}
var out []string
for _, f := range files {
rel, err := filepath.Rel(modDir, filepath.Dir(f))
if err != nil {
continue
}
pat := "./"
if rel != "." {
pat = "./" + filepath.ToSlash(rel)
}
if !seen[pat] {
seen[pat] = true
out = append(out, pat)
}
}
sort.Strings(out)
return out
}

// repoRelPath maps a path golangci-lint printed — relative to the
// directory it ran in, or absolute — to a slash path relative to root,
// which is what the wanted filter and gitx.Finding carry. A path outside
// root is returned as printed.
func repoRelPath(root, modDir, p string) string {
abs := p
if !filepath.IsAbs(abs) {
abs = filepath.Join(modDir, p)
}
if rel, ok := under(root, abs); ok {
return rel
}
// the tool may print a symlink-resolved path (macOS /var -> /private/var)
if real, err := filepath.EvalSymlinks(root); err == nil {
if rel, ok := under(real, abs); ok {
return rel
}
}
return filepath.ToSlash(p)
}

// golangciConfigIn is the golangci config file sitting directly in dir, or
// "" when there is none.
func golangciConfigIn(dir string) string {
for _, name := range golangciConfigNames {
p := filepath.Join(dir, name)
if _, err := os.Stat(p); err == nil {
return p
}
}
return ""
}

// under is abs relative to base as a slash path, false when it is outside.
func under(base, abs string) (string, bool) {
rel, err := filepath.Rel(base, abs)
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
return "", false
}
return filepath.ToSlash(rel), true
}

// pathModeSupport caches, per binary, whether `golangci-lint run` knows
// --path-mode: asked once per process, not once per module.
var pathModeSupport sync.Map // bin path -> bool

// supportsPathMode reports whether bin's `run --help` lists --path-mode.
// A binary that cannot even print its help is treated as not supporting it;
// the real run then fails (or not) on its own terms and says so.
func supportsPathMode(bin string) bool {
if v, ok := pathModeSupport.Load(bin); ok {
return v.(bool)
}
raw, _ := execute(filepath.Dir(bin), bin, []string{"run", "--help"})
ok := strings.Contains(raw, "--path-mode")
pathModeSupport.Store(bin, ok)
return ok
}
Loading
Loading