Skip to content

fix: localStorage exception in Node 25+#60

Merged
chenjiahan merged 1 commit intomainfrom
global_clone_0326
Mar 26, 2026
Merged

fix: localStorage exception in Node 25+#60
chenjiahan merged 1 commit intomainfrom
global_clone_0326

Conversation

@chenjiahan
Copy link
Member

Copilot AI review requested due to automatic review settings March 26, 2026 07:56
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a compatibility issue with Node.js 25+ environments where the vmContext creation could fail if localStorage was not explicitly enabled. The changes refactor how the global context is prepared for the VM, ensuring robustness and preventing runtime exceptions in newer Node.js versions.

Highlights

  • Node.js 25+ Compatibility: Modified the vmContext creation to prevent exceptions related to localStorage when running in Node.js 25 and above.
  • Refactored vmContext Creation: Replaced the direct spread of global into the vmContext with a more controlled approach, cloning global properties and explicitly adding necessary ones.
  • Removed eval from Context: Deleted the eval property from the cloned global context passed to vmContext to address potential issues.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@chenjiahan chenjiahan merged commit 7c01e5f into main Mar 26, 2026
5 checks passed
@chenjiahan chenjiahan deleted the global_clone_0326 branch March 26, 2026 07:57
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the creation of the vmContext within the HtmlRspackPlugin. The change involves creating a deep clone of the global object, explicitly removing the eval property to prevent conflicts, and then assigning specific plugin-related and environment variables. This addresses potential issues with localStorage in Node 25+ and eval conflicts when directly spreading the global object. There are no review comments to address.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fc1b782496

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +517 to +520
const globalClone = Object.create(
Object.getPrototypeOf(global),
Object.getOwnPropertyDescriptors(global),
);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Avoid cloning host globalThis into VM context

Copying all descriptors from global also copies the non-enumerable globalThis data property, whose value is the host process global object. In a vm context this makes globalThis inside templates point to the outer global (globalThis !== this), so template code that uses globalThis cannot see injected context values like HTML_WEBPACK_PLUGIN and can accidentally read/mutate host-global state. This is a behavior regression from the previous ...global approach, which did not copy globalThis.

Useful? React with 👍 / 👎.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes template-evaluation failures on newer Node versions (25+) by changing how the VM sandbox is constructed, avoiding eager access to globals (notably localStorage) that can throw during cloning.

Changes:

  • Replace ...global-based sandbox creation with a global clone built from property descriptors.
  • Remove eval from the cloned sandbox to avoid interfering with template eval behavior.
  • Keep nonstandard globals (require, __filename, __dirname, htmlWebpackPluginPublicPath) explicitly injected into the VM context.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +517 to +535
const globalClone = Object.create(
Object.getPrototypeOf(global),
Object.getOwnPropertyDescriptors(global),
);

// Presence of `eval` breaks template's explicit `eval` call, might be a bug in Node
delete globalClone.eval;

// Not using `...global` as it throws when localStorage is not explicitly enabled in Node 25+
const vmContext = vm.createContext(
Object.assign(globalClone, {
HTML_WEBPACK_PLUGIN: true,
// Copying nonstandard globals like `require` explicitly as they may be absent from `global`
require: require,
htmlWebpackPluginPublicPath: publicPath,
__filename: templateWithoutLoaders,
__dirname: path.dirname(templateWithoutLoaders),
}),
);
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change alters the VM context creation to avoid triggering throwing getters (e.g. Node 25+ localStorage). There’s no regression test ensuring evaluateCompilationResult doesn’t throw when an enumerable global property getter throws during context setup. Consider adding a Jest test that temporarily defines global.localStorage (or another property) as an enumerable getter that throws, then runs a minimal template through evaluateCompilationResult/a compilation to assert it still succeeds (and cleans up the property after).

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants