Skip to content

Commit 43373fc

Browse files
committed
fix(persist): warn on session data decode failure instead of silent fallback
Add path-prefixed warning when JSON decode fails in store.read and store.read_sync, so users can identify corrupted session files instead of seeing sessions silently disappear.
1 parent 92b2c4b commit 43373fc

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

lua/peekstack/persist/store.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ function M.read(scope, opts)
7272
vim.schedule(function()
7373
local ok, decoded = pcall(vim.json.decode, data)
7474
if not ok or type(decoded) ~= "table" then
75+
notify.warn("Failed to decode session data: " .. path)
7576
on_done(empty_data())
7677
return
7778
end
@@ -104,6 +105,7 @@ function M.read_sync(scope)
104105

105106
local ok, decoded = pcall(vim.json.decode, data)
106107
if not ok or type(decoded) ~= "table" then
108+
notify.warn("Failed to decode session data: " .. path)
107109
return empty_data()
108110
end
109111
return decoded

tests/persist_store_spec.lua

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,24 @@ describe("peekstack.persist.store", function()
120120
assert.same(data, result)
121121
end)
122122

123-
it("returns empty data for invalid JSON", function()
123+
it("returns empty data and warns for invalid JSON", function()
124124
local path = fs.scope_path(test_scope)
125125
write_raw_and_wait(path, "{ invalid json")
126126

127+
local warnings = {}
128+
local original_notify = vim.notify
129+
vim.notify = function(msg, level)
130+
if level == vim.log.levels.WARN then
131+
table.insert(warnings, msg)
132+
end
133+
end
134+
127135
local result = read_and_wait(test_scope)
136+
137+
vim.notify = original_notify
128138
assert.same({ version = 2, sessions = {} }, result)
139+
assert.is_true(#warnings > 0, "should warn about decode failure")
140+
assert.is_true(warnings[1]:find("Failed to decode", 1, true) ~= nil)
129141
end)
130142

131143
it("read_sync returns data for valid store content", function()
@@ -144,12 +156,24 @@ describe("peekstack.persist.store", function()
144156
assert.same(data, result)
145157
end)
146158

147-
it("read_sync returns empty data for invalid JSON", function()
159+
it("read_sync returns empty data and warns for invalid JSON", function()
148160
local path = fs.scope_path(test_scope)
149161
write_raw_and_wait(path, "{ invalid json")
150162

163+
local warnings = {}
164+
local original_notify = vim.notify
165+
vim.notify = function(msg, level)
166+
if level == vim.log.levels.WARN then
167+
table.insert(warnings, msg)
168+
end
169+
end
170+
151171
local result = store.read_sync(test_scope)
172+
173+
vim.notify = original_notify
152174
assert.same({ version = 2, sessions = {} }, result)
175+
assert.is_true(#warnings > 0, "should warn about decode failure")
176+
assert.is_true(warnings[1]:find("Failed to decode", 1, true) ~= nil)
153177
end)
154178

155179
it("write_sync stores data that can be read back", function()

tests/setup_idempotent_spec.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ describe("peekstack.setup idempotent", function()
3535
providers = {
3636
marks = {
3737
enable = true,
38-
scope = "buffer",
3938
},
4039
},
4140
})

0 commit comments

Comments
 (0)