Skip to content

ssrTransform fails to transform import.meta when there is imported variable named meta #22004

@uhyo

Description

@uhyo

Describe the bug

ssrTransform fails to transform code below:

import { meta } from './meta';

console.log(import.meta.url, `Hello, ${meta}!`);
Error message
Error: Cannot split a chunk that has already been edited (1:19 – "import.meta")
    at MagicString._splitChunk (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/node.js:4738:10)
    at MagicString._split (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/node.js:4729:43)
    at MagicString.update (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/node.js:4553:8)
    at onIdentifier (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/node.js:11324:75)
    at eval (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/node.js:11436:37)
    at walk (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/node.js:11435:14)
    at ssrTransformScript (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/node.js:11301:2)
    at async loadAndTransform (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/node.js:24317:64)

Looks like it hits the below s.update(id.start, id.end, binding) call and tries to replace meta in import.meta with something else. Need to handle a MetaProperty parent.

s.update(id.start, id.end, binding)

For context, I encountered this bug while using Storybook Vitest addon.

Reproduction

https://stackblitz.com/edit/vitejs-vite-5acvpgpu?file=repro.ts

Steps to reproduce

vite.config.ts:

import { defineConfig } from "vite";

export default defineConfig({
  environments: {
    ssr: {},
  },
  builder: {
    async buildApp() { }
  }
})

repro.ts:

import { createServer } from "vite"

const server = await createServer()
const result = await server.environments.ssr.transformRequest("src/index.ts");

console.log(result?.code);

await server.close();

src/index.ts:

import { meta } from './meta';

console.log(import.meta.url, `Hello, ${meta}!`);

Run node repro.ts to see the error.

System Info

Both Vite 7 and 8 have this issue.

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 22.22.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Labels

feat: ssrp3-minor-bugAn edge case that only affects very specific usage (priority)

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions