Skip to content

fix(entrypoint-loader): define location in the entrypoint environment - #2611

Merged
aklinker1 merged 3 commits into
wxt-dev:mainfrom
ethanstoner:fix/entrypoint-env-location
Oct 4, 2026
Merged

aklinker1 merged 3 commits into
wxt-dev:mainfrom
ethanstoner:fix/entrypoint-env-location

Conversation

@ethanstoner

Copy link
Copy Markdown
Contributor

Closes #2601.

What was wrong

getBrowserEnvironmentGlobals hands entrypoint evaluation LinkeDOM's window and document, but no location. Confirmed against linkedom 0.18.13:

typeof window        : object
typeof document      : object
'location' in window : false
window.location      : undefined
reading .href THROWS : TypeError: Cannot read properties of undefined (reading 'href')

That is the worst combination for browser-detecting dependencies. They check typeof window !== 'undefined' && typeof document !== 'undefined', conclude they are in a browser, and then read window.location while the module is still initializing. Axios 1.18.1 does exactly this, and wxt prepare dies before it can generate types, with the error above.

The fix

Define an inert location on window, on the LinkeDOM global (so self.location resolves too), and in the returned globals so applyGlobals puts it on globalThis.

It is a read-only stand-in pointing at http://localhost/, carrying the fields detection code actually reads: href, protocol, host, hostname, origin, port, pathname, search, hash, plus toString. There is no page to navigate at build time, so assign, reload and replace are no-ops rather than throwing.

A plain URL was the obvious choice and does not typecheck: window.location is typed string & Location, and URL is missing ancestorOrigins, assign, reload and replace. Hence the small Location-shaped helper and one narrow cast at the window.location assignment, which nothing can satisfy honestly.

Testing

New browser-environment.test.ts, 4 tests. All 4 pass with the change and all 4 fail without it, the key one failing with the issue's exact error:

TypeError: Cannot read properties of undefined (reading 'href')
     41|         return window.location.href;

One of them reproduces the real shape of the bug rather than just asserting the field exists: it runs inside env.run, asserts hasBrowserEnv is true, and then reads window.location.href.

Full packages/wxt suite on Windows: 52 files passed, 550 tests passed, 4 skipped, 2 todo, 0 failed.

tsc --noEmit is clean and prettier --check passes on both files.

Worth flagging honestly: an earlier full-suite run showed e2e/tests/wxt.test.ts failing with EADDRINUSE ... ::1:3000. That test deliberately occupies port 3000, it passed twice in isolation with this change applied, it passed on a baseline run, and the final full run above is green. It was leaked processes from my repeated runs, not this diff.

Not verified

I did not run the gist reproduction end to end with axios installed; the tests above model the same failure directly against the environment. I also have not checked the extension environment path beyond it reusing getBrowserEnvironmentGlobals.

Happy to change the placeholder URL if you would rather it were something else, or make it configurable.

AI usage

Claude Code (Claude Opus 5) helped investigate and write this; I reviewed the diff and ran everything above myself.

The inline environment used to evaluate entrypoints gives LinkeDOM's
window and document but no location. A dependency that detects a browser
from those two then reads window.location while its module initializes
and throws, so wxt prepare fails before it can generate types. Axios
1.18.1 is a minimal case: it reads window.location.href and dies with
"Cannot read properties of undefined (reading 'href')".

Define an inert location on window, on the LinkeDOM global and on
globalThis. It is a read-only stand-in pointing at http://localhost/;
there is no page to navigate at build time, so assign, reload and
replace do nothing rather than throw.
@netlify

netlify Bot commented Sep 6, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 9c8ef69
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/6ac1e91615b2cb0008610f01
😎 Deploy Preview https://deploy-preview-2611--creative-fairy-df92c4.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions github-actions Bot added the pkg/wxt Includes changes to the `packages/wxt` directory label Sep 6, 2026
@pkg-pr-new

pkg-pr-new Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@wxt-dev/analytics

npm i https://pkg.pr.new/@wxt-dev/analytics@2611

@wxt-dev/auto-icons

npm i https://pkg.pr.new/@wxt-dev/auto-icons@2611

@wxt-dev/browser

npm i https://pkg.pr.new/@wxt-dev/browser@2611

@wxt-dev/i18n

npm i https://pkg.pr.new/@wxt-dev/i18n@2611

@wxt-dev/is-background

npm i https://pkg.pr.new/@wxt-dev/is-background@2611

@wxt-dev/module-react

npm i https://pkg.pr.new/@wxt-dev/module-react@2611

@wxt-dev/module-solid

npm i https://pkg.pr.new/@wxt-dev/module-solid@2611

@wxt-dev/module-svelte

npm i https://pkg.pr.new/@wxt-dev/module-svelte@2611

@wxt-dev/module-vue

npm i https://pkg.pr.new/@wxt-dev/module-vue@2611

@wxt-dev/runner

npm i https://pkg.pr.new/@wxt-dev/runner@2611

@wxt-dev/storage

npm i https://pkg.pr.new/@wxt-dev/storage@2611

@wxt-dev/unocss

npm i https://pkg.pr.new/@wxt-dev/unocss@2611

@wxt-dev/webextension-polyfill

npm i https://pkg.pr.new/@wxt-dev/webextension-polyfill@2611

wxt

npm i https://pkg.pr.new/wxt@2611

commit: 9c8ef69

@codecov

codecov Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 58.33333% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.60%. Comparing base (b11b093) to head (9c8ef69).

Files with missing lines Patch % Lines
...src/core/utils/environments/browser-environment.ts 58.33% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2611      +/-   ##
==========================================
+ Coverage   79.57%   79.60%   +0.03%     
==========================================
  Files         138      138              
  Lines        4102     4114      +12     
  Branches      943      943              
==========================================
+ Hits         3264     3275      +11     
- Misses        739      741       +2     
+ Partials       99       98       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread packages/wxt/src/core/utils/environments/browser-environment.ts Outdated
Comment thread packages/wxt/src/core/utils/environments/browser-environment.ts Outdated
Comment thread packages/wxt/src/core/utils/environments/__tests__/browser-environment.test.ts Outdated
Co-authored-by: Aaron <aaronklinker1@gmail.com>
@coderabbitai

coderabbitai Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7c1bd1cd-21ba-4e4b-9955-9094a87fb387

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@aklinker1 aklinker1 changed the title fix: define location in the entrypoint environment fix(entrypoint-loader): define location in the entrypoint environment Oct 4, 2026
@aklinker1
aklinker1 enabled auto-merge (squash) October 4, 2026 05:50
@aklinker1
aklinker1 merged commit 7e2f64d into wxt-dev:main Oct 4, 2026
19 checks passed
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Thanks for helping make WXT better!

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

Labels

pkg/wxt Includes changes to the `packages/wxt` directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wxt prepare crashes when an imported dependency reads window.location

2 participants