diff --git a/packages/flutter_tools/lib/src/web/chrome.dart b/packages/flutter_tools/lib/src/web/chrome.dart index 427f1d85da..95c1491e95 100644 --- a/packages/flutter_tools/lib/src/web/chrome.dart +++ b/packages/flutter_tools/lib/src/web/chrome.dart @@ -344,9 +344,13 @@ class ChromiumLauncher { final Directory sourceChromeDefault = _fileSystem.directory(_fileSystem.path.join(cacheDir.path ?? '', _chromeDefaultPath)); final Directory targetChromeDefault = _fileSystem.directory(_fileSystem.path.join(userDataDir.path, _chromeDefaultPath)); - if (sourceChromeDefault.existsSync()) { - targetChromeDefault.createSync(recursive: true); - _fileSystemUtils.copyDirectorySync(sourceChromeDefault, targetChromeDefault); + try { + if (sourceChromeDefault.existsSync()) { + targetChromeDefault.createSync(recursive: true); + _fileSystemUtils.copyDirectorySync(sourceChromeDefault, targetChromeDefault); + } + } on FileSystemException catch (err) { + _logger.printError('Failed to restore 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 14f474822f..91fc7d697a 100644 --- a/packages/flutter_tools/test/general.shard/web/chrome_test.dart +++ b/packages/flutter_tools/test/general.shard/web/chrome_test.dart @@ -13,7 +13,6 @@ import 'package:flutter_tools/src/base/os.dart'; import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/web/chrome.dart'; import 'package:mockito/mockito.dart'; - import '../../src/common.dart'; import '../../src/context.dart'; @@ -143,6 +142,46 @@ void main() { expect(logger.errorText, contains('Failed to save Chrome preferences')); }); + testWithoutContext('does not crash if restoring 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, + )); + + fileSystem.currentDirectory.childDirectory('Default').createSync(); + 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 restore Chrome preferences')); + }); + testWithoutContext('can launch chrome with a custom debug port', () async { processManager.addCommand(const FakeCommand( command: [