Fix error importing *.grammar.terms from vitest #21

Closed
epfremmer wants to merge 1 commit from fix-vitest-terms-import-errors into main
epfremmer commented 2024-08-26 17:55:28 +02:00 (Migrated from github.com)

The vitest dev server calls resolveId a second time with the initially resolved \0${id}.terms module id.

When this happens resolveId attempts to resolve the module id a second time, but produces an invalid path which looks like \0${basePath}\0${id}.terms. This results in an error when the second invalid path gets later passed to load(id) {}.

Checking for a leading null byte (\0) which is used to identify virtual modules prevents this second module id resolution attempt.

I created a small reproduction repo which demonstrates the issue: https://github.com/epfremmer/lezer-vitest-repro-1

Hopefully this is helpful 🎉

The `vitest` dev server calls `resolveId` a second time with the initially resolved `\0${id}.terms` module id. When this happens `resolveId` attempts to resolve the module id a second time, but produces an invalid path which looks like `\0${basePath}\0${id}.terms`. This results in an error when the second invalid path gets later passed to `load(id) {}`. Checking for a leading null byte (`\0`) which is used to identify [virtual modules](https://vitejs.dev/guide/api-plugin#virtual-modules-convention) prevents this second module id resolution attempt. I created a small reproduction repo which demonstrates the issue: https://github.com/epfremmer/lezer-vitest-repro-1 Hopefully this is helpful 🎉
marijnh commented 2024-08-26 18:05:53 +02:00 (Migrated from github.com)

Could this be a bug in the test server? Neither Rollup nor Vite will resolve these twice.

Could this be a bug in the test server? Neither Rollup nor Vite will resolve these twice.
epfremmer commented 2024-08-26 18:38:07 +02:00 (Migrated from github.com)

TBH I'm not sure... I was having a hard time debugging where in the test server was causing this to happen. It does seem pretty strange that it happens which makes me think it's not intended.

I've opened an issue on vitest and see if anyone can shed some light on this behavior?

TBH I'm not sure... I was having a hard time debugging where in the test server was causing this to happen. It does seem pretty strange that it happens which makes me think it's not intended. I've opened an issue on [vitest](https://github.com/vitest-dev/vitest) and see if anyone can shed some light on this behavior?
marijnh commented 2024-08-27 07:45:14 +02:00 (Migrated from github.com)

In the meantime, I've pushed a patch that does something similar to this, using the already present regexp match instead of an extra line.

In the meantime, I've pushed a patch that does something similar to this, using the already present regexp match instead of an extra line.
epfremmer commented 2024-08-28 02:53:59 +02:00 (Migrated from github.com)

Awesome, thank you! Your patch looks much nicer than what I had 👍

Awesome, thank you! [Your patch](https://github.com/lezer-parser/generator/commit/c22856613071070dd6eb1953a8bf1f21c309840b) looks much nicer than what I had 👍

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
lezer/generator!21
No description provided.