Skip to content

fix(ssr): ssrTransform incorrectly rewrites meta identifier inside import.meta when a binding named meta exists - #22019

Merged
sapphi-red merged 2 commits into
mainfrom
copilot/fix-ssrtransform-import-meta-issue
Mar 25, 2026
Merged

fix(ssr): ssrTransform incorrectly rewrites meta identifier inside import.meta when a binding named meta exists#22019
sapphi-red merged 2 commits into
mainfrom
copilot/fix-ssrtransform-import-meta-issue

Conversation

Copilot AI commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

When an imported binding was named meta, ssrTransform would incorrectly replace the meta in import.meta with the import binding accessor, corrupting the output.

Root Cause

isRefIdentifier had no guard for MetaProperty parent nodes. The meta identifier in import.meta has a MetaProperty parent, but the function only excluded MemberExpression non-computed properties — so meta was treated as a rewritable reference.

Fix

  • Added a MetaProperty parent check to isRefIdentifier, returning false to prevent rewriting identifiers that are part of import.meta.
// Before — broken
import { meta } from './meta';
console.log(import.meta.url, `Hello, ${meta}!`);
// Transformed to (wrong):
// console.log(__vite_ssr_import_meta__.url, `Hello, ${__vite_ssr_import_0__.meta}!`)
// ^ meta in import.meta was also getting rewritten incorrectly

// After — correct
// console.log(__vite_ssr_import_meta__.url, `Hello, ${__vite_ssr_import_0__.meta}!`)
Original prompt

This section details on the original issue you should resolve

<issue_title>ssrTransform fails to transform import.meta when there is imported variable named meta</issue_title>
<issue_description>### 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/vite@8.0.2/node_modules/vite/dist/node/chunks/node.js:4738:10)
    at MagicString._split (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/vite@8.0.2/node_modules/vite/dist/node/chunks/node.js:4729:43)
    at MagicString.update (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/vite@8.0.2/node_modules/vite/dist/node/chunks/node.js:4553:8)
    at onIdentifier (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/vite@8.0.2/node_modules/vite/dist/node/chunks/node.js:11324:75)
    at eval (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/vite@8.0.2/node_modules/vite/dist/node/chunks/node.js:11436:37)
    at walk (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/vite@8.0.2/node_modules/vite/dist/node/chunks/node.js:11435:14)
    at ssrTransformScript (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/vite@8.0.2/node_modules/vite/dist/node/chunks/node.js:11301:2)
    at async loadAndTransform (file:///home/projects/vitejs-vite-5acvpgpu/node_modules/.pnpm/vite@8.0.2/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

Comments on the Issue (you are @copilot in this section)


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat: ssr p3-minor-bug An edge case that only affects very specific usage (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants