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

Uri.base is broken on Node.js #27979

Open
nex3 opened this issue Dec 2, 2016 · 8 comments
Open

Uri.base is broken on Node.js #27979

nex3 opened this issue Dec 2, 2016 · 8 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. customer-dart-sass dev-compiler-node type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js web-dev-compiler

Comments

@nex3
Copy link
Member

nex3 commented Dec 2, 2016

Currently Uri.base doesn't work in many situations when compiled Dart code is run on Node.js. It uses global.location, which is undefined in Node.js. The preamble tries to work around this by shoving a path into a file: URL, but this is invalid in general. It breaks in all cases on Windows, and will break on POSIX systems if the path contains characters that need to be percent-escaped.

This is an issue for both DDC and dart2js.

@nex3 nex3 added web-dart2js web-dev-compiler type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Dec 2, 2016
@nex3
Copy link
Member Author

nex3 commented Dec 2, 2016

One possible way to do this would be to have Uri.base()'s JS implementation try to call process.cwd() on Node and use new Uri.file() to properly convert that to a URL.

@nex3
Copy link
Member Author

nex3 commented Jul 3, 2019

This is causing real-world breakages for Angular users in the wild (see sass/dart-sass#734). Can it be prioritized?

@vsmenon vsmenon added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Jul 20, 2019
@kevintruby
Copy link

This is also appears to be the cause of an odd SSR issue when working with Vue CLI 3 single-file components. In my use case, also trying to compile SCSS directly from components via webpack and the sass npm module, which is a distribution of Dart Sass.

Returns the following:

> vue-cli-service build


⠧  Building for production...

 ERROR  Failed to compile with 1 errors                                                                                                                                               1:47:15 PM

 error  in ./src/components/Breadcrumb.vue?vue&type=style&index=0&id=352a2dd2&lang=scss&scoped=true&

Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js):
ModuleBuildError: Module build failed (from ./node_modules/sass-loader/lib/loader.js):
Unsupported operation: 'Uri.base' is not supported

It's been really difficult finding help on this issue, and would love to get more info 🙂

@molszanski
Copy link

I've also faced issues with SSR.

Managed to duct-tape it with with this code in our setup-for-ssr.js

 global.location=  {
   href: "file://"+process.cwd()+"/"
 };

@nex3
Copy link
Member Author

nex3 commented Feb 26, 2020

@molszanski Be aware that file paths are not substitutable for file: URLs in general. There are many edge cases that work differently between them, particularly on Windows, so that code will break in some situations.

@molszanski
Copy link

@nex3 Thank you Natalie for the heads up! I've read your comments and warnings. Appreciate them a lot! Luckily I can get away with this hack for now. But this will come back and bite me one day.

@nex3
Copy link
Member Author

nex3 commented Oct 6, 2020

This breaks even more badly when the Node.js environment is using jsdom, which is common in test environments. jsdom configures window.location.href to simulate a browser environment, which overrides the preamble's (already-hacky) replacement and makes Uri.base look like it's operating in a browser when in fact it's not.

Once again, this is affecting real-world users. Can we please prioritize this issue?

@birdofpreyru
Copy link

I've stumbled across this bug yesterday, it is fun to learn that the problem is known for 6 years, and it is not patched yet 😀 Many thanks for suggesting to set global.location.href to URL based on process.cwd(), that saved my day! 🎉

As it is mentioned above that the following is fragile

global.location = {
  href: `file://${process.cwd()}/`,
};

I decided to mention here that this variation might be more bulletproof, at my first glance:

const { sep } = require('path');
const { pathToFileURL } = require('url');

global.location = {
  href: `${pathToFileURL(process.cwd()).href}${sep}`,
};

birdofpreyru added a commit to birdofpreyru/react-utils that referenced this issue Feb 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. customer-dart-sass dev-compiler-node type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js web-dev-compiler
Projects
None yet
Development

No branches or pull requests

5 participants