Skip to content

Commit

Permalink
[flutter_tools] add more debugging when pub get fails (#108062)
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherfujino authored Jul 21, 2022
1 parent 092481d commit 6abd369
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
41 changes: 29 additions & 12 deletions packages/flutter_tools/lib/src/dart/pub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -266,17 +266,10 @@ class _DefaultPub implements Pub {
} catch (exception) { // ignore: avoid_catches_without_on_clauses
status?.cancel();
if (exception is io.ProcessException) {
final StringBuffer buffer = StringBuffer(exception.message);
final StringBuffer buffer = StringBuffer('${exception.message}\n');
buffer.writeln('Working directory: "$directory"');
final Map<String, String> env = await _createPubEnvironment(context, flutterRootOverride);
if (env.entries.isNotEmpty) {
buffer.writeln('pub env: {');
for (final MapEntry<String, String> entry in env.entries) {
buffer.writeln(' "${entry.key}": "${entry.value}",');
}
buffer.writeln('}');
}

buffer.write(_stringifyPubEnv(env));
throw io.ProcessException(
exception.executable,
exception.arguments,
Expand All @@ -298,6 +291,20 @@ class _DefaultPub implements Pub {
);
}

// For surfacing pub env in crash reporting
String _stringifyPubEnv(Map<String, String> map, {String prefix = 'pub env'}) {
if (map.isEmpty) {
return '';
}
final StringBuffer buffer = StringBuffer();
buffer.writeln('$prefix: {');
for (final MapEntry<String, String> entry in map.entries) {
buffer.writeln(' "${entry.key}": "${entry.value}",');
}
buffer.writeln('}');
return buffer.toString();
}

@override
Future<void> batch(
List<String> arguments, {
Expand Down Expand Up @@ -330,13 +337,15 @@ class _DefaultPub implements Pub {
int attempts = 0;
int duration = 1;
int code;
final List<String> pubCommand = _pubCommand(arguments);
final Map<String, String> pubEnvironment = await _createPubEnvironment(context, flutterRootOverride);
while (true) {
attempts += 1;
code = await _processUtils.stream(
_pubCommand(arguments),
pubCommand,
workingDirectory: directory,
mapFunction: filterWrapper, // may set versionSolvingFailed, lastPubMessage
environment: await _createPubEnvironment(context, flutterRootOverride),
environment: pubEnvironment,
);
String? message;
if (retry) {
Expand Down Expand Up @@ -372,7 +381,15 @@ class _DefaultPub implements Pub {
).send();

if (code != 0) {
throwToolExit('$failureMessage ($code; $lastPubMessage)', exitCode: code);
final StringBuffer buffer = StringBuffer('$failureMessage\n');
buffer.writeln('command: "${pubCommand.join(' ')}"');
buffer.write(_stringifyPubEnv(pubEnvironment));
buffer.writeln('exit code: $code');
buffer.writeln('last line of pub output: "${lastPubMessage.trim()}"');
throwToolExit(
buffer.toString(),
exitCode: code,
);
}
}

Expand Down
14 changes: 12 additions & 2 deletions packages/flutter_tools/test/general.shard/dart/pub_get_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ void main() {
);
});
expect(logger.errorText, isEmpty);
expect(error, 'test failed unexpectedly: Exception: pub get failed (69; no message)');
expect(error, contains('test failed unexpectedly: Exception: pub get failed'));
expect(processManager, hasNoRemainingExpectations);
});

Expand Down Expand Up @@ -643,9 +643,19 @@ void main() {
botDetector: const BotDetectorAlwaysNo(),
processManager: processManager,
);
const String toolExitMessage = '''
pub get failed
command: "bin/cache/dart-sdk/bin/dart __deprecated_pub --verbosity=warning get --no-precompile"
pub env: {
"FLUTTER_ROOT": "",
"PUB_ENVIRONMENT": "flutter_cli:flutter_tests",
}
exit code: 66
last line of pub output: "err3"
''';
await expectLater(
() => pub.get(context: PubContext.flutterTests),
throwsA(isA<ToolExit>().having((ToolExit error) => error.message, 'message', 'pub get failed (66; err3)')),
throwsA(isA<ToolExit>().having((ToolExit error) => error.message, 'message', toolExitMessage)),
);
expect(logger.statusText,
'Running "flutter pub get" in /...\n'
Expand Down

0 comments on commit 6abd369

Please sign in to comment.