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

Avoid resolving imports relative to working directory for compileString #124

Closed
wants to merge 1 commit into from
Closed

Avoid resolving imports relative to working directory for compileString #124

wants to merge 1 commit into from

Conversation

ntkme
Copy link
Contributor

@ntkme ntkme commented Apr 3, 2022

Comment on lines +96 to +102
const importer = new proto.InboundMessage.CompileRequest.Importer();
importer.setPath(p.resolve('.'));
input.setImporter(importer);
Copy link
Contributor Author

@ntkme ntkme Apr 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nex3 With new spec, null URL will end up with noOpImporter on embedded compiler, but legacy API wants a FilesystemImporter even without filename/url, so explicitly set it should give the compiler the correct importer.

lib/src/compile.ts Outdated Show resolved Hide resolved
url: options.file ? pathToFileURL(options.file) : undefined,
url: options.file
? pathToFileURL(options.file)
: new URL(legacyImporterProtocol),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legacy API needs to have a FilesystemImporter('.') as if it has an url even if it does not. Set a special value here to let the new API know that.

@@ -87,10 +90,19 @@ function newCompileStringRequest(
input.setSource(source);
input.setSyntax(utils.protofySyntax(options?.syntax ?? 'scss'));

if (options?.url) input.setUrl(options.url.toString());
const url = options?.url?.toString();
if (url && url !== legacyImporterProtocol) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The special value legacyImporterProtocol means this is from Legacy API that even if it does not set a url it should behave as if it has one down below when setting the importer.

@nex3 nex3 self-requested a review April 6, 2022 20:29
@nex3
Copy link
Contributor

nex3 commented Apr 6, 2022

Unless I'm mistaken, updating sass/dart-sass-embedded#83 so that the compiler uses a filesystem importer in the right circumstances should mitigate the need for this PR. However, it would be good to also add a JS API spec to sass-spec that verifies that CWD-relative imports don't work with compileString.

@ntkme ntkme closed this Apr 6, 2022
@ntkme ntkme deleted the fix-importer branch April 6, 2022 23:37
@ntkme ntkme restored the fix-importer branch April 9, 2022 00:17
@ntkme ntkme reopened this Apr 9, 2022
@ntkme
Copy link
Contributor Author

ntkme commented Apr 9, 2022

Looks like this can be simplified a bit by constructing proto importer inside the legacy compile.

@ntkme
Copy link
Contributor Author

ntkme commented Apr 9, 2022

Let me close this and open an new PR.

@ntkme ntkme closed this Apr 9, 2022
@ntkme ntkme deleted the fix-importer branch April 9, 2022 00:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Imports shall not be resolved relative to working directory of dart-sass-embedded process
2 participants