- Properly return null if an unknown package is resolved.

- Allow short form package:foo imports.

R=asiva@google.com

Review URL: https://codereview.chromium.org/1822863002 .
This commit is contained in:
Ivan Posva 2016-03-22 09:02:04 -07:00
parent a48732a03d
commit 3b2a695d71
7 changed files with 113 additions and 2 deletions

View file

@ -295,7 +295,18 @@ Uri _resolvePackageUri(Uri uri) {
if (mapping == null) {
throw "No mapping for '$packageName' package when resolving '$uri'.";
}
var path = uri.path.substring(packageName.length + 1);
var path;
if (uri.path.length > packageName.length) {
path = uri.path.substring(packageName.length + 1);
} else {
// Handle naked package resolution to the default package name:
// package:foo is equivalent to package:foo/foo.dart
assert(uri.path.length == packageName.length);
path = "$packageName.dart";
}
if (_traceLoading) {
_log("Path to be resolved in package: $path");
}
resolvedUri = mapping.resolve(path);
}
if (_traceLoading) {
@ -754,7 +765,17 @@ Future<Uri> _resolvePackageUriFuture(Uri packageUri) async {
}
assert(_packagesReady);
var result = _resolvePackageUri(packageUri);
var result;
try {
result = _resolvePackageUri(packageUri);
} catch (e, s) {
// Any error during resolution will resolve this package as not mapped,
// which is indicated by a null return.
if (_traceLoading) {
_log("Exception ($e) when resolving package URI: $packageUri");
}
result = null;
}
if (_traceLoading) {
_log("Resolved '$packageUri' to '$result'");
}

View file

@ -0,0 +1 @@
# Intentionally left blank to ensure no packages are resolved.

View file

@ -0,0 +1,47 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// PackageRoot=none
import 'dart:io';
import 'dart:isolate';
main([args, port]) async {
if (port != null) {
testBadResolvePackage(port);
return;
}
var p = new RawReceivePort();
Isolate.spawnUri(Platform.script, [], p.sendPort);
p.handler = (msg) {
p.close();
if (msg is! List) {
print(msg.runtimeType);
throw "Failure return from spawned isolate:\n\n$msg";
}
// Expecting a null resolution for inexistent package mapping.
if (msg[0] != null) {
throw "Bad package config in child isolate: ${msg[0]}\n"
"Expected: 'Foo'";
}
print("SUCCESS");
};
}
testBadResolvePackage(port) async {
try {
var packageRootStr = Platform.packageRoot;
var packageConfigStr = Platform.packageConfig;
var packageConfig = await Isolate.packageConfig;
var badPackageUri = Uri.parse("package:asdf/qwerty.dart");
var resolvedPkg = await Isolate.resolvePackageUri(badPackageUri);
print("Spawned isolate's package root flag: $packageRootStr");
print("Spawned isolate's package config flag: $packageConfigStr");
print("Spawned isolate's loaded package config: $packageConfig");
print("Spawned isolate's resolved package path: $resolvedPkg");
port.send([resolvedPkg]);
} catch (e, s) {
port.send("$e\n$s\n");
}
}

View file

@ -0,0 +1 @@
flu:flu_package/

View file

@ -0,0 +1,3 @@
class Flu {
static var value = "Flu";
}

View file

@ -0,0 +1 @@
Bar

View file

@ -0,0 +1,37 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// PackageRoot=none
import 'dart:io';
import 'dart:isolate';
import "package:flu";
var PACKAGE_FLU = "package:flu";
var FLU_TEXT = "flu.text";
testShortResolution(package_uri) async {
var fluPackage = await Isolate.resolvePackageUri(Uri.parse(package_uri));
print("Resolved $package_uri to $fluPackage");
var fluText = fluPackage.resolve(FLU_TEXT);
print("Resolved $FLU_TEXT from $package_uri to $fluText");
var fluFile = new File.fromUri(fluText);
var fluString = await fluFile.readAsString();
if (fluString != "Bar") {
throw "Contents of $FLU_TEXT not matching.\n"
"Got: $fluString\n"
"Expected: Bar";
}
}
main([args, port]) async {
if (Flu.value != "Flu") {
throw "Import of wrong Flu package.";
}
await testShortResolution(PACKAGE_FLU);
await testShortResolution(PACKAGE_FLU + "/");
await testShortResolution(PACKAGE_FLU + "/abc.def");
print("SUCCESS");
}