Repository: rollup/rollup (2025-07-12T11:10:50Z)

Issue:
bug from 4.45.0: Cannot add property 0, object is not extensible
### Rollup Version

4.45.0

### Operating System (or Browser)

mac

### Node Version (if applicable)

20

### Link To Reproduction

private project

### Expected Behaviour

build successfully.

### Actual Behaviour

Rollup works fine on Rollup@4.44.0, but throw the error below on Rollup@4.45.0:

```bash
error during build:
Cannot add property 0, object is not extensible
    at Array.push (<anonymous>)
    at ConditionalExpression.getLiteralValueAtPath (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:12270:45)
    at file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:5064:30
    at EntityPathTracker.withTrackedEntityAtPath (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:2014:24)
    at LocalVariable.getLiteralValueAtPath (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:5062:33)
    at Identifier.getLiteralValueAtPath (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:5207:48)
    at MemberExpression.getDynamicPropertyKey (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:7636:41)
    at MemberExpression.hasAccessEffect (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:7652:65)
    at MemberExpression.hasEffects (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:7433:18)
    at VariableDeclarator.hasEffects (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:12914:39)
    at VariableDeclaration.hasEffects (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:2805:32)
    at BlockStatement.hasEffects (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:6506:22)
    at IfStatement.hasEffects (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:12725:33)
    at IfStatement.shouldBeIncluded (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:2928:62)
    at BlockStatement.include (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:6518:56)
    at ArrowFunctionExpression.include (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:6715:19)
    at ArrowFunctionExpression.include (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:11447:15)
    at Graph.includeStatements (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:22548:36)
    at Graph.build (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:22487:14)
    at async file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:23172:13
    at async catchUnfinishedHookActions (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:22643:16)
    at async rollupInternal (file:///WorkSpace/node_modules/.pnpm/rollup@4.45.0/node_modules/rollup/dist/es/shared/node-entry.js:23167:5)
 
```


Discussion:
This is blocking any project we have in CI!
This is a regression that was fixed in 4.29.1. Lock your rollup version to 4.29.1 it should solve the issue.
Original issue: https://github.com/rollup/rollup/issues/5770
4.44.0 works well in my project
as work around, I tried to add this to package.json
```
  "pnpm": {
    "overrides": {
      "rollup": "4.43.0"
    }
  }
```
```
x Build failed in 19.16s
--
15:14:17.538 | error during build:
15:14:17.538 | [vite-plugin-pwa:build] [plugin vite-plugin-pwa:build] There was an error during the build:
15:14:17.538 | Cannot add property 0, object is not extensible
15:14:17.538 | Additionally, handling the error in the 'buildEnd' hook caused the following error:
15:14:17.539 | Cannot add property 0, object is not extensible
15:14:17.539 | at getRollupError (file:///opt/buildhome/repo/node_modules/.pnpm/@rollup+wasm-node@4.45.0/node_modules/@rollup/wasm-node/dist/es/shared/parseAst.js:401:41)
15:14:17.539 | at file:///opt/buildhome/repo/node_modules/.pnpm/@rollup+wasm-node@4.45.0/node_modules/@rollup/wasm-node/dist/es/shared/node-entry.js:23184:39
15:14:17.539 | at async catchUnfinishedHookActions (file:///opt/buildhome/repo/node_modules/.pnpm/@rollup+wasm-node@4.45.0/node_modules/@rollup/wasm-node/dist/es/shared/node-entry.js:22643:16)
15:14:17.539 | at async rollupInternal (file:///opt/buildhome/repo/node_modules/.pnpm/@rollup+wasm-node@4.45.0/node_modules/@rollup/wasm-node/dist/es/shared/node-entry.js:23167:5)
15:14:17.539 | at async build (file:///opt/buildhome/repo/node_modules/.pnpm/vite@5.4.19_@types+node@20.17.44_terser@5.43.1/node_modules/vite/dist/node/chunks/dep-C6uTJdX2.js:65693:14)
15:14:17.539 | at async CAC.<anonymous> (file:///opt/buildhome/repo/node_modules/.pnpm/vite@5.4.19_@types+node@20.17.44_terser@5.43.1/node_modules/vite/dist/node/cli.js:829:5)
15:14:17.580 | ELIFECYCLE  Command failed with exit code 1.
15:14:17.615 | node:events:652
15:14:17.616 | return this.listener.apply(this.target, arguments);
15:14:17.616 | ^
15:14:17.616 |  
15:14:17.616 | X [Error]: Error while executing user command. Exited with error code: 1

```

I hope this is resolved soon. 4.45.0 broke my project
This issue is critically blocking everything. No testing performed before the release of it?
We are also experiencing issues with this
> We are also experiencing issues with this

Same, and it's blocking our CI process, hope it will be fixed soon. (´-ω-`)
    "overrides": {
        "rollup": "4.44.0"
    },

in package.json, it helps.
// 添加构建配置来解决 Rollup 兼容性问题
    build: {
      // 设置块大小警告的限制
      chunkSizeWarningLimit: 2000,
      // 禁用源码映射
      sourcemap: false,
      // 设置 rollup 选项
      rollupOptions: {
        treeshake: false,  // 这是关键！
        output: {
          manualChunks: {
            vendor: ['vue', 'vue-router', 'pinia'],
            elementPlus: ['element-plus'],
            echarts: ['echarts']
          }
        }
      }
    }
This is a newly introduced bug caused by a treeshaking optimization for conditional expressions that was added in version 4.45.0.
There is an open PR #6009 that addresses this issue, and the fix will be included in an upcoming patch release once it's merged.
Before the patch is available, you can temporarily downgrade Rollup to version 4.44.2. 
If your project depends on Rollup indirectly, you can use the `overrides` field to pin the Rollup version. Different package managers may have different ways to configure this, you can refer to the following links:
[npm overrides](https://docs.npmjs.com/cli/v11/configuring-npm/package-json#overrides)
[pnpm overrides](https://pnpm.io/settings#overrides)
[yarn resoultions](https://yarnpkg.com/configuration/manifest#resolutions)
Tried overriding to 4.44.2 but it's not working it's still not downgrading using overrides, can you share your package.json where you have overridden the rollup @TrickyPi 
> Tried overriding to 4.44.2 but it's not working it's still not downgrading using overrides, can you share your package.json where you have overridden the rollup [@TrickyPi](https://github.com/TrickyPi)

my config
```js
"overrides": {
    "rollup": "4.44.2"
},
```

You may need to remove node_modules and reinstall packages. If you're using npm, you can run `npm ls rollup` to check whether the `overrides` are working.
using resolutions in package.json works for yarn users

"resolutions": {
    "rollup": "4.44.2"
},
Yeah, I guess your package manager is `Yarn`
i use npm，same error！
This is also making our whole project down
> This is a newly introduced bug caused by a treeshaking optimization for conditional expressions that was added in version 4.45.0. There is an open PR [#6009](https://github.com/rollup/rollup/pull/6009) that addresses this issue, and the fix will be included in an upcoming patch release once it's merged. Before the patch is available, you can temporarily downgrade Rollup to version 4.44.2. If your project depends on Rollup indirectly, you can use the `overrides` field to pin the Rollup version. Different package managers may have different ways to configure this, you can refer to the following links: [npm overrides](https://docs.npmjs.com/cli/v11/configuring-npm/package-json#overrides) [pnpm overrides](https://pnpm.io/settings#overrides) [yarn resoultions](https://yarnpkg.com/configuration/manifest#resolutions)

Please see here
> ```
> "overrides": {
>     "rollup": "4.44.0"
> },
> ```
> 
> in package.json, it helps.

thanks, i love you
for **yarn** users
```
  "resolutions": {
    "rollup": "4.44.0"
  }
```
This is actually quite a destructive issue that affected a few of my apps which were using Vite. I wasn't directly using Rollup. Please in future ensure these things are tested. It's important for a library as widely used as Rollup to ensure things don't get pushed out that have the potential to break a lot of apps like this.

Thanks to the posters above who detailed pinning the version.
Locally, I use npm, and it worked fine. However, in ci/cd we use yarn, and it was where the issue happened.

In my case, in order to fix the ci/cd, I updated the package.json as mentioned above, in the following way:

```
"resolutions": {
    "rollup": "4.44.2"
}
```

Thanks a lot!
All please stop posting more and more "me too" comments. Maintainers are aware of the problem. Please use the "Subscribe" button on top of this issue and wait for the fix, and do not spam all others that have already used the "Subscribe" button :)
You can solve this problem by downgrading rollup.

package.json
```json
"overrides": {
    "rollup": "4.44.1"
  },
  "resolutions": {
    "rollup": "4.44.1"
  }
```
I solved this problem by temporarily disabling tree-shaking in vite.config.mts

```
build: {
    rollupOptions: {
        treeshake: false
    }
}
```
> I solved this problem by temporarily disabling tree-shaking in vite.config.mts
> 
> ```
> build: {
>     rollupOptions: {
>         treeshake: false
>     }
> }
> ```

can confirm this is the only workaround that worked for us.
Using rollup-critical-plugin 1.0.15 (latest). 

I run `npm ls rollup` to see the exact rollup version vite is using: 

└─┬ vite@6.0.11
  └── rollup@4.32.1

Not sure if that means 4.32.1 is failing too, but adding override solved it for me and my build is successful.

` "overrides": {
    "rollup": "4.44.0" 
  }`
This issue has been resolved via #6009 as part of rollup@4.45.1. You can test it via `npm install rollup`.
for me it did not resolve with the PR (rollup@4.45.1)

When I run a build in my vite app, I still get: 

[vite-plugin-pwa:build] [plugin vite-plugin-pwa:build] There was an error during the build:
  Cannot add property 0, object is not extensible
Additionally, handling the error in the 'buildEnd' hook caused the following error:
  Cannot add property 0, object is not extensible

> for me it did not resolve with the PR (rollup@4.45.1)
> 
> When I run a build in my vite app, I still get:
> 
> [vite-plugin-pwa:build] [plugin vite-plugin-pwa:build] There was an error during the build: Cannot add property 0, object is not extensible Additionally, handling the error in the 'buildEnd' hook caused the following error: Cannot add property 0, object is not extensible

Hi! Please check the `rollup`version specified in your `package-lock.json`, or feel free to share it here so we can take a look.
Make sure that the version is set to `4.45.1`. If it isn't, try the following steps:

Delete your `node_modules` folder,

Clear the npm cache using `npm cache clean --force`,

Reinstall the dependencies — this should pull the correct `rollup` version.

Let us know if that helps!
Works now for me. Thanks!
Any solutions for Bun? I've deleted my bun lock and node_modules, set `"resolutions": { "rollup": "4.45.1" }`, as well as tried `override`, and after reinstalling, vite is still using an old version in my bun lock. I have also deleted the install cache.
> Any solutions for Bun? I've deleted my bun lock and node_modules, set `"resolutions": { "rollup": "4.45.1" }`, as well as tried `override`, and after reinstalling, vite is still using an old version in my bun lock. I have also deleted the install cache.

bun pm cache clean --force
> > Any solutions for Bun? I've deleted my bun lock and node_modules, set `"resolutions": { "rollup": "4.45.1" }`, as well as tried `override`, and after reinstalling, vite is still using an old version in my bun lock. I have also deleted the install cache.
> 
> bun pm cache clean --force

I had mentioned that I already cleared my install cache, to no avail. 
While I see the the overrides with the new rollup version in the bun lock file, Vite still has the old version. Does the override "override" packages at build time? I would've assumed at install but I'm sure I'm wrong.
I'm using 
```
"pnpm": {
	"overrides": {
		"rollup@^4.0.0": "^4.45.1"
	}
}
```

and that seems to have done the trick for all my direct and indirect dependencies (without accidentally messing up any (very) indirect dependency that could depend on rollup < v4
Using vite, this worked for me:
```
rm -rf node_modules/
rm package-lock.json 
npm cache clean --force
```

And then `npm install`



Patch:
diff --git a/src/ast/nodes/ConditionalExpression.ts b/src/ast/nodes/ConditionalExpression.ts
index cb8e6dd1da5..0400ac21ae3 100644
--- a/src/ast/nodes/ConditionalExpression.ts
+++ b/src/ast/nodes/ConditionalExpression.ts
@@ -34,6 +34,13 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
 		this.flags = setFlag(this.flags, Flag.isBranchResolutionAnalysed, value);
 	}
 
+	private get hasDeoptimizedCache(): boolean {
+		return isFlagSet(this.flags, Flag.hasDeoptimizedCache);
+	}
+	private set hasDeoptimizedCache(value: boolean) {
+		this.flags = setFlag(this.flags, Flag.hasDeoptimizedCache, value);
+	}
+
 	private expressionsToBeDeoptimized: DeoptimizableEntity[] = [];
 	private usedBranch: ExpressionNode | null = null;
 
@@ -47,6 +54,8 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
 	}
 
 	deoptimizeCache(): void {
+		if (this.hasDeoptimizedCache) return;
+		this.hasDeoptimizedCache = true;
 		if (this.usedBranch !== null) {
 			const unusedBranch = this.usedBranch === this.consequent ? this.alternate : this.consequent;
 			this.usedBranch = null;
@@ -79,6 +88,9 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz
 	): LiteralValueOrUnknown {
 		const usedBranch = this.getUsedBranch();
 		if (!usedBranch) {
+			if (this.hasDeoptimizedCache) {
+				return UnknownValue;
+			}
 			const consequentValue = this.consequent.getLiteralValueAtPath(path, recursionTracker, origin);
 			const castedConsequentValue = tryCastLiteralValueToBoolean(consequentValue);
 			if (castedConsequentValue === UnknownValue) return UnknownValue;


Test patch:
diff --git a/test/function/samples/conditional-expression-literal-value/_config.js b/test/function/samples/conditional-expression-literal-value/_config.js
new file mode 100644
index 00000000000..4df2805d473
--- /dev/null
+++ b/test/function/samples/conditional-expression-literal-value/_config.js
@@ -0,0 +1,4 @@
+module.exports = {
+	description:
+		'do not optimize the literal value of conditional expression if the cache is deoptimized'
+};
diff --git a/test/function/samples/conditional-expression-literal-value/main.js b/test/function/samples/conditional-expression-literal-value/main.js
new file mode 100644
index 00000000000..707bfd551e1
--- /dev/null
+++ b/test/function/samples/conditional-expression-literal-value/main.js
@@ -0,0 +1,8 @@
+let count = 0;
+let foo = false;
+let bar = false;
+let condition = foo ? bar ? true: false : false;
+if (!condition) count++;
+foo = true;
+if (!condition) count++;
+assert.strictEqual(count, 2);
\ No newline at end of file