diff --git a/packages/flutter_tools/lib/src/web/chrome.dart b/packages/flutter_tools/lib/src/web/chrome.dart index b029a312b3..0dc98ac66d 100644 --- a/packages/flutter_tools/lib/src/web/chrome.dart +++ b/packages/flutter_tools/lib/src/web/chrome.dart @@ -106,13 +106,14 @@ class ChromiumLauncher { @required OperatingSystemUtils operatingSystemUtils, @required BrowserFinder browserFinder, @required Logger logger, + @visibleForTesting FileSystemUtils fileSystemUtils, }) : _fileSystem = fileSystem, _platform = platform, _processManager = processManager, _operatingSystemUtils = operatingSystemUtils, _browserFinder = browserFinder, _logger = logger, - _fileSystemUtils = FileSystemUtils( + _fileSystemUtils = fileSystemUtils ?? FileSystemUtils( fileSystem: fileSystem, platform: platform, ); @@ -268,7 +269,13 @@ class ChromiumLauncher { if (sourceLocalStorageDir.existsSync()) { targetLocalStorageDir.createSync(recursive: true); - _fileSystemUtils.copyDirectorySync(sourceLocalStorageDir, targetLocalStorageDir); + try { + _fileSystemUtils.copyDirectorySync(sourceLocalStorageDir, targetLocalStorageDir); + } on FileSystemException catch (err) { + // This is a best-effort update. Display the message in case the failure is relevant. + // one possible example is a file lock due to multiple running chrome instances. + _logger.printError('Failed to save Chrome preferences: $err'); + } } } diff --git a/packages/flutter_tools/test/general.shard/web/chrome_test.dart b/packages/flutter_tools/test/general.shard/web/chrome_test.dart index f32e8513a9..aa75ed4aa8 100644 --- a/packages/flutter_tools/test/general.shard/web/chrome_test.dart +++ b/packages/flutter_tools/test/general.shard/web/chrome_test.dart @@ -103,6 +103,45 @@ void main() { ); }); + testWithoutContext('does not crash if saving profile information fails due to a file system exception.', () async { + final MockFileSystemUtils fileSystemUtils = MockFileSystemUtils(); + final BufferLogger logger = BufferLogger.test(); + chromeLauncher = ChromiumLauncher( + fileSystem: fileSystem, + platform: platform, + processManager: processManager, + operatingSystemUtils: operatingSystemUtils, + browserFinder: findChromeExecutable, + logger: logger, + fileSystemUtils: fileSystemUtils, + ); + when(fileSystemUtils.copyDirectorySync(any, any)) + .thenThrow(const FileSystemException()); + processManager.addCommand(const FakeCommand( + command: [ + 'example_chrome', + '--user-data-dir=/.tmp_rand0/flutter_tools_chrome_device.rand0', + '--remote-debugging-port=1234', + ...kChromeArgs, + 'example_url', + ], + stderr: kDevtoolsStderr, + )); + + final Chromium chrome = await chromeLauncher.launch( + 'example_url', + skipCheck: true, + cacheDir: fileSystem.currentDirectory, + ); + + // Create cache dir that the Chrome launcher will atttempt to persist. + fileSystem.directory('/.tmp_rand0/flutter_tools_chrome_device.rand0/Default/Local Storage') + .createSync(recursive: true); + + await chrome.close(); // does not exit with error. + expect(logger.errorText, contains('Failed to save Chrome preferences')); + }); + testWithoutContext('can launch chrome with a custom debug port', () async { processManager.addCommand(const FakeCommand( command: [ @@ -207,6 +246,7 @@ void main() { }); } +class MockFileSystemUtils extends Mock implements FileSystemUtils {} class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {} Future _testLaunchChrome(String userDataDir, FakeProcessManager processManager, ChromiumLauncher chromeLauncher) {