Skip to content

Commit

Permalink
- simplify convertFileOrArgumentToUri to just use resolveInputUri
Browse files Browse the repository at this point in the history
- Allow non-file uris as a multi-root root

resolveInputUri already does what we need, and also supports other types of uris such as http.

This enables us to set a multi-root uri which is actually pointing to a dev server, thus hiding the server uri from generated kernel files.

Bug:dart-lang/webdev#865
Change-Id: I0994df0594d57f5d2f020ecfe3bfc0657771cb74
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/133083
Commit-Queue: Alexander Aprelev <aam@google.com>
Auto-Submit: Jake Macdonald <jakemac@google.com>
Reviewed-by: Alexander Aprelev <aam@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
  • Loading branch information
jakemac53 authored and commit-bot@chromium.org committed Jan 24, 2020
1 parent e2a899e commit d3070f4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 76 deletions.
11 changes: 5 additions & 6 deletions pkg/frontend_server/lib/frontend_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ class FrontendCompiler implements CompilerInterface {
_fileSystem = createFrontEndFileSystem(
options['filesystem-scheme'], options['filesystem-root'],
allowHttp: options['enable-http-uris']);
_mainSource = _getFileOrUri(entryPoint);
_mainSource = resolveInputUri(entryPoint);
_kernelBinaryFilenameFull = _options['output-dill'] ?? '$entryPoint.dill';
_kernelBinaryFilenameIncremental = _options['output-incremental-dill'] ??
(_options['output-dill'] != null
Expand All @@ -317,10 +317,12 @@ class FrontendCompiler implements CompilerInterface {
final Uri sdkRoot = _ensureFolderPath(options['sdk-root']);
final String platformKernelDill =
options['platform'] ?? 'platform_strong.dill';
final String packagesOption = _options['packages'];
final CompilerOptions compilerOptions = CompilerOptions()
..sdkRoot = sdkRoot
..fileSystem = _fileSystem
..packagesFileUri = _getFileOrUri(_options['packages'])
..packagesFileUri =
packagesOption != null ? resolveInputUri(packagesOption) : null
..sdkSummary = sdkRoot.resolve(platformKernelDill)
..verbose = options['verbose']
..embedSourceText = options['embed-source-text']
Expand Down Expand Up @@ -777,7 +779,7 @@ class FrontendCompiler implements CompilerInterface {
_outputStream.writeln('result $boundaryKey');
await invalidateIfInitializingFromDill();
if (entryPoint != null) {
_mainSource = _getFileOrUri(entryPoint);
_mainSource = resolveInputUri(entryPoint);
}
errors.clear();

Expand Down Expand Up @@ -959,9 +961,6 @@ class FrontendCompiler implements CompilerInterface {
_kernelBinaryFilename = _kernelBinaryFilenameFull;
}

Uri _getFileOrUri(String fileOrUri) =>
convertFileOrUriArgumentToUri(_fileSystem, fileOrUri);

IncrementalCompiler _createGenerator(Uri initializeFromDillUri) {
return IncrementalCompiler(_compilerOptions, _mainSource,
initializeFromDillUri: initializeFromDillUri,
Expand Down
103 changes: 67 additions & 36 deletions pkg/frontend_server/test/frontend_server_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1235,44 +1235,75 @@ true
expect(await starter(args), 0);
});

test('compile with http uris', () async {
group('http uris', () {
var host = 'localhost';
var server = await HttpServer.bind(host, 0);
addTearDown(server.close);
var port = server.port;
server.listen((request) {
var path = request.uri.path;
var file = File('${tempDir.path}$path');
var response = request.response;
if (!file.existsSync()) {
response.statusCode = 404;
} else {
response.statusCode = 200;
response.add(file.readAsBytesSync());
}
response.close();
File dillFile;
int port;
HttpServer server;

setUp(() async {
dillFile = File('${tempDir.path}/app.dill');
server = await HttpServer.bind(host, 0);
port = server.port;
server.listen((request) {
var path = request.uri.path;
var file = File('${tempDir.path}$path');
var response = request.response;
if (!file.existsSync()) {
response.statusCode = 404;
} else {
response.statusCode = 200;
response.add(file.readAsBytesSync());
}
response.close();
});
var main = File('${tempDir.path}/foo.dart')..createSync();
main.writeAsStringSync(
"import 'package:foo/foo.dart'; main() {print(foo);}\n");
File('${tempDir.path}/.packages')
..createSync()
..writeAsStringSync("\nfoo:http://$host:$port/packages/foo");
File('${tempDir.path}/packages/foo/foo.dart')
..createSync(recursive: true)
..writeAsStringSync("var foo = 'hello';");
});

tearDown(() async {
await server.close();
print('closed');
});

test('compile with http uris', () async {
expect(dillFile.existsSync(), equals(false));
final List<String> args = <String>[
'--sdk-root=${sdkRoot.toFilePath()}',
'--incremental',
'--platform=${platformKernel.path}',
'--output-dill=${dillFile.path}',
'--enable-http-uris',
'--packages=http://$host:$port/.packages',
'http://$host:$port/foo.dart',
];
expect(await starter(args), 0);
expect(dillFile.existsSync(), equals(true));
});

test('compile with an http file system root', () async {
expect(dillFile.existsSync(), equals(false));
final List<String> args = <String>[
'--sdk-root=${sdkRoot.toFilePath()}',
'--incremental',
'--platform=${platformKernel.path}',
'--output-dill=${dillFile.path}',
'--enable-http-uris',
'--packages=test-app:///.packages',
'--filesystem-root=http://$host:$port/',
'--filesystem-scheme=test-app',
'test-app:///foo.dart',
];
expect(await starter(args), 0);
expect(dillFile.existsSync(), equals(true));
});
var main = File('${tempDir.path}/foo.dart')..createSync();
main.writeAsStringSync(
"import 'package:foo/foo.dart'; main() {print(foo);}\n");
File('${tempDir.path}/.packages')
..createSync()
..writeAsStringSync("\nfoo:http://$host:$port/packages/foo");
File('${tempDir.path}/packages/foo/foo.dart')
..createSync(recursive: true)
..writeAsStringSync("var foo = 'hello';");
var dillFile = File('${tempDir.path}/app.dill');
expect(dillFile.existsSync(), equals(false));
final List<String> args = <String>[
'--sdk-root=${sdkRoot.toFilePath()}',
'--incremental',
'--platform=${platformKernel.path}',
'--output-dill=${dillFile.path}',
'--enable-http-uris',
'--packages=http://$host:$port/.packages',
'http://$host:$port/foo.dart',
];
expect(await starter(args), 0);
});

test('compile to JavaScript', () async {
Expand Down
39 changes: 5 additions & 34 deletions pkg/vm/lib/kernel_front_end.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import 'package:front_end/src/api_unstable/vm.dart'
kernelForProgram,
parseExperimentalArguments,
parseExperimentalFlags,
printDiagnosticMessage;
printDiagnosticMessage,
resolveInputUri;

import 'package:kernel/class_hierarchy.dart' show ClassHierarchy;
import 'package:kernel/ast.dart' show Component, Library, Reference;
Expand Down Expand Up @@ -197,17 +198,15 @@ Future<int> runCompiler(ArgResults options, String usage) async {
final fileSystem =
createFrontEndFileSystem(fileSystemScheme, fileSystemRoots);

final Uri packagesUri = packages != null
? convertFileOrUriArgumentToUri(fileSystem, packages)
: null;
final Uri packagesUri = packages != null ? resolveInputUri(packages) : null;

final platformKernelUri = Uri.base.resolveUri(new Uri.file(platformKernel));
final List<Uri> linkedDependencies = <Uri>[];
if (aot || linkPlatform) {
linkedDependencies.add(platformKernelUri);
}

Uri mainUri = convertFileOrUriArgumentToUri(fileSystem, input);
Uri mainUri = resolveInputUri(input);
if (packagesUri != null) {
mainUri = await convertToPackageUri(fileSystem, mainUri, packagesUri);
}
Expand Down Expand Up @@ -572,42 +571,14 @@ FileSystem createFrontEndFileSystem(
multiRootFileSystemScheme != null) {
final rootUris = <Uri>[];
for (String root in multiRootFileSystemRoots) {
rootUris.add(Uri.base.resolveUri(new Uri.file(root)));
rootUris.add(resolveInputUri(root));
}
fileSystem = new MultiRootFileSystem(
multiRootFileSystemScheme, rootUris, fileSystem);
}
return fileSystem;
}

/// Convert command line argument [input] which is a file or URI to an
/// absolute URI.
///
/// If virtual multi-root file system is used, or [input] can be parsed to a
/// URI with 'package', 'file', or 'http' scheme, then [input] is interpreted
/// as URI. Otherwise [input] is interpreted as a file path.
Uri convertFileOrUriArgumentToUri(FileSystem fileSystem, String input) {
if (input == null) {
return null;
}
// If using virtual multi-root file system, input source argument should be
// specified as URI.
if (fileSystem is MultiRootFileSystem) {
return Uri.base.resolve(input);
}
try {
Uri uri = Uri.parse(input);
if (uri.scheme == 'package' ||
uri.scheme == 'file' ||
uri.scheme == 'http') {
return uri;
}
} on FormatException {
// Ignore, treat input argument as file path.
}
return Uri.base.resolveUri(new Uri.file(input));
}

/// Convert a URI which may use virtual file system schema to a real file URI.
Future<Uri> asFileUri(FileSystem fileSystem, Uri uri) async {
FileSystemEntity fse = fileSystem.entityForUri(uri);
Expand Down

0 comments on commit d3070f4

Please sign in to comment.