Skip to content

Commit

Permalink
Fix importing files relative to "package:" imports (#638)
Browse files Browse the repository at this point in the history
The PackageImporter wasn't accepting paths relative to its
canonicalized outputs as inputs.

Closes #631
  • Loading branch information
nex3 authored Apr 3, 2019
1 parent 9999835 commit 8c9412b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## 1.17.5

* Fix importing files relative to `package:`-imported files.

### Dart API

* Explicitly require that importers' `canonicalize()` methods be able to take
paths relative to their outputs as valid inputs. This isn't considered a
breaking change because the importer infrastructure already required this in
practice.

## 1.17.4

* Consistently parse U+000C FORM FEED, U+000D CARRIAGE RETURN, and sequences of
Expand Down
6 changes: 4 additions & 2 deletions lib/src/importer/async.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ abstract class AsyncImporter {
///
/// If no stylesheets are found, the importer should return `null`.
///
/// Sass assumes that calling [canonicalize] multiple times with the same URL
/// will return the same result.
/// Calling [canonicalize] multiple times with the same URL must return the
/// same result. Calling [canonicalize] with a URL returned by [canonicalize]
/// must return that URL. Calling [canonicalize] with a URL relative to one
/// returned by [canonicalize] must return a meaningful result.
FutureOr<Uri> canonicalize(Uri url);

/// Loads the Sass text for the given [url], or returns `null` if
Expand Down
1 change: 1 addition & 0 deletions lib/src/importer/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class PackageImporter extends Importer {
PackageImporter(this._packageResolver);

Uri canonicalize(Uri url) {
if (url.scheme == 'file') return _filesystemImporter.canonicalize(url);
if (url.scheme != 'package') return null;

var resolved = _packageResolver.resolveUri(url);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sass
version: 1.17.4
version: 1.17.5-dev
description: A Sass implementation in Dart.
author: Dart Team <[email protected]>
homepage: https://github.com/sass/dart-sass
Expand Down
16 changes: 16 additions & 0 deletions test/dart_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ main() {
expect(css, equals("a {\n b: 3;\n}"));
});

test("can resolve relative paths in a package", () async {
await d.dir("subdir", [
d.file("test.scss", "@import 'other'"),
d.file("_other.scss", "a {b: 1 + 2}"),
]).create();

await d
.file("test.scss", '@import "package:fake_package/test";')
.create();
var resolver = SyncPackageResolver.config(
{"fake_package": p.toUri(d.path('subdir'))});

var css = compile(d.path("test.scss"), packageResolver: resolver);
expect(css, equals("a {\n b: 3;\n}"));
});

test("doesn't import a package URL from a missing package", () async {
await d
.file("test.scss", '@import "package:fake_package/test_aux";')
Expand Down

0 comments on commit 8c9412b

Please sign in to comment.