Skip to content
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

ERR_INVALID_FILE_URL_PATH happens when resolving alias including @ with importer #126

Closed
sapphi-red opened this issue Apr 7, 2022 · 4 comments · Fixed by #129
Closed
Assignees
Labels
bug Something isn't working

Comments

@sapphi-red
Copy link

When resolving alias including @ with importer, ERR_INVALID_FILE_URL_PATH happens.
The implementation of importer is below.

const importer = (p, importer) => {
  return { file: path.resolve(__dirname, './src', p.replace(/^\/@\//, '')) }
}

Also I am using legacy render function.

This works well with dart-sass.

Error message

Error: Error: TypeError [ERR_INVALID_FILE_URL_PATH]: File URL path must be absolute
  ╷
1 │ @import '/@/bar.scss';
  │         ^^^^^^^^^^^^^
  ╵
  src\foo.scss 1:9  root stylesheet
    at handleCompileResponse (D:\documents\GitHub\sass-embedded-atmark-invalid-url-reproduction\node_modules\sass-embedded\dist\lib\src\compile.js:213:15)
    at compileRequestAsync (D:\documents\GitHub\sass-embedded-atmark-invalid-url-reproduction\node_modules\sass-embedded\dist\lib\src\compile.js:101:16)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Step to reproduce

  1. Clone https://github.com/sapphi-red/sass-embedded-atmark-invalid-url-reproduction.
  2. Run npm i.
  3. Run node sass.js
  4. dart-sass runs with no error. sass-embedded fails with error.

Additional context

I was trying to use sass-embedded with vite (vitejs/vite#6734).
I used npm i -D sass@npm:sass-embedded@latest to install/replace it.
Then I tried but this error occured. So I created a minimal reproduction without vite.

@nex3 nex3 self-assigned this Apr 11, 2022
@nex3
Copy link
Contributor

nex3 commented Apr 12, 2022

I can't reproduce this:

$ git clone https://github.com/sapphi-red/sass-embedded-atmark-invalid-url-reproduction
Cloning into 'sass-embedded-atmark-invalid-url-reproduction'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 12 (delta 0), reused 12 (delta 0), pack-reused 0
Receiving objects: 100% (12/12), 9.10 KiB | 4.55 MiB/s, done.

$ cd sass-embedded-atmark-invalid-url-reproduction/
$ npm i

added 69 packages, and audited 70 packages in 2s

8 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

$ node sass.js
[dart-sass]
.bar {
  color: blue;
}

.foo {
  color: red;
}

-----------
[sass-embedded]
.bar {
  color: blue;
}

.foo {
  color: red;
}

@nex3 nex3 added bug Something isn't working needs info labels Apr 12, 2022
@sapphi-red
Copy link
Author

Thanks for taking a look.

Look like this only happens on windows.
When I ran this script on windows, it happened. But it did not happen when I ran this script on WSL.

@nex3
Copy link
Contributor

nex3 commented Apr 12, 2022

Okay, I can reproduce this now. Here's what's going on under the covers:

  • The Sass compiler encounters @import "/@/bar.scss".
  • Because the embedded host implements legacy-API importers as a wrapper around the modern API, the compiler attempts to resolve this import relative to the current URL.
  • Since any URL beginning with / is a root-relative URL, the relative resolution here is file:///@/bar.scss.
  • The embedded host then tries to convert this resolved URL to a path, which fails on Windows because @ isn't a valid drive name.

Note that the @ sign isn't actually the problem here; this will also reproduce with any root-relative URL that isn't a valid Windows path, such as /whatever/bar.scss. The solution is probably to catch path-conversion errors and treat them as though the path couldn't be found, effectively passing on the original string to the importer in question.

@sapphi-red
Copy link
Author

Thank you for the fix!
I confirmed that #129 will fix for me.

@nex3 nex3 closed this as completed in #129 Apr 13, 2022
nex3 added a commit to sass/sass-spec that referenced this issue Apr 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants