Skip to content

V3 breaks dynamic imports #7361

@Jason3S

Description

@Jason3S

Describe the bug

V3 seems to be changing how dynamic imports work.

Imports in the following style seem to be incorrectly cached.

const lib = await import('<url#hash1>');
// ...
const libUpdated = await import('<url#hash2>');
assert(lib !== libUpdated);

Note: This works correctly in V2.

Might be related to: #7269, #7266, but it doesn't seem to be fixed in v3.0.4.

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-1rbfzzpd?file=test%2Fsuite.test.ts

describe('suite name', () => {
  it('validates that async import works as expected.', async () => {
    const hello0 = await import(urlHelloJs.href);
    expect(hello0['sayHello']).toBeTypeOf('function');
  });

  it('validates that async import with hash change will reload.', async () => {
    const hello0 = await import(urlHelloJs.href);
    const url1 = new URL(urlHelloJs);
    url1.hash = 'bustCache=1';
    const hello1 = await import(url1.href);
    expect(hello1['sayHello']).toBeTypeOf('function');
    expect(hello1).not.toBe(hello0);
  });

  it('validates that async import with query change will reload.', async () => {
    const hello0 = await import(urlHelloJs.href);
    const url1 = new URL(urlHelloJs);
    url1.searchParams.set('bustCache', '1');
    const hello1 = await import(url1.href);
    expect(hello1['sayHello']).toBeTypeOf('function');
    expect(hello1).not.toBe(hello0);
  });
});

System Info

Note: it breaks on Linux and Windows as well.

  System:
    OS: macOS 15.2
    CPU: (12) arm64 Apple M3 Pro
    Memory: 182.05 MB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.18.1 - ~/.nvm/versions/node/v20.18.1/bin/node
    npm: 10.8.2 - ~/.nvm/versions/node/v20.18.1/bin/npm
    pnpm: 9.15.0 - ~/.nvm/versions/node/v20.18.1/bin/pnpm
  Browsers:
    Chrome: 132.0.6834.110
    Edge: 132.0.2957.127
    Safari: 18.2
  npmPackages:
    @vitest/coverage-istanbul: ^3.0.4 => 3.0.4 
    vite: ^6.0.11 => 6.0.11 
    vitest: ^3.0.4 => 3.0.4

Used Package Manager

pnpm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions