fix: M._cache holds stale reference after cache.load() #266

Closed
opened 2026-02-27 00:21:57 +00:00 by barrettruth · 0 comments
barrettruth commented 2026-02-27 00:21:57 +00:00

Description

In lua/cp/cache.lua, M._cache = cache_data is evaluated at module load time, capturing a reference to the initial empty {}. When M.load() is called and reassigns cache_data = decoded, the module-level M._cache field still points to the original empty table, not the live data.

local cache_data = {}          -- initial empty table, let's call it T1
...
function M.load()
  ...
  cache_data = decoded          -- cache_data now points to T2 (decoded data)
  ...
end

M._cache = cache_data           -- this captures T1, not T2

Any external code using require('cp.cache')._cache for inspection/debugging will see an empty table regardless of what has been loaded.

Steps to reproduce

  1. Call require('cp.cache').load()
  2. Inspect require('cp.cache')._cache — it is {}
  3. Inspect require('cp.cache').get_data_pretty() — this returns actual data (uses module-local cache_data correctly)

Proposed fix

Either remove M._cache entirely (it's undocumented and only useful for debugging, covered better by get_data_pretty()), or change it to a getter function:

function M._get_cache()
  return cache_data
end
## Description In `lua/cp/cache.lua`, `M._cache = cache_data` is evaluated at module load time, capturing a reference to the initial empty `{}`. When `M.load()` is called and reassigns `cache_data = decoded`, the module-level `M._cache` field still points to the original empty table, not the live data. ```lua local cache_data = {} -- initial empty table, let's call it T1 ... function M.load() ... cache_data = decoded -- cache_data now points to T2 (decoded data) ... end M._cache = cache_data -- this captures T1, not T2 ``` Any external code using `require('cp.cache')._cache` for inspection/debugging will see an empty table regardless of what has been loaded. ## Steps to reproduce 1. Call `require('cp.cache').load()` 2. Inspect `require('cp.cache')._cache` — it is `{}` 3. Inspect `require('cp.cache').get_data_pretty()` — this returns actual data (uses module-local `cache_data` correctly) ## Proposed fix Either remove `M._cache` entirely (it's undocumented and only useful for debugging, covered better by `get_data_pretty()`), or change it to a getter function: ```lua function M._get_cache() return cache_data end ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
barrettruth/cp.nvim#266
No description provided.