Skip to content

Terser nameCache is broken by multi-threaded mode, causing object key–value mismatches in different chunks #20751

@AntaresQAQ

Description

@AntaresQAQ

Describe the bug

When nameCache is provided, it gets cloned for each worker thread, resulting in isolated caches that are not synchronized back to the main thread.
This inconsistency can cause key–value mismatches for the same type of object in different chunks, making the nameCache ineffective and leading to incorrect minification results.

Reproduction

https://github.com/AntaresQAQ/vite-terser-bug-repro

Steps to reproduce

Vite Config

import { defineConfig } from "vite";

const nameCache = {};

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    {
      name: "terser-mangle-props-cache",
      closeBundle() {
        console.log("Name cache:", nameCache);
      },
    },
  ],
  build: {
    minify: "terser",
    terserOptions: {
      compress: false,
      mangle: {
        properties: true,
      },
      nameCache,
    },
  },
});

Codes

obj1.ts

import type { IDemoObj } from "./types";
const obj: IDemoObj = {
  baz: "baz",
  foo: "foo",
  qux: "qux",
  bar: "bar",
};
export default obj;

src/obj2.ts

import type { IDemoObj } from "./types";
const obj: IDemoObj = {
  foo: "foo",
  bar: "bar",
  baz: "baz",
  qux: "qux",
};
export default obj;

Built files

  1. chunk obj1-Ds7_RE47.js
const a={o:"baz",t:"foo",u:"qux",q:"bar"};export{a as default};
Image
  1. chunk obj2-DqD_p4d0.js
const a={o:"foo",t:"bar",u:"baz",q:"qux"};export{a as default};
Image

Vite outputs

yarn run v1.22.22
$ tsc -b && vite build
vite v7.1.5 building for production...
✓ 6 modules transformed.
dist/index.html                0.39 kB │ gzip: 0.27 kB
dist/assets/obj1-Ds7_RE47.js   0.06 kB │ gzip: 0.08 kB
dist/assets/obj2-DqD_p4d0.js   0.06 kB │ gzip: 0.08 kB
dist/assets/index-BJnHZlGe.js  2.21 kB │ gzip: 1.03 kB
✓ built in 171ms
Name cache: {}
✨  Done in 0.84s.

Expected Result

  • the obj1 is the same as obj2.
  • the nameCache is not empty.

System Info

System:
    OS: macOS 15.6.1
    CPU: (10) arm64 Apple M2 Pro
    Memory: 263.14 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.16.0 - ~/.nvm/versions/node/v22.16.0/bin/node
    Yarn: 1.22.22 - ~/.nvm/versions/node/v22.16.0/bin/yarn
    npm: 10.9.2 - ~/.nvm/versions/node/v22.16.0/bin/npm
  Browsers:
    Chrome: 140.0.7339.133
    Safari: 18.6
  npmPackages:
    vite: ^7.1.2 => 7.1.5

Used Package Manager

yarn

Logs

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p2-edge-caseBug, but has workaround or limited in scope (priority)

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions