@@ -21,9 +21,13 @@ import 'platform.dart';
2121// ToolExit and a message that is more clear than the FileSystemException by
2222// itself.
2323
24- /// On windows this is error code 2: ERROR_FILE_NOT_FOUND, and on
24+ /// On Windows this is error code 2: ERROR_FILE_NOT_FOUND, and on
2525/// macOS/Linux it is error code 2/ENOENT: No such file or directory.
26- const int kSystemCannotFindFile = 2 ;
26+ const int kSystemCodeCannotFindFile = 2 ;
27+
28+ /// On Windows this error is 3: ERROR_PATH_NOT_FOUND, and on
29+ /// macOS/Linux, it is error code 3/ESRCH: No such process.
30+ const int kSystemCodePathNotFound = 3 ;
2731
2832/// A [FileSystem] that throws a [ToolExit] on certain errors.
2933///
@@ -72,22 +76,26 @@ class ErrorHandlingFileSystem extends ForwardingFileSystem {
7276 /// This method should be preferred to checking if it exists and
7377 /// then deleting, because it handles the edge case where the file or directory
7478 /// is deleted by a different program between the two calls.
75- static bool deleteIfExists (FileSystemEntity file , {bool recursive = false }) {
76- if (! file .existsSync ()) {
79+ static bool deleteIfExists (FileSystemEntity entity , {bool recursive = false }) {
80+ if (! entity .existsSync ()) {
7781 return false ;
7882 }
7983 try {
80- file .deleteSync (recursive: recursive);
84+ entity .deleteSync (recursive: recursive);
8185 } on FileSystemException catch (err) {
8286 // Certain error codes indicate the file could not be found. It could have
8387 // been deleted by a different program while the tool was running.
8488 // if it still exists, the file likely exists on a read-only volume.
85- if (err.osError? .errorCode != kSystemCannotFindFile || _noExitOnFailure) {
89+ // This check will falsely match "3/ESRCH: No such process" on Linux/macOS,
90+ // but this should be fine since this code should never come up here.
91+ final bool codeCorrespondsToPathOrFileNotFound = err.osError? .errorCode == kSystemCodeCannotFindFile ||
92+ err.osError? .errorCode == kSystemCodePathNotFound;
93+ if (! codeCorrespondsToPathOrFileNotFound || _noExitOnFailure) {
8694 rethrow ;
8795 }
88- if (file .existsSync ()) {
96+ if (entity .existsSync ()) {
8997 throwToolExit (
90- 'The Flutter tool tried to delete the file or directory ${file .path } but was '
98+ 'The Flutter tool tried to delete the file or directory ${entity .path } but was '
9199 "unable to. This may be due to the file and/or project's location on a read-only "
92100 'volume. Consider relocating the project and trying again' ,
93101 );
@@ -104,7 +112,7 @@ class ErrorHandlingFileSystem extends ForwardingFileSystem {
104112 return _runSync (() => directory (delegate.currentDirectory), platform: _platform);
105113 } on FileSystemException catch (err) {
106114 // Special handling for OS error 2 for current directory only.
107- if (err.osError? .errorCode == kSystemCannotFindFile ) {
115+ if (err.osError? .errorCode == kSystemCodeCannotFindFile ) {
108116 throwToolExit (
109117 'Unable to read current working directory. This can happen if the directory the '
110118 'Flutter tool was run from was moved or deleted.'
0 commit comments