From 292695e7c139262b4741df927bd587b352159521 Mon Sep 17 00:00:00 2001 From: Ben Konyi Date: Wed, 11 Dec 2024 15:12:48 -0500 Subject: [PATCH] Don't throw `StateError` when listing core devices during tool shutdown (#160094) The previous attempt at this fix was assuming that the tool's file system was a `LocalFileSystem`, but in reality it's a `LocalFileSystem` wrapped in an `ErrorHandlingFileSystem`. This change takes this into account. Fixes https://github.com/flutter/flutter/issues/160082 Fixes https://github.com/flutter/flutter/issues/156962 --------- Co-authored-by: Andrew Kolos --- .../lib/src/base/error_handling_io.dart | 2 -- .../lib/src/ios/core_devices.dart | 12 ++++--- .../general.shard/ios/core_devices_test.dart | 32 +++++++++++++++---- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/error_handling_io.dart b/packages/flutter_tools/lib/src/base/error_handling_io.dart index e7086141d9..0dd57ef55b 100644 --- a/packages/flutter_tools/lib/src/base/error_handling_io.dart +++ b/packages/flutter_tools/lib/src/base/error_handling_io.dart @@ -7,7 +7,6 @@ import 'dart:io' as io show Directory, File, Link, Process, ProcessException, Pr import 'dart:typed_data'; import 'package:file/file.dart'; -import 'package:meta/meta.dart'; import 'package:path/path.dart' as p; // flutter_ignore: package_path_import import 'package:process/process.dart'; @@ -47,7 +46,6 @@ class ErrorHandlingFileSystem extends ForwardingFileSystem { _platform = platform, super(delegate); - @visibleForTesting FileSystem get fileSystem => delegate; final Platform _platform; diff --git a/packages/flutter_tools/lib/src/ios/core_devices.dart b/packages/flutter_tools/lib/src/ios/core_devices.dart index fb851550cd..5b820c88db 100644 --- a/packages/flutter_tools/lib/src/ios/core_devices.dart +++ b/packages/flutter_tools/lib/src/ios/core_devices.dart @@ -79,7 +79,13 @@ class IOSCoreDeviceControl { try { final RunResult result = await _processUtils.run(command, throwOnError: true); - final bool isToolPossiblyShutdown = _fileSystem is LocalFileSystem && _fileSystem.disposed; + bool isToolPossiblyShutdown = false; + if (_fileSystem is ErrorHandlingFileSystem) { + final FileSystem delegate = _fileSystem.fileSystem; + if (delegate is LocalFileSystem) { + isToolPossiblyShutdown = delegate.disposed; + } + } // It's possible that the tool is in the process of shutting down, which // could result in the temp directory being deleted after the shutdown hooks run @@ -94,10 +100,6 @@ class IOSCoreDeviceControl { _logger.printError('The process exited with code ${result.exitCode} and'); _logger.printError('Stdout:\n\n${result.stdout.trim()}\n'); _logger.printError('Stderr:\n\n${result.stderr.trim()}'); - _logger.printError('Using file system type: ${_fileSystem.runtimeType}'); - if (_fileSystem is LocalFileSystem) { - _logger.printError('LocalFileSystem disposed: ${_fileSystem.disposed}'); - } throw StateError('Expected the file ${output.path} to exist but it did not'); } else if (isToolPossiblyShutdown) { return []; diff --git a/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart b/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart index 1b54f2fa51..459dd713f4 100644 --- a/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/core_devices_test.dart @@ -4,25 +4,44 @@ import 'package:file/memory.dart'; import 'package:file_testing/file_testing.dart'; +import 'package:flutter_tools/src/base/error_handling_io.dart'; import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/logger.dart'; -import 'package:flutter_tools/src/base/signals.dart'; +import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/base/version.dart'; import 'package:flutter_tools/src/ios/core_devices.dart'; import 'package:flutter_tools/src/ios/xcodeproj.dart'; import 'package:flutter_tools/src/macos/xcode.dart'; +import 'package:test/fake.dart'; import '../../src/common.dart'; import '../../src/fake_process_manager.dart'; -class LocalFileSystemFake extends LocalFileSystem { - LocalFileSystemFake.test({required super.signals}) : super.test(); - +class LocalFileSystemFake extends Fake implements LocalFileSystem { MemoryFileSystem memoryFileSystem = MemoryFileSystem.test(); @override Directory get systemTempDirectory => memoryFileSystem.systemTempDirectory; + + @override + Directory directory(dynamic path) => memoryFileSystem.directory(path); + + @override + File file(dynamic path) => memoryFileSystem.file(path); + + @override + Context get path => memoryFileSystem.path; + + @override + Future dispose() async { + _disposed = true; + } + + @override + bool get disposed => _disposed; + + bool _disposed = false; } void main() { @@ -1411,12 +1430,13 @@ invalid JSON }); testWithoutContext('Handles file system disposal', () async { - final LocalFileSystem localFs = LocalFileSystemFake.test(signals: Signals.test()); + final LocalFileSystem localFs = LocalFileSystemFake(); + final ErrorHandlingFileSystem fs = ErrorHandlingFileSystem(delegate: localFs, platform: FakePlatform()); deviceControl = IOSCoreDeviceControl( logger: logger, processManager: fakeProcessManager, xcode: xcode, - fileSystem: localFs, + fileSystem: fs, ); final Directory tempDir = localFs.systemTempDirectory .childDirectory('core_devices.rand0');