Skip to content

Commit e48d837

Browse files
committed
[flutter_tools] Lazily construct CrashReportSender in SwiftPM migration
Replace `CrashReportSender? crashReportSender` in `SwiftPackageManagerIntegrationMigration` with an optional `CrashReportSender Function()? crashReportSenderFactory` so `CrashReportSender` and its underlying `http.Client` are only instantiated if `getInfo` throws an exception on non-bot builds. Update the `validate project settings` tests in `swift_package_manager_integration_migration_test.dart` to pass the factory and verify it is not invoked when project settings compile. Previous output: ``` 00:32 +3002 ~2 -1: test/general.shard/migrations/swift_package_manager_integration_migration_test.dart: Flutter Package Migration validate project settings throw if settings fail to compile [E] TimeoutException after 0:00:02.000000: Test timed out after 2 seconds. dart:isolate _RawReceivePort._handleMessage ``` Current output: ``` +157: All tests passed! ``` Fixes flutter#196307
1 parent 3f921a4 commit e48d837

4 files changed

Lines changed: 75 additions & 24 deletions

File tree

‎packages/flutter_tools/lib/src/ios/mac.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ Future<XcodeBuildResult> buildXcodeProject({
171171
darwinPlatform,
172172
buildInfo,
173173
analytics: analytics,
174-
crashReportSender: botDetector.isRunningOnBot
174+
crashReportSenderFactory: botDetector.isRunningOnBot
175175
? null
176-
: CrashReportSender(
176+
: () => CrashReportSender(
177177
platform: platform,
178178
logger: logger,
179179
operatingSystemUtils: os,

‎packages/flutter_tools/lib/src/macos/build_macos.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ Future<void> buildMacOS({
152152
darwinPlatform,
153153
buildInfo,
154154
analytics: analytics,
155-
crashReportSender: botDetector.isRunningOnBot
155+
crashReportSenderFactory: botDetector.isRunningOnBot
156156
? null
157-
: CrashReportSender(
157+
: () => CrashReportSender(
158158
platform: platform,
159159
logger: logger,
160160
operatingSystemUtils: os,

‎packages/flutter_tools/lib/src/migrations/swift_package_manager_integration_migration.dart‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class SwiftPackageManagerIntegrationMigration extends ProjectMigrator {
3535
required this._plistParser,
3636
required this._toolContext,
3737
required this._xcodeProjectInterpreter,
38-
this._crashReportSender,
38+
this._crashReportSenderFactory,
3939
}) : _xcodeProject = project,
4040
_xcodeProjectInfoFile = project.xcodeProjectInfoFile,
4141
super(_toolContext.logger);
@@ -44,7 +44,7 @@ class SwiftPackageManagerIntegrationMigration extends ProjectMigrator {
4444
final FlutterDarwinPlatform _platform;
4545
final BuildInfo _buildInfo;
4646
final Analytics _analytics;
47-
final CrashReportSender? _crashReportSender;
47+
final CrashReportSender Function()? _crashReportSenderFactory;
4848
final PlistParser _plistParser;
4949
final ToolContext _toolContext;
5050
final XcodeProjectInterpreter _xcodeProjectInterpreter;
@@ -240,10 +240,10 @@ class SwiftPackageManagerIntegrationMigration extends ProjectMigrator {
240240
);
241241
} on Exception catch (e, stackTrace) {
242242
// Send error to crash reporter.
243-
if (_crashReportSender case final crashReportSender?) {
243+
if (_crashReportSenderFactory case final crashReportSenderFactory?) {
244244
try {
245245
await asyncGuard(() async {
246-
await crashReportSender.sendReport(
246+
await crashReportSenderFactory().sendReport(
247247
error: e,
248248
stackTrace: stackTrace,
249249
getFlutterVersion: _flutterVersionString,

‎packages/flutter_tools/test/general.shard/migrations/swift_package_manager_integration_migration_test.dart‎

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4584,7 +4584,7 @@ void main() {
45844584
platform,
45854585
BuildInfo.debug,
45864586
analytics: fakeAnalytics,
4587-
crashReportSender: crashReportSender,
4587+
crashReportSenderFactory: () => crashReportSender,
45884588
plistParser: plistParser,
45894589
toolContext: FakeToolContext(
45904590
config: FakeConfig(),
@@ -4654,7 +4654,7 @@ void main() {
46544654
platform,
46554655
BuildInfo.debug,
46564656
analytics: fakeAnalytics,
4657-
crashReportSender: crashReportSender,
4657+
crashReportSenderFactory: () => crashReportSender,
46584658
plistParser: plistParser,
46594659
toolContext: FakeToolContext(
46604660
config: FakeConfig(),
@@ -4697,7 +4697,66 @@ void main() {
46974697
expect(report.typeId, 'XcodeBuildError');
46984698
});
46994699

4700-
testWithoutContext('does not send crash report if crashReportSender is null', () async {
4700+
testWithoutContext(
4701+
'does not send crash report if crashReportSenderFactory is null',
4702+
() async {
4703+
final memoryFileSystem = MemoryFileSystem();
4704+
final testLogger = BufferLogger.test();
4705+
final FakeAnalytics fakeAnalytics = getInitializedFakeAnalyticsInstance(
4706+
fs: memoryFileSystem,
4707+
fakeFlutterVersion: FakeFlutterVersion(),
4708+
);
4709+
const FlutterDarwinPlatform platform = FlutterDarwinPlatform.ios;
4710+
final project = FakeXcodeProject(
4711+
platform: platform.name,
4712+
fileSystem: memoryFileSystem,
4713+
logger: testLogger,
4714+
);
4715+
_createProjectFiles(project, platform);
4716+
project.xcodeProjectInfoFile.writeAsStringSync(
4717+
_projectSettings(_allSectionsUnmigrated(platform)),
4718+
);
4719+
4720+
final plistParser = FakePlistParser.multiple(<String>[
4721+
_plutilOutput(_allSectionsUnmigratedAsJson(platform)),
4722+
_plutilOutput(_allSectionsMigratedAsJson(platform)),
4723+
]);
4724+
4725+
final projectMigration = SwiftPackageManagerIntegrationMigration(
4726+
project,
4727+
platform,
4728+
BuildInfo.debug,
4729+
analytics: fakeAnalytics,
4730+
plistParser: plistParser,
4731+
toolContext: FakeToolContext(
4732+
config: FakeConfig(),
4733+
flutterVersion: FakeFlutterVersion(),
4734+
fs: memoryFileSystem,
4735+
logger: testLogger,
4736+
os: FakeOperatingSystemUtils(),
4737+
platform: FakePlatform(),
4738+
),
4739+
xcodeProjectInterpreter: FakeXcodeProjectInterpreter(throwErrorOnGetInfo: true),
4740+
);
4741+
await expectLater(
4742+
() => projectMigration.migrate(),
4743+
throwsToolExit(message: 'Unable to get Xcode project information'),
4744+
);
4745+
expect(
4746+
fakeAnalytics.sentEvents,
4747+
contains(
4748+
Event.appleUsageEvent(
4749+
workflow: 'swiftpm-migration-failure',
4750+
parameter: 'full',
4751+
result: 'Xcode failed for unknown reason.',
4752+
),
4753+
),
4754+
);
4755+
expect(testLogger.traceText, isNot(contains('Sending crash report to Google.')));
4756+
},
4757+
);
4758+
4759+
testWithoutContext('does not invoke crashReportSenderFactory if settings compile', () async {
47014760
final memoryFileSystem = MemoryFileSystem();
47024761
final testLogger = BufferLogger.test();
47034762
final FakeAnalytics fakeAnalytics = getInitializedFakeAnalyticsInstance(
@@ -4725,6 +4784,8 @@ void main() {
47254784
platform,
47264785
BuildInfo.debug,
47274786
analytics: fakeAnalytics,
4787+
crashReportSenderFactory: () =>
4788+
fail('crashReportSenderFactory should not be invoked when settings compile'),
47284789
plistParser: plistParser,
47294790
toolContext: FakeToolContext(
47304791
config: FakeConfig(),
@@ -4734,23 +4795,13 @@ void main() {
47344795
os: FakeOperatingSystemUtils(),
47354796
platform: FakePlatform(),
47364797
),
4737-
xcodeProjectInterpreter: FakeXcodeProjectInterpreter(throwErrorOnGetInfo: true),
4738-
);
4739-
await expectLater(
4740-
() => projectMigration.migrate(),
4741-
throwsToolExit(message: 'Unable to get Xcode project information'),
4798+
xcodeProjectInterpreter: FakeXcodeProjectInterpreter(),
47424799
);
4800+
await projectMigration.migrate();
47434801
expect(
47444802
fakeAnalytics.sentEvents,
4745-
contains(
4746-
Event.appleUsageEvent(
4747-
workflow: 'swiftpm-migration-failure',
4748-
parameter: 'full',
4749-
result: 'Xcode failed for unknown reason.',
4750-
),
4751-
),
4803+
contains(Event.appleUsageEvent(workflow: 'swiftpm-migration-success', parameter: 'full')),
47524804
);
4753-
expect(testLogger.traceText, isNot(contains('Sending crash report to Google.')));
47544805
});
47554806

47564807
testWithoutContext('restore project settings from backup on failure', () async {

0 commit comments

Comments
 (0)