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

Validate build_web_compilers, Exception handling #16

Merged
merged 6 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,66 @@ analyzer:
override_on_non_overriding_method: error
linter:
rules:
# Errors
- avoid_empty_else
- await_only_futures
- comment_references
- control_flow_in_finally
- empty_statements
- hash_and_equals
- iterable_contains_unrelated_type
- no_duplicate_case_values
- test_types_in_equals
- throw_in_finally
- unawaited_futures
- unnecessary_statements
- unrelated_type_equality_checks
- valid_regexps

# Style
- annotate_overrides
- avoid_empty_else
- avoid_function_literals_in_foreach_calls
- avoid_init_to_null
- avoid_null_checks_in_equality_operators
- avoid_return_types_on_setters
- avoid_returning_null
- avoid_unused_constructor_parameters
- await_only_futures
- camel_case_types
- cancel_subscriptions
#- cascade_invocations
- comment_references
- constant_identifier_names
- control_flow_in_finally
- directives_ordering
- empty_catches
- empty_constructor_bodies
- empty_statements
- hash_and_equals
- implementation_imports
- invariant_booleans
- iterable_contains_unrelated_type
- library_names
- library_prefixes
- list_remove_unrelated_type
- no_adjacent_strings_in_list
- no_duplicate_case_values
- non_constant_identifier_names
- omit_local_variable_types
- only_throw_errors
- overridden_fields
- package_api_docs
- package_names
- package_prefixed_library_names
- prefer_adjacent_string_concatenation
- prefer_collection_literals
- prefer_conditional_assignment
- prefer_const_constructors
- prefer_contains
- prefer_final_fields
#- prefer_final_locals
- prefer_initializing_formals
- prefer_interpolation_to_compose_strings
- prefer_is_empty
- prefer_is_not_empty
- prefer_single_quotes
- prefer_typing_uninitialized_variables
- recursive_getters
- slash_for_doc_comments
- super_goes_last
- test_types_in_equals
- throw_in_finally
- type_init_formals
- unawaited_futures
- unnecessary_brace_in_string_interps
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_null_aware_assignments
- unnecessary_statements
- unnecessary_this
- unrelated_type_equality_checks
- valid_regexps
5 changes: 5 additions & 0 deletions webdev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.1

- Checks for a dependency on `build_web_compilers`.
- Gracefully handle exceptions.

## 0.1.0

- Initial release. Supports basic invocation of `build` and `serve` with
Expand Down
12 changes: 12 additions & 0 deletions webdev/bin/webdev.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';
import 'dart:io';
import 'dart:isolate';

import 'package:args/command_runner.dart';
import 'package:io/ansi.dart';
Expand All @@ -28,5 +29,16 @@ Future main(List<String> args) async {
}

exitCode = ExitCode.config.code;
} on FileSystemException catch (e) {
print(yellow.wrap('Could not run in the current directory.'));
print(e.message);
if (e.path != null) {
print(' ${e.path}');
}
exitCode = ExitCode.config.code;
} on IsolateSpawnException catch (e) {
print(red.wrap('An unexpected exception has occurred.'));
print(e.message);
exitCode = ExitCode.software.code;
}
}
121 changes: 72 additions & 49 deletions webdev/lib/src/command/build_runner_command_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import 'package:stack_trace/stack_trace.dart';

import '../pubspec.dart';

const _packagesFileName = '.packages';

/// Extend to get a command with the arguments common to all build_runner
/// commands.
abstract class BuildRunnerCommandBase extends Command<int> {
Expand All @@ -30,6 +32,8 @@ abstract class BuildRunnerCommandBase extends Command<int> {
Future<int> runCore(String command) async {
await checkPubspecLock();

var buildRunnerScript = await _buildRunnerScript();

final arguments = [command]..addAll(argResults.arguments);

var exitCode = 0;
Expand All @@ -49,68 +53,87 @@ abstract class BuildRunnerCommandBase extends Command<int> {
stderr.writeln(new Trace.parse(trace).terse);
if (exitCode == 0) exitCode = 1;
});
await Isolate.spawnUri(
await _buildRunnerScript(), arguments, messagePort.sendPort,
onExit: exitPort.sendPort,
onError: errorPort.sendPort,
automaticPackageResolution: true);
StreamSubscription exitCodeListener;
exitCodeListener = messagePort.listen((isolateExitCode) {
if (isolateExitCode is! int) {
throw new StateError(
'Bad response from isolate, expected an exit code but got '
'$isolateExitCode');
}
exitCode = isolateExitCode as int;
exitCodeListener.cancel();
exitCodeListener = null;
});
await exitPort.first;
await errorListener.cancel();
await exitCodeListener?.cancel();

return exitCode;
try {
await Isolate.spawnUri(buildRunnerScript, arguments, messagePort.sendPort,
onExit: exitPort.sendPort,
onError: errorPort.sendPort,
automaticPackageResolution: true);
StreamSubscription exitCodeListener;
exitCodeListener = messagePort.listen((isolateExitCode) {
if (isolateExitCode is! int) {
throw new StateError(
'Bad response from isolate, expected an exit code but got '
'$isolateExitCode');
}
exitCode = isolateExitCode as int;
exitCodeListener.cancel();
exitCodeListener = null;
});
await exitPort.first;
await errorListener.cancel();
await exitCodeListener?.cancel();

return exitCode;
} finally {
exitPort.close();
errorPort.close();
messagePort.close();
}
}
}

Future<Uri> _buildRunnerScript() async {
var packagesFile = new File(_packagesFileName);
if (!packagesFile.existsSync()) {
throw new FileSystemException(
'A `$_packagesFileName` file does not exist in the target directory.',
packagesFile.absolute.path);
}

var dataUri = new Uri.dataFromString(_bootstrapScript);

var messagePort = new ReceivePort();
var exitPort = new ReceivePort();
var errorPort = new ReceivePort();

await Isolate.spawnUri(dataUri, [], messagePort.sendPort,
onExit: exitPort.sendPort,
onError: errorPort.sendPort,
errorsAreFatal: true,
packageConfig: new Uri.file('.packages'));

var allErrorsFuture = errorPort.forEach((error) {
var errorList = error as List;
var message = errorList[0] as String;
var stack = new StackTrace.fromString(errorList[1] as String);

stderr.writeln(message);
stderr.writeln(stack);
});

var items = await Future.wait([
messagePort.toList(),
allErrorsFuture,
exitPort.first.whenComplete(() {
messagePort.close();
errorPort.close();
})
]);
try {
await Isolate.spawnUri(dataUri, [], messagePort.sendPort,
onExit: exitPort.sendPort,
onError: errorPort.sendPort,
errorsAreFatal: true,
packageConfig: new Uri.file(_packagesFileName));

var messages = items[0] as List;
if (messages.isEmpty) {
throw new StateError('An error occurred while running booting.');
}
var allErrorsFuture = errorPort.forEach((error) {
var errorList = error as List;
var message = errorList[0] as String;
var stack = new StackTrace.fromString(errorList[1] as String);

stderr.writeln(message);
stderr.writeln(stack);
});

assert(messages.length == 1);
return new Uri.file(messages.single as String);
var items = await Future.wait([
messagePort.toList(),
allErrorsFuture,
exitPort.first.whenComplete(() {
messagePort.close();
errorPort.close();
})
]);

var messages = items[0] as List;
if (messages.isEmpty) {
throw new StateError('An error occurred while bootstrapping.');
}

assert(messages.length == 1);
return new Uri.file(messages.single as String);
} finally {
messagePort.close();
exitPort.close();
errorPort.close();
}
}

const _bootstrapScript = r'''
Expand Down
12 changes: 10 additions & 2 deletions webdev/lib/src/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class PackageExceptionDetails {
'You must have a dependency on `build_runner` in `pubspec.yaml`. '
'It can be in either `dependencies` or `dev_dependencies`.');

static const noBuildWebCompilersDep = const PackageExceptionDetails._(
'A dependency on `build_web_compilers` was not found.',
description:
'You must have a dependency on `build_web_compilers` in `pubspec.yaml` '
'or transitively via another dependency.');

@override
String toString() => [error, description].join('\n');
}
Expand Down Expand Up @@ -81,8 +87,10 @@ Future checkPubspecLock() async {
}
}

// TODO: validate build_web_compilers
//var buldWebCompilers = packages['build_web_compilers'];
var buldWebCompilers = packages['build_web_compilers'];
if (buldWebCompilers == null) {
issues.add(PackageExceptionDetails.noBuildWebCompilersDep);
}

if (issues.isNotEmpty) {
throw new PackageException._(issues);
Expand Down
2 changes: 1 addition & 1 deletion webdev/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: webdev
version: 0.1.0
version: 0.1.1-dev
author: Dart Team <misc@dartlang.org>
homepage: https://github.com/dart-lang/webdev
description: >-
Expand Down
Loading