Skip to content

fix(detector): sort previous JSON dirs in descending order for diff#2555

Merged
MaineK00n merged 2 commits into
masterfrom
MaineK00n/fix-detector-loadprevious-sort
May 19, 2026
Merged

fix(detector): sort previous JSON dirs in descending order for diff#2555
MaineK00n merged 2 commits into
masterfrom
MaineK00n/fix-detector-loadprevious-sort

Conversation

@MaineK00n
Copy link
Copy Markdown
Collaborator

@MaineK00n MaineK00n commented May 18, 2026

Summary

Fixes #2536report --diff-plus keeps re-sending the same unfixed alerts because it compares the current scan against an ancient one (the second-oldest result dir) instead of the most recent previous scan.

Root cause

detector.ListValidJSONDirs sorts ascending, so dirs[0] is the oldest dir. loadPrevious then iterates dirs[1:] and breaks on the first matching JSON, which makes it pick the second-oldest as "previous". The function's own doc comment says the opposite ("recent directories are at the head"), and the sibling reporter.ListValidJSONDirs sorts descending.

This is a regression from #2415 (Go 1.26 bump), where the original

sort.Slice(dirs, func(i, j int) bool { return dirs[j] < dirs[i] })  // descending

was modernized to

slices.Sort(dirs)  // ascending — wrong direction

Fix

Match reporter/util.go's descending sort:

slices.SortFunc(dirs, func(a, b string) int {
    return -cmp.Compare(a, b)
})

Verification — before / after

Added TestListValidJSONDirs_SortedDescending that recreates the directory list from the discussion and asserts dirs[0] is the newest. Running the same test against master vs. this branch:

Before (on master, broken sort):

=== RUN   TestListValidJSONDirs_SortedDescending
    util_test.go:60: dirs[0] = .../2026-04-24T08-22-31+0000, want .../2026-05-01T15-33-21+0000
    util_test.go:60: dirs[1] = .../2026-04-25T08-27-25+0000, want .../2026-05-01T15-17-13+0000
    util_test.go:60: dirs[2] = .../2026-04-25T17-52-14+0000, want .../2026-05-01T12-57-29+0000
    ... (every position wrong, fully reversed)
    util_test.go:60: dirs[11] = .../2026-05-01T15-33-21+0000, want .../2026-04-24T08-22-31+0000
--- FAIL: TestListValidJSONDirs_SortedDescending (0.01s)
FAIL    github.com/future-architect/vuls/detector

Note that dirs[1] = 2026-04-25T08-27-25+0000exactly the path the discussion reporter saw in Previous json found: ....

After (this branch):

=== RUN   TestListValidJSONDirs_SortedDescending
--- PASS: TestListValidJSONDirs_SortedDescending (0.00s)
PASS
ok      github.com/future-architect/vuls/detector       0.032s

Test plan

  • go build ./...
  • go test ./detector/ -run TestListValidJSONDirs_SortedDescending -v — PASS on this branch, FAIL on master (see above)

🤖 Generated with Claude Code

References

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes report --diff-plus selecting an older “previous” scan by ensuring detector.ListValidJSONDirs returns result directories in descending (newest-first) order, matching the documented behavior and reporter.ListValidJSONDirs.

Changes:

  • Sort detector.ListValidJSONDirs results in descending order using slices.SortFunc + cmp.Compare.
  • Add a unit test ensuring the returned directory list is newest-first and ignores non-timestamp dirs.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
detector/util.go Changes directory sorting to newest-first so diff logic selects the correct prior scan.
detector/util_test.go Adds coverage verifying descending sort order and filtering of invalid directory names.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@MaineK00n MaineK00n requested a review from shino May 18, 2026 09:53
MaineK00n and others added 2 commits May 18, 2026 22:13
ListValidJSONDirs in detector was switched from a descending sort.Slice
to slices.Sort in #2415, which sorts ascending. The doc comment and
loadPrevious both assume descending order — loadPrevious takes dirs[1:]
expecting the second-newest dir as the previous scan, but with the
ascending sort it picks the second-oldest, so report --diff-plus
compares against an ancient scan instead of the latest prior one.

Match reporter/util.go's ListValidJSONDirs by sorting descending.

Reported in #2536

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Reproduces the scenario from discussion #2536 — when many timestamped
result dirs exist, the newest must come first so loadPrevious picks
the second-newest as the previous scan.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@MaineK00n MaineK00n force-pushed the MaineK00n/fix-detector-loadprevious-sort branch from 07a16b2 to 7df1c49 Compare May 18, 2026 13:13
Copy link
Copy Markdown
Collaborator

@shino shino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥂

@MaineK00n MaineK00n merged commit da0e250 into master May 19, 2026
7 checks passed
@MaineK00n MaineK00n deleted the MaineK00n/fix-detector-loadprevious-sort branch May 19, 2026 05:09
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.

3 participants