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 774181c88b..322cb7158c 100644 --- a/packages/flutter_tools/lib/src/base/error_handling_io.dart +++ b/packages/flutter_tools/lib/src/base/error_handling_io.dart @@ -63,6 +63,40 @@ class ErrorHandlingFileSystem extends ForwardingFileSystem { } } + /// Delete the file or directory and return true if it exists, take no + /// action and return false if it does not. + /// + /// This method should be prefered to checking if it exists and + /// then deleting, because it handles the edge case where the file or directory + /// is deleted by a different program between the two calls. + static bool deleteIfExists(FileSystemEntity file, {bool recursive = false}) { + if (!file.existsSync()) { + return false; + } + try { + file.deleteSync(recursive: recursive); + } on FileSystemException catch (err) { + // Certain error codes indicate the file could not be found. It could have + // been deleted by a different program while the tool was running. + // if it still exists, the file likely exists on a read-only volume. + // + // On windows this is error code 2: ERROR_FILE_NOT_FOUND, and on + // macOS/Linux it is error code 2/ENOENT: No such file or directory. + const int kSystemCannotFindFile = 2; + if (err?.osError?.errorCode != kSystemCannotFindFile || _noExitOnFailure) { + rethrow; + } + if (file.existsSync()) { + throwToolExit( + 'The Flutter tool tried to delete the file or directory ${file.path} but was ' + 'unable to. This may be due to the file and/or project\'s location on a read-only ' + 'volume. Consider relocating the project and trying again', + ); + } + } + return true; + } + static bool _noExitOnFailure = false; @override diff --git a/packages/flutter_tools/lib/src/build_system/build_system.dart b/packages/flutter_tools/lib/src/build_system/build_system.dart index 5aa7666228..ccbf36255f 100644 --- a/packages/flutter_tools/lib/src/build_system/build_system.dart +++ b/packages/flutter_tools/lib/src/build_system/build_system.dart @@ -10,6 +10,7 @@ import 'package:pool/pool.dart'; import 'package:process/process.dart'; import '../artifacts.dart'; +import '../base/error_handling_io.dart'; import '../base/file_system.dart'; import '../base/logger.dart'; import '../base/platform.dart'; @@ -160,9 +161,7 @@ abstract class Target { /// Invoke to remove the stamp file if the [buildAction] threw an exception. void clearStamp(Environment environment) { final File stamp = _findStampFile(environment); - if (stamp.existsSync()) { - stamp.deleteSync(); - } + ErrorHandlingFileSystem.deleteIfExists(stamp); } void _writeStamp( @@ -697,9 +696,7 @@ class FlutterBuildSystem extends BuildSystem { for (final String lastOutput in lastOutputs) { if (!currentOutputs.containsKey(lastOutput)) { final File lastOutputFile = fileSystem.file(lastOutput); - if (lastOutputFile.existsSync()) { - lastOutputFile.deleteSync(); - } + ErrorHandlingFileSystem.deleteIfExists(lastOutputFile); } } } @@ -820,9 +817,7 @@ class _BuildInstance { continue; } final File previousFile = fileSystem.file(previousOutput); - if (previousFile.existsSync()) { - previousFile.deleteSync(); - } + ErrorHandlingFileSystem.deleteIfExists(previousFile); } } on Exception catch (exception, stackTrace) { // TODO(jonahwilliams): throw specific exception for expected errors to mark diff --git a/packages/flutter_tools/lib/src/build_system/depfile.dart b/packages/flutter_tools/lib/src/build_system/depfile.dart index 9c7b1f11e3..2a654986c7 100644 --- a/packages/flutter_tools/lib/src/build_system/depfile.dart +++ b/packages/flutter_tools/lib/src/build_system/depfile.dart @@ -4,6 +4,7 @@ import 'package:meta/meta.dart'; +import '../base/error_handling_io.dart'; import '../base/file_system.dart'; import '../base/logger.dart'; @@ -26,9 +27,7 @@ class DepfileService { /// exist. void writeToFile(Depfile depfile, File output) { if (depfile.inputs.isEmpty || depfile.outputs.isEmpty) { - if (output.existsSync()) { - output.deleteSync(); - } + ErrorHandlingFileSystem.deleteIfExists(output); return; } final StringBuffer buffer = StringBuffer(); diff --git a/packages/flutter_tools/lib/src/cache.dart b/packages/flutter_tools/lib/src/cache.dart index 196b5dbe07..e997b3fdbf 100644 --- a/packages/flutter_tools/lib/src/cache.dart +++ b/packages/flutter_tools/lib/src/cache.dart @@ -10,6 +10,7 @@ import 'package:process/process.dart'; import 'android/gradle_utils.dart'; import 'base/common.dart'; +import 'base/error_handling_io.dart'; import 'base/file_system.dart'; import 'base/io.dart' show HttpClient, HttpClientRequest, HttpClientResponse, HttpStatus, ProcessException, SocketException; import 'base/logger.dart'; @@ -436,9 +437,7 @@ class Cache { getStampFileFor('flutter_tools').deleteSync(); for (final ArtifactSet artifact in _artifacts) { final File file = getStampFileFor(artifact.stampName); - if (file.existsSync()) { - file.deleteSync(); - } + ErrorHandlingFileSystem.deleteIfExists(file); } } on FileSystemException catch (err) { _logger.printError('Failed to delete some stamp files: $err'); diff --git a/packages/flutter_tools/lib/src/macos/cocoapods.dart b/packages/flutter_tools/lib/src/macos/cocoapods.dart index b424463863..e3b7dbc081 100644 --- a/packages/flutter_tools/lib/src/macos/cocoapods.dart +++ b/packages/flutter_tools/lib/src/macos/cocoapods.dart @@ -7,6 +7,7 @@ import 'package:meta/meta.dart'; import 'package:process/process.dart'; import '../base/common.dart'; +import '../base/error_handling_io.dart'; import '../base/file_system.dart'; import '../base/io.dart'; import '../base/logger.dart'; @@ -307,9 +308,7 @@ class CocoaPods { /// Ensures that pod install is deemed needed on next check. void invalidatePodInstallOutput(XcodeBasedProject xcodeProject) { final File manifestLock = xcodeProject.podManifestLock; - if (manifestLock.existsSync()) { - manifestLock.deleteSync(); - } + ErrorHandlingFileSystem.deleteIfExists(manifestLock); } // Check if you need to run pod install. diff --git a/packages/flutter_tools/lib/src/plugins.dart b/packages/flutter_tools/lib/src/plugins.dart index fc12f99488..17e245b38c 100644 --- a/packages/flutter_tools/lib/src/plugins.dart +++ b/packages/flutter_tools/lib/src/plugins.dart @@ -9,6 +9,7 @@ import 'package:yaml/yaml.dart'; import 'android/gradle.dart'; import 'base/common.dart'; +import 'base/error_handling_io.dart'; import 'base/file_system.dart'; import 'convert.dart'; import 'dart/package_map.dart'; @@ -435,11 +436,7 @@ const String _kFlutterPluginsDependenciesKey = 'dependencies'; bool _writeFlutterPluginsList(FlutterProject project, List plugins) { final File pluginsFile = project.flutterPluginsDependenciesFile; if (plugins.isEmpty) { - if (pluginsFile.existsSync()) { - pluginsFile.deleteSync(); - return true; - } - return false; + return ErrorHandlingFileSystem.deleteIfExists(pluginsFile); } final String iosKey = project.ios.pluginConfigKey; @@ -506,11 +503,7 @@ List _createPluginLegacyDependencyGraph(List plugins) { bool _writeFlutterPluginsListLegacy(FlutterProject project, List plugins) { final File pluginsFile = project.flutterPluginsFile; if (plugins.isEmpty) { - if (pluginsFile.existsSync()) { - pluginsFile.deleteSync(); - return true; - } - return false; + return ErrorHandlingFileSystem.deleteIfExists(pluginsFile); } const String info = 'This is a generated file; do not edit or check into version control.'; @@ -1051,9 +1044,7 @@ Future _writeWebPluginRegistrant(FlutterProject project, List plug final String filePath = globals.fs.path.join(registryDirectory, 'generated_plugin_registrant.dart'); if (webPlugins.isEmpty) { final File file = globals.fs.file(filePath); - if (file.existsSync()) { - file.deleteSync(); - } + return ErrorHandlingFileSystem.deleteIfExists(file); } else { _renderTemplateToFile( _dartPluginRegistryTemplate, diff --git a/packages/flutter_tools/lib/src/tracing.dart b/packages/flutter_tools/lib/src/tracing.dart index d38a2a6dfd..5c2ff04c53 100644 --- a/packages/flutter_tools/lib/src/tracing.dart +++ b/packages/flutter_tools/lib/src/tracing.dart @@ -3,14 +3,15 @@ // found in the LICENSE file. import 'dart:async'; + import 'package:meta/meta.dart'; import 'package:vm_service/vm_service.dart' as vm_service; import 'base/common.dart'; +import 'base/error_handling_io.dart'; import 'base/file_system.dart'; import 'base/logger.dart'; import 'base/utils.dart'; - import 'vmservice.dart'; // Names of some of the Timeline events we care about. @@ -94,9 +95,7 @@ Future downloadStartupTrace(vm_service.VmService vmService, { final File traceInfoFile = output.childFile('start_up_info.json'); // Delete old startup data, if any. - if (traceInfoFile.existsSync()) { - traceInfoFile.deleteSync(); - } + ErrorHandlingFileSystem.deleteIfExists(traceInfoFile); // Create "build" directory, if missing. if (!traceInfoFile.parent.existsSync()) { diff --git a/packages/flutter_tools/test/commands.shard/permeable/analyze_once_test.dart b/packages/flutter_tools/test/commands.shard/permeable/analyze_once_test.dart index 3f4207bfb0..0b3064f327 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/analyze_once_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/analyze_once_test.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'package:flutter_tools/src/base/error_handling_io.dart'; import 'package:flutter_tools/src/globals.dart' as globals; import 'package:flutter_tools/src/artifacts.dart'; import 'package:flutter_tools/src/base/common.dart'; @@ -246,9 +247,7 @@ flutter_project:lib/ exitCode: 1, ); } finally { - if (optionsFile.existsSync()) { - optionsFile.deleteSync(); - } + ErrorHandlingFileSystem.deleteIfExists(optionsFile); } }); diff --git a/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart b/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart index 3bb2bccc46..55c5f85abb 100644 --- a/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/packages_test.dart @@ -6,6 +6,7 @@ import 'dart:convert'; import 'package:args/command_runner.dart'; import 'package:flutter_tools/src/base/bot_detector.dart'; +import 'package:flutter_tools/src/base/error_handling_io.dart'; import 'package:flutter_tools/src/base/file_system.dart' hide IOSink; import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/cache.dart'; @@ -182,9 +183,7 @@ void main() { ].expand((List list) => list); for (final String path in allFiles) { final File file = globals.fs.file(globals.fs.path.join(projectPath, path)); - if (file.existsSync()) { - file.deleteSync(); - } + ErrorHandlingFileSystem.deleteIfExists(file); } } diff --git a/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart b/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart index 06d1614833..00df1a4b76 100644 --- a/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart +++ b/packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart @@ -111,6 +111,58 @@ void setupDirectoryMocks({ } void main() { + testWithoutContext('deleteIfExists does not delete if file does not exist', () { + final File file = MockFile(); + when(file.existsSync()).thenReturn(false); + + expect(ErrorHandlingFileSystem.deleteIfExists(file), false); + }); + + testWithoutContext('deleteIfExists deletes if file exists', () { + final File file = MockFile(); + when(file.existsSync()).thenReturn(true); + + expect(ErrorHandlingFileSystem.deleteIfExists(file), true); + }); + + testWithoutContext('deleteIfExists handles separate program deleting file', () { + final File file = MockFile(); + bool exists = true; + // Return true for the first call, false for any subsequent calls. + when(file.existsSync()).thenAnswer((Invocation _) { + final bool result = exists; + exists = false; + return result; + }); + when(file.deleteSync(recursive: false)) + .thenThrow(const FileSystemException('', '', OSError('', 2))); + + expect(ErrorHandlingFileSystem.deleteIfExists(file), true); + }); + + testWithoutContext('deleteIfExists throws tool exit if file exists on read-only volume', () { + final File file = MockFile(); + when(file.existsSync()).thenReturn(true); + when(file.deleteSync(recursive: false)) + .thenThrow(const FileSystemException('', '', OSError('', 2))); + + expect(() => ErrorHandlingFileSystem.deleteIfExists(file), throwsA(isA())); + }); + + testWithoutContext('deleteIfExists does not tool exit if file exists on read-only ' + 'volume and it is run under noExitOnFailure', () { + final File file = MockFile(); + when(file.existsSync()).thenReturn(true); + when(file.deleteSync(recursive: false)) + .thenThrow(const FileSystemException('', '', OSError('', 2))); + + expect(() { + ErrorHandlingFileSystem.noExitOnFailure(() { + ErrorHandlingFileSystem.deleteIfExists(file); + }); + }, throwsA(isA())); + }); + group('throws ToolExit on Windows', () { const int kDeviceFull = 112; const int kUserMappedSectionOpened = 1224; diff --git a/packages/flutter_tools/test/general.shard/base/file_system_test.dart b/packages/flutter_tools/test/general.shard/base/file_system_test.dart index 3ead31becd..2c9161cbf0 100644 --- a/packages/flutter_tools/test/general.shard/base/file_system_test.dart +++ b/packages/flutter_tools/test/general.shard/base/file_system_test.dart @@ -180,3 +180,4 @@ void main() { } class MockIoProcessSignal extends Mock implements io.ProcessSignal {} +class MockFile extends Mock implements File {}