fix: escape virtual module import paths with apostrophes#2143
fix: escape virtual module import paths with apostrophes#2143Arktomson wants to merge 2 commits intowxt-dev:mainfrom
Conversation
❌ Deploy Preview for creative-fairy-df92c4 failed.
|
aklinker1
left a comment
There was a problem hiding this comment.
Thanks for the PR. A few small changes to the tests, then we can merge!
| '\0virtual:wxt-background-entrypoint?' + inputPath, | ||
| ); | ||
|
|
||
| expect(code).toBe(`import definition from ${JSON.stringify(inputPath)};`); |
There was a problem hiding this comment.
We should hardcode the expected path here so it's clear what's being escaped and so the test doesn't rely on the same API as the runtime code.
Is this correct? Or should there be one more \?
| expect(code).toBe(`import definition from ${JSON.stringify(inputPath)};`); | |
| expect(code).toBe(`import definition from "/tmp/foo\'bar/background.ts";`); |
When I run it in the browser, I just get "/tmp/foo'bar/background.ts" without any escape characters...
> console.log(JSON.stringify(`/tmp/foo'bar/background.ts`))
"/tmp/foo'bar/background.ts"There was a problem hiding this comment.
Oh wait 🤦 I see, the problem is just that the string was defined with single quotes, so having another one in it is invalid:
'/tmp/foo'bar/background.ts'Can you also add an expected test for when a project path has a double quote " in it?
| `import definition from 'virtual:user-background-entrypoint';`, | ||
| `import definition from "virtual:user-background-entrypoint";`, | ||
| ])( | ||
| 'should escape input paths when template contains %s', |
There was a problem hiding this comment.
| 'should escape input paths when template contains %s', | |
| 'should escape input paths with apostrophes when encountering: %s', |
Mention that we're testing apostrophes specifically in this test.
@wxt-dev/analytics
@wxt-dev/auto-icons
@wxt-dev/browser
@wxt-dev/i18n
@wxt-dev/module-react
@wxt-dev/module-solid
@wxt-dev/module-svelte
@wxt-dev/module-vue
@wxt-dev/runner
@wxt-dev/storage
@wxt-dev/unocss
@wxt-dev/webextension-polyfill
wxt
commit: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2143 +/- ##
==========================================
- Coverage 76.00% 75.98% -0.03%
==========================================
Files 113 113
Lines 3047 3052 +5
Branches 683 684 +1
==========================================
+ Hits 2316 2319 +3
- Misses 645 647 +2
Partials 86 86 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
I did a bit more research here. Should we use |
ok, let me continue editing. |
|
@Arktomson could you also look at #2146? I think providing this cleanup logic could be a good util for WXT to export as well. But it needs to handle more special characters in file paths to be generic enough. |
Overview
When a user's project path contains an apostrophe (e.g. /tmp/foo'bar/), building the extension fails because virtual module templates use a simple string replacement that doesn't account for quote characters in file paths. The path gets inserted directly into an import statement like import definition from '/tmp/foo'bar/background.ts', producing invalid JavaScript.
This fix replaces the naive .replace() with a quote-aware approach: it matches the full quoted import specifier (including surrounding quotes) and replaces it with JSON.stringify(inputPath), which properly escapes any special characters in the path.
A unit test is added to verify both single-quoted and double-quoted template variants handle paths with apostrophes correctly.
Manual Testing
Related Issue
This PR closes #1697