Skip to content

fix: drop missing files from state when content fetch hits ENOENT#196

Merged
k1LoW merged 2 commits intomainfrom
fix/drop-missing-files-from-state
Apr 30, 2026
Merged

fix: drop missing files from state when content fetch hits ENOENT#196
k1LoW merged 2 commits intomainfrom
fix/drop-missing-files-from-state

Conversation

@k1LoW
Copy link
Copy Markdown
Owner

@k1LoW k1LoW commented Apr 30, 2026

Summary

  • Fix the lingering sidebar entry that shows "Failed to load file." after a file is deleted from disk while the server is running (fsnotify can miss the Remove event on macOS).
  • When os.ReadFile in handleFileContent fails with os.IsNotExist, call the new RemoveFilesByPath to drop every entry referencing that path from all groups; groups left empty without patterns are deleted.
  • The watcher entry is also unregistered and an update SSE event is emitted so the frontend re-syncs immediately.

When a file is deleted from disk while the server is running, the watcher
sometimes misses the event (e.g. fsnotify on macOS) and the entry lingers
in the sidebar showing "Failed to load file." Detect ENOENT during the
content fetch and remove every reference to that path across all groups.
Empty groups without patterns are dropped.
@github-actions

This comment has been minimized.

This comment was marked as outdated.

The slice is reused with g.Files[:0], so without zeroing the truncated
tail the removed entries stay reachable through the backing array until
the slice grows again. Nil out the tail explicitly.
@github-actions
Copy link
Copy Markdown
Contributor

Code Metrics Report

main (c9b5218) #196 (39e45cd) +/-
Coverage 57.7% 58.0% +0.3%
Code to Test Ratio 1:0.5 1:0.5 -0.1
Test Execution Time 35s 35s 0s
Details
  |                     | main (c9b5218) | #196 (39e45cd) |  +/-  |
  |---------------------|----------------|----------------|-------|
+ | Coverage            |          57.7% |          58.0% | +0.3% |
  |   Files             |             44 |             44 |     0 |
  |   Lines             |           3724 |           3761 |   +37 |
+ |   Covered           |           2149 |           2184 |   +35 |
- | Code to Test Ratio  |          1:0.5 |          1:0.5 |  -0.1 |
  |   Code              |           5959 |           5999 |   +40 |
  |   Test              |           3077 |           3077 |     0 |
  | Test Execution Time |            35s |            35s |    0s |

Code coverage of files in pull request scope (78.6% → 79.0%)

Files Coverage +/- Status
internal/server/server.go 79.0% +0.4% modified

Reported by octocov

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

This PR improves server-side state hygiene when a markdown file disappears from disk while the server is running (notably when fsnotify misses a remove event on macOS), ensuring the UI doesn’t retain a “Failed to load file.” entry indefinitely.

Changes:

  • Add State.RemoveFilesByPath to remove all entries referencing a given path across all groups, clean up watches, and drop now-empty groups without patterns.
  • Update handleFileContent to treat os.IsNotExist from os.ReadFile as a signal to remove the file from state and return 404.
  • Add tests covering removal-on-content-fetch and cross-group cleanup behavior.

Reviewed changes

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

File Description
internal/server/server.go Adds removal-by-path state cleanup and integrates it into the file content API when reads hit ENOENT.
internal/server/server_test.go Adds tests ensuring missing-file content fetches remove stale entries and empty groups are cleaned up.

@k1LoW k1LoW added the enhancement New feature or request label Apr 30, 2026
@k1LoW k1LoW self-assigned this Apr 30, 2026
@k1LoW k1LoW merged commit 6fceb64 into main Apr 30, 2026
7 checks passed
@k1LoW k1LoW deleted the fix/drop-missing-files-from-state branch April 30, 2026 12:37
@github-actions github-actions Bot mentioned this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants