Meta is not set when storage item with migrations defined is initially created
Describe the bug
Given that there's an item definition with migrations (https://wxt.dev/storage), for example
const item = storage.defineItem<{
value: string;
}>('local:data', {
version: 2,
migrations: {
2: () => {
// some code
},
},
});
Assume that user installed extension for the first time (i.e. there's no version 1 in their storage).
Setting any value to this item will store it without version metadata, which will lead to migration run when refreshing the page (if e.g. it's accessed in some content script).
This is usually incorrect cause migration code assumes data has old format, which never existed and was never set for a fresh install.
Reproduction
A PR with a failing test case: https://github.com/wxt-dev/wxt/pull/1774
Steps to reproduce
No response
System Info
System:
OS: Windows 11 10.0.26100
CPU: (16) x64 Intel(R) Core(TM) i7-10700KF CPU @ 3.80GHz
Memory: 34.40 GB / 63.87 GB
Binaries:
Node: 22.14.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.22 - C:\Program Files\nodejs\yarn.CMD
npm: 11.4.2 - C:\Program Files\nodejs\npm.CMD
pnpm: 9.1.4 - C:\Program Files\nodejs\pnpm.CMD
Browsers:
Edge: Chromium (133.0.3065.82)
Internet Explorer: 11.0.26100.1882
Used Package Manager
npm
Validations
- [x] Read the Contributing Guidelines.
- [x] Read the docs.
- [x] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- [x] Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- [x] The provided reproduction is a minimal reproducible example of the bug.
TLDR: given empty storage, setValue() followed by migrate() should not run any migrations. Here's somewhat cleaner version of the test from PR
const migrateToV2 = vi.fn();
let item = storage.defineItem<string>('local:data', {
version: 2,
migrations: {
2: migrateToV2,
},
});
await item.setValue("test");
await item.migrate()
expect(migrateToV2).toBeCalledTimes(0);
~~I did some limited testing and it seems like it works correctly when setting init and then getting the metadata after the item def is run https://wxt.dev/storage.html#default-values.~~
edit: nevermind this does not work