Skip to content

fix(ls-metrics): report instead of panicking when nothing lints - #1144

Open
VXNCXNX wants to merge 1 commit into
vale-cli:v3from
VXNCXNX:fix/ls-metrics-empty-panic
Open

fix(ls-metrics): report instead of panicking when nothing lints#1144
VXNCXNX wants to merge 1 commit into
vale-cli:v3from
VXNCXNX:fix/ls-metrics-empty-panic

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

What's broken

vale ls-metrics panics on any path that lints to zero files.

$ mkdir /tmp/vempty
$ vale ls-metrics /tmp/vempty
panic: runtime error: index out of range [0] with length 0
	main.printMetrics(...) cmd/vale/command.go:239
	main.main() cmd/vale/main.go:145

The guard above it is system.FileExists(args[0]), which is os.Stat(f) == nil and therefore true for a directory. linter.Lint returns an empty slice when nothing matches, and linted[0] is unchecked.

A single non-lintable file is fine, vale ls-metrics pic.png prints {}, which is exactly what makes this look covered. The reachable cases are an empty directory, a directory of only empty subdirectories, and a directory whose files all sit under a default-ignored path such as node_modules or .git.

The fix

Guard the index and return an error naming the situation:

$ vale ls-metrics /tmp/vempty
E100 [ls-metrics] Runtime error

'/tmp/vempty' contains no lintable files

An error rather than {}, because testdata/e2e/metrics.yaml already pins ls-metrics test.txt to exit 0 with {} for a real file Vale read and found no metrics in. Reusing {} for "Vale read nothing at all" would conflate the two and hide the mistake. core.NewE100 is what the sibling commands in command.go use, and handleError renders it and exits 2.

Verification

An empty-directory case in testdata/e2e/metrics.yaml, run by internal/e2e/e2e_test.go. It uses the suite's inline files: mechanism, since git cannot check in an empty directory, and puts the only file under node_modules so the walker skips it and the lint comes back empty.

With the guard reverted the case fails with the panic above instead of the expected output. go test ./... passes, and golangci-lint run ./cmd/... reports the same single pre-existing gosec issue with and without the change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant