Skip to content

fix: keep calcdirsize results when directory is reloaded - #2690

Open
tk1475 wants to merge 2 commits into
gokcehan:masterfrom
tk1475:fix-calcdirsize-reload
Open

tk1475 wants to merge 2 commits into
gokcehan:masterfrom
tk1475:fix-calcdirsize-reload

Conversation

@tk1475

@tk1475 tk1475 commented Sep 28, 2026

Copy link
Copy Markdown

Fixes #2675.

set dircounts false calls nav.renew(), which starts an async reload of the directory because the dircounts value changed. calcdirsize then runs right away and stores the sizes on the current file objects. When the reloaded directory arrives on dirChan, it replaces the cached one with fresh file objects whose dirSize is -1, so the sizes flash on screen and then turn back into -.

This adds a small copyDirSizes helper on dir that carries over already calculated sizes by path for directories that are still present, and calls it in the dirChan handler when an existing directory is replaced (before sorting, so sortby size keeps using them).

Testing

  • New TestCopyDirSizes in nav_test.go. With a no-op helper it fails (at "a" expected dirSize 42 but got -1), and passes with the fix.
  • Reproduced end to end by driving the built binary in a pseudo terminal with map C :set dircounts false; calcdirsize and pressing C once: on master the directory shows -, with this change it shows 3.0M.
  • go fmt, go vet ./..., GOOS=windows go vet ./..., go test ./..., golangci-lint run ./... (0 issues).

This change was investigated and written with the help of an AI assistant (Claude Code); the checks above were run locally.

🤖 Generated with Claude Code

Reloading a directory (e.g. after toggling `dircounts`) creates new file
objects, so sizes calculated via `calcdirsize` on the old objects were
lost once the reloaded directory replaced the cached one. Carry them
over by path for files that are still present.

Fixes gokcehan#2675

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@valoq

valoq commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

The fix works for #2675 but sizes are copied by path only. If a directory is deleted and created again or a file is added to it, it keeps its old size after a reload. you could copy the size only if the directory's modification time did not change.

Compare the modification time of each directory with the previous load
and drop the cached size when it differs, so a directory that was
recreated or had entries added or removed gets recalculated.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@tk1475

tk1475 commented Sep 29, 2026

Copy link
Copy Markdown
Author

@valoq thanks, good point. Pushed d8078b0: a cached size is now only copied over if the directory's modification time matches the previous load, so a recreated directory or one with entries added or removed gets recalculated. The test covers the changed mtime case too.

One limit: a directory's mtime only changes when its direct entries change, so a file added deeper in the tree (say a/b/new) keeps the old size for a until calcdirsize is run again. Checking that would mean walking the tree on every reload, which is what calcdirsize is there to avoid. Happy to change it if you'd prefer a different trade-off.

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.

Synchronicity issue with set dircounts false; calcdirsize

2 participants