rixo / svench

A lightweight workbench to develop your Svelte components in isolation
svench-docs.vercel.app
207 stars 6 forks source link

Absolute path matching problem for `npx svench` command on Windows #30

Closed jacob-8 closed 3 years ago

jacob-8 commented 3 years ago

When running npx svench on Windows using version 0.2.0-14, I immediately receive the error: Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

Windows Subsystem for Linux is not an option either as it throws: error: The working directory "/mnt/c/dev/kit-svench" is not an absolute path

https://github.com/nodejs/node/issues/31710 provides some context and https://github.com/sveltejs/vite-plugin-svelte/issues/29 makes me think the solution will be found in wrapping the culprit url in url.pathToFileURL().

Repro: https://github.com/jwrunner/svelte-kit-svench

jacob-8 commented 3 years ago

I borrowed from https://github.com/sveltejs/vite-plugin-svelte/commit/7a7f9089d33a93b21366fd26b79cac9d501ebae5 and tried to create a temporary patch on my machine by editing svench/cli/commands/vite/dev.js. I succeeded it getting it one step further by getting loadVite() to run but then ran into a problem with loadSvenchifiedConfig(). See my attempt at https://gist.github.com/jwrunner/adeb4b0e4f9c20d881f080e1f77b3e58

A permanent patch may belong in normalizeGlobalOptions() or neighbors in svench/cli/cli.js?

rixo commented 3 years ago

Thanks for reporting.

Can you please try with version v0.2.0-20 (tagged next on npm, so something like npm i -D svench@next) and confirm you still have this error?

If you still have the problem, can you post the complete errors that you get, including stack trace (you can edit out the complete paths on your machine if you want, but the actual call stack & line numbers in Svench files is important).

jacob-8 commented 3 years ago

Still have the error, but found two more spots where it's thrown now. There is never a stack trace beyond the 1 line Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only file and data URLs are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:' and its always a problem with a dynamic import of a file that has an absolute path beginning with C:\\dev\\... on my machine.

image

In debugging, line 67 of lib/bundle.js is throwing the error as we never get to setting the start variable:

image

That error and the import of the configFile are solved with using pathToFileURL(*).href and I get the message [svench] Compiled Svench runtime in 20.71s image

Then it proceeds on and runs into the same errors in cli/commands/vite/dev.js detailed in my previous message containing the gist. I can solve the loading of Vite with pathToFileUrl, but still cannot fix loadSvenchifiedConfig() with this: image Changing the info.svench.dir path does however throw a different error:

C:\dev\kit-svench-2\node_modules\svench\lib\import-relative.cjs:1
Error: Cannot find module 'file:\\C:\\dev\\kit-svench-2\\node_modules\\svench\\presets\\vite'
Require stack:
- C:\dev\kit-svench-2\node_modules\svench\lib\import-relative.cjs
rixo commented 3 years ago

OK thanks for all the debugging. I'll try your repro in a Windows box to fix all the problematic paths.

Note, however, that we don't really have support for Kit projects yet unfortunately...

rixo commented 3 years ago

Hi @jwrunner! Sorry for taking so long to come back at this, things kinda crazy at works these days...

Anyway thanks again for your investigative work, it really helped. I was able to reproduce your issue in a Windows box, then I changed all dynamic imports that target an absolute path on the fs to a custom version that makes sure we're doing so with URLs. Now it seems to run well both on Windows and Linux, so we have a candidate fix.

Can you try svench@0.2.0-23 (currently tagged svench@next-win on npm) and see if it fixes the problem for you too?

jacob-8 commented 3 years ago

Wonderful! Running npx svench works on Windows with svench@0.2.0-23. Thank you for helping get this fixed as I've been chomping at the bit to try it out. Storybook is slow and large and your tool here is just perfect!