From 0dd4d4e80f5e7d96de89e4f9f812d5546b079321 Mon Sep 17 00:00:00 2001 From: Natalie Weizenbaum Date: Tue, 10 Aug 2021 15:59:06 -0700 Subject: [PATCH] Don't try to force custom URL schemes to file paths in the Node API When an error occurred in a stylesheet loaded by a custom importer with a custom URL scheme, the Node error wrapper tried to convert that URL into a path and ended up crashing. Closes #1138 --- CHANGELOG.md | 3 +++ lib/src/node.dart | 12 +++++++++++- test/node_api/importer_test.dart | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6cceb4362..c5207cec4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ * Don't crash when a Windows path is returned by a custom Node importer at the same time as file contents. +* Don't crash when an error occurs in a stylesheet loaded via a custom importer + with a custom URL scheme. + ## 1.37.5 * No user-visible changes. diff --git a/lib/src/node.dart b/lib/src/node.dart index a97d5f93c..709b2a3fe 100644 --- a/lib/src/node.dart +++ b/lib/src/node.dart @@ -183,10 +183,20 @@ RenderResult _renderSync(RenderOptions options) { /// Converts an exception to a [JsError]. JsError _wrapException(Object exception) { if (exception is SassException) { + String file; + var url = exception.span.sourceUrl; + if (url == null) { + file = 'stdin'; + } else if (url.scheme == 'file') { + file = p.fromUri(url); + } else { + file = url.toString(); + } + return _newRenderError(exception.toString().replaceFirst("Error: ", ""), line: exception.span.start.line + 1, column: exception.span.start.column + 1, - file: exception.span.sourceUrl.andThen(p.fromUri) ?? 'stdin', + file: file, status: 1); } else { return JsError(exception.toString()); diff --git a/test/node_api/importer_test.dart b/test/node_api/importer_test.dart index 50f5787fd..f36142e7e 100644 --- a/test/node_api/importer_test.dart +++ b/test/node_api/importer_test.dart @@ -704,6 +704,24 @@ void main() { " ╵\n" " stdin 1:9 root stylesheet")); }); + + test("it occurs in a file with a custom URL scheme", () { + var error = + renderSyncError(RenderOptions( + data: "@import 'foo:bar'", + importer: allowInterop(expectAsync2((String _, void __) { + return NodeImporterResult(contents: '@error "oh no";'); + })))); + + expect(error, + toStringAndMessageEqual("\"oh no\"\n" + " ╷\n" + "1 │ @error \"oh no\";\n" + " │ ^^^^^^^^^^^^^^\n" + " ╵\n" + " foo:bar 1:1 @import\n" + " stdin 1:9 root stylesheet")); + }); }); group("render()", () {