wxt icon indicating copy to clipboard operation
wxt copied to clipboard

Meta is not set when storage item with migrations defined is initially created

Open absdjfh opened this issue 7 months ago • 2 comments

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

absdjfh avatar Jun 26 '25 17:06 absdjfh

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);

absdjfh avatar Jun 26 '25 22:06 absdjfh

~~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

ShoeBoom avatar Oct 16 '25 09:10 ShoeBoom