Skip to content

Commit dbef131

Browse files
authored
Trim any text before osascript JSON response (#166296)
Fixes flutter/flutter#164772. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 0befaaa commit dbef131

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

packages/flutter_tools/lib/src/ios/xcode_debug.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,19 @@ class XcodeDebug {
269269

270270
@visibleForTesting
271271
XcodeAutomationScriptResponse? parseScriptResponse(String results) {
272+
// Some users reported text before the json. Trim any text before the opening
273+
// curly brace.
274+
// Example: `start process_extensions{"status":true,"errorMessage":null,"debugResult":{"completed":false,"status":"running","errorMessage":null}}`
275+
final String trimmedResults;
276+
final int jsonBeginIndex = results.indexOf('{');
277+
if (jsonBeginIndex > -1) {
278+
trimmedResults = results.substring(jsonBeginIndex);
279+
} else {
280+
trimmedResults = results;
281+
}
282+
272283
try {
273-
final Object decodeResult = json.decode(results) as Object;
284+
final Object decodeResult = json.decode(trimmedResults) as Object;
274285
if (decodeResult is Map<String, Object?>) {
275286
final XcodeAutomationScriptResponse response = XcodeAutomationScriptResponse.fromJson(
276287
decodeResult,
@@ -280,10 +291,10 @@ class XcodeDebug {
280291
return response;
281292
}
282293
}
283-
_logger.printError('osascript returned unexpected JSON response: $results');
294+
_logger.printError('osascript returned unexpected JSON response: $trimmedResults');
284295
return null;
285296
} on FormatException {
286-
_logger.printError('osascript returned non-JSON response: $results');
297+
_logger.printError('osascript returned non-JSON response: $trimmedResults');
287298
return null;
288299
}
289300
}

packages/flutter_tools/test/general.shard/ios/xcode_debug_test.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,27 @@ void main() {
617617
expect(logger.errorText, contains('osascript returned unexpected JSON response'));
618618
expect(response, isNull);
619619
});
620+
621+
testWithoutContext('successfully removes any text before JSON', () async {
622+
final Xcode xcode = setupXcode(
623+
fakeProcessManager: FakeProcessManager.any(),
624+
fileSystem: fileSystem,
625+
flutterRoot: flutterRoot,
626+
);
627+
final XcodeDebug xcodeDebug = XcodeDebug(
628+
logger: logger,
629+
processManager: fakeProcessManager,
630+
xcode: xcode,
631+
fileSystem: fileSystem,
632+
);
633+
634+
final XcodeAutomationScriptResponse? response = xcodeDebug.parseScriptResponse(
635+
'start process_extensions{"status":true,"errorMessage":null,"debugResult":{"completed":false,"status":"running","errorMessage":null}}',
636+
);
637+
638+
expect(logger.errorText, isEmpty);
639+
expect(response, isNotNull);
640+
});
620641
});
621642

622643
group('exit', () {

0 commit comments

Comments
 (0)