Skip to content

Commit 8185fd9

Browse files
committed
Cleanly handle case here no .packages file exists
1 parent 5ba096b commit 8185fd9

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed

webdev/bin/webdev.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ Future main(List<String> args) async {
2727
}
2828
}
2929

30+
exitCode = ExitCode.config.code;
31+
} on FileSystemException catch (e) {
32+
print(yellow.wrap('Could not run in the current directory.'));
33+
print(e.message);
34+
if (e.path != null) {
35+
print(' ${e.path}');
36+
}
3037
exitCode = ExitCode.config.code;
3138
}
3239
}

webdev/lib/src/command/build_runner_command_base.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import 'package:stack_trace/stack_trace.dart';
1111

1212
import '../pubspec.dart';
1313

14+
const _packagesFileName = '.packages';
15+
1416
/// Extend to get a command with the arguments common to all build_runner
1517
/// commands.
1618
abstract class BuildRunnerCommandBase extends Command<int> {
@@ -30,6 +32,8 @@ abstract class BuildRunnerCommandBase extends Command<int> {
3032
Future<int> runCore(String command) async {
3133
await checkPubspecLock();
3234

35+
var buildRunnerScript = await _buildRunnerScript();
36+
3337
final arguments = [command]..addAll(argResults.arguments);
3438

3539
var exitCode = 0;
@@ -49,8 +53,7 @@ abstract class BuildRunnerCommandBase extends Command<int> {
4953
stderr.writeln(new Trace.parse(trace).terse);
5054
if (exitCode == 0) exitCode = 1;
5155
});
52-
await Isolate.spawnUri(
53-
await _buildRunnerScript(), arguments, messagePort.sendPort,
56+
await Isolate.spawnUri(buildRunnerScript, arguments, messagePort.sendPort,
5457
onExit: exitPort.sendPort,
5558
onError: errorPort.sendPort,
5659
automaticPackageResolution: true);
@@ -74,6 +77,13 @@ abstract class BuildRunnerCommandBase extends Command<int> {
7477
}
7578

7679
Future<Uri> _buildRunnerScript() async {
80+
var packagesFile = new File(_packagesFileName);
81+
if (!packagesFile.existsSync()) {
82+
throw new FileSystemException(
83+
'A `$_packagesFileName` file does not exist in the target directory.',
84+
packagesFile.absolute.path);
85+
}
86+
7787
var dataUri = new Uri.dataFromString(_bootstrapScript);
7888

7989
var messagePort = new ReceivePort();
@@ -84,7 +94,7 @@ Future<Uri> _buildRunnerScript() async {
8494
onExit: exitPort.sendPort,
8595
onError: errorPort.sendPort,
8696
errorsAreFatal: true,
87-
packageConfig: new Uri.file('.packages'));
97+
packageConfig: new Uri.file(_packagesFileName));
8898

8999
var allErrorsFuture = errorPort.forEach((error) {
90100
var errorList = error as List;
@@ -106,7 +116,7 @@ Future<Uri> _buildRunnerScript() async {
106116

107117
var messages = items[0] as List;
108118
if (messages.isEmpty) {
109-
throw new StateError('An error occurred while running booting.');
119+
throw new StateError('An error occurred while bootstrapping.');
110120
}
111121

112122
assert(messages.length == 1);

webdev/test/integration_test.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,34 @@ packages:
9797
contains('A dependency on `build_web_compilers` was not found.'));
9898
await process.shouldExit(78);
9999
});
100+
101+
test('should fail gracefully if there is no .packages file', () async {
102+
await d.file('pubspec.lock', '''
103+
# Copy-pasted from a valid run
104+
packages:
105+
build_runner:
106+
dependency: "direct main"
107+
description:
108+
name: build_runner
109+
url: "https://pub.dartlang.org"
110+
source: hosted
111+
version: "0.8.0"
112+
build_web_compilers:
113+
dependency: "direct main"
114+
description:
115+
name: build_web_compilers
116+
url: "https://pub.dartlang.org"
117+
source: hosted
118+
version: "0.3.4+2"
119+
''').create();
120+
121+
var process = await TestProcess.start('dart', [_webdevBin, 'build'],
122+
workingDirectory: d.sandbox);
123+
var output = (await process.stdoutStream().join('\n')).trim();
124+
125+
expect(output, contains('Could not run in the current directory.'));
126+
expect(output,
127+
contains('A `.packages` file does not exist in the target directory.'));
128+
await process.shouldExit(78);
129+
});
100130
}

0 commit comments

Comments
 (0)