-
Notifications
You must be signed in to change notification settings - Fork 41
fix(nitro): normalize module paths for windows virtual modules #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| 'evlog': patch | ||
| --- | ||
|
|
||
| Fix `evlog/nitro` and `evlog/nitro/v3` on Windows. The module passed native `path.resolve()` paths to `nitro.options.plugins` and `nitro.options.errorHandler`. Nitro raw-interpolates those into the `#nitro/virtual/plugins` and `#nitro/virtual/error-handler` JS string literals, so Windows backslashes were parsed as escape sequences (`\n`, `\v`, …) and broke module resolution — surfacing as `Cannot find module … imported from '#nitro/virtual/error-handler'`. Paths are now normalized to forward slashes before being handed to Nitro. | ||
|
|
||
| Closes [#345](https://github.com/HugoRCD/evlog/issues/345). |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import nitroV2Module from '../../src/nitro/module' | ||
| import nitroV3Module from '../../src/nitro-v3/module' | ||
|
|
||
| // Nitro builds its #nitro/virtual/plugins and #nitro/virtual/error-handler | ||
| // virtual modules by raw-interpolating these paths into JS string literals: | ||
| // import _abc from "${plugin}" | ||
| // import errorHandler$0 from "${h}" | ||
| // On Windows, path.resolve() returns paths with backslashes (e.g. C:\…\plugin). | ||
| // Those backslashes would then be parsed as JS escape sequences (\n, \v, …), | ||
| // silently corrupting the specifier. Regression test for evlog#345. | ||
| describe('nitro modules avoid backslash paths', () => { | ||
| function makeNitroStub() { | ||
| return { | ||
| options: { | ||
| plugins: [] as string[], | ||
| errorHandler: undefined as string | string[] | undefined, | ||
| noExternals: undefined as undefined | true | string[], | ||
| runtimeConfig: {} as Record<string, unknown>, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| it('nitro v2 module pushes POSIX-style plugin and errorHandler paths', () => { | ||
| const nitro = makeNitroStub() | ||
| nitroV2Module({ env: { service: 'test' } }).setup(nitro as never) | ||
|
|
||
| expect(nitro.options.plugins).toHaveLength(1) | ||
| expect(nitro.options.plugins[0]).not.toMatch(/\\/) | ||
| expect(nitro.options.plugins[0]).toMatch(/\/nitro\/plugin$/) | ||
|
|
||
| expect(nitro.options.errorHandler).toBeTypeOf('string') | ||
| expect(nitro.options.errorHandler).not.toMatch(/\\/) | ||
| expect(nitro.options.errorHandler).toMatch(/\/nitro\/errorHandler$/) | ||
| }) | ||
|
|
||
| it('nitro v3 module pushes POSIX-style plugin and errorHandler paths', () => { | ||
| const nitro = makeNitroStub() | ||
| nitroV3Module({ env: { service: 'test' } }).setup(nitro as never) | ||
|
|
||
| expect(nitro.options.plugins).toHaveLength(1) | ||
| expect(nitro.options.plugins[0]).not.toMatch(/\\/) | ||
| expect(nitro.options.plugins[0]).toMatch(/\/nitro-v3\/plugin$/) | ||
|
|
||
| expect(Array.isArray(nitro.options.errorHandler)).toBe(true) | ||
| const handlers = nitro.options.errorHandler as string[] | ||
| expect(handlers).toHaveLength(1) | ||
| expect(handlers[0]).not.toMatch(/\\/) | ||
| expect(handlers[0]).toMatch(/\/nitro-v3\/errorHandler$/) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win
Move
makeNitroStub()intotest/helpers/and import it here.This test re-implements a helper inline, which violates the test helper policy and makes reuse harder across Nitro tests.
As per coding guidelines, "Always import real source helpers in tests, never re-implement them in tests." and "Use the helpers in
test/helpers/(drain spies, fake timers, fetch mock, framework matrix) in tests."🤖 Prompt for AI Agents