fix: define location in the entrypoint environment - #2611
Open
ethanstoner wants to merge 1 commit into
Open
Conversation
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.
✅ Deploy Preview for creative-fairy-df92c4 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2601.
What was wrong
getBrowserEnvironmentGlobalshands entrypoint evaluation LinkeDOM'swindowanddocument, but nolocation. Confirmed against linkedom 0.18.13: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 readwindow.locationwhile the module is still initializing. Axios 1.18.1 does exactly this, andwxt preparedies before it can generate types, with the error above.The fix
Define an inert
locationonwindow, on the LinkeDOMglobal(soself.locationresolves too), and in the returned globals soapplyGlobalsputs it onglobalThis.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, plustoString. There is no page to navigate at build time, soassign,reloadandreplaceare no-ops rather than throwing.A plain
URLwas the obvious choice and does not typecheck:window.locationis typedstring & Location, andURLis missingancestorOrigins,assign,reloadandreplace. Hence the smallLocation-shaped helper and one narrow cast at thewindow.locationassignment, 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:One of them reproduces the real shape of the bug rather than just asserting the field exists: it runs inside
env.run, assertshasBrowserEnvis true, and then readswindow.location.href.Full
packages/wxtsuite on Windows: 52 files passed, 550 tests passed, 4 skipped, 2 todo, 0 failed.tsc --noEmitis clean andprettier --checkpasses on both files.Worth flagging honestly: an earlier full-suite run showed
e2e/tests/wxt.test.tsfailing withEADDRINUSE ... ::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.