Skip to content

[tool] consider making ErrorHandlingFileSystem.deleteIfExists catch system error code ERROR_PATH_NOT_FOUND (3) on Windows #150736

Closed

Description

@andrewkolos

Discovered while investigating #137168. As of writing this, I believe that resolving this issue is prerequisite to solving that one.

Consider #137168. The crashing exception here is PathNotFoundException: PathNotFoundException: Deletion failed, path = 'D:\<project dir path>\windows\flutter\ephemeral\.plugin_symlinks' (OS Error: The system cannot find the path specified. , errno = 3). However, if we look at the code that threw this, we notice that it confirms the existence of the directory before it tries to delete it.

if (force && symlinkDirectory.existsSync()) {
// Start fresh to avoid stale links.
symlinkDirectory.deleteSync(recursive: true);
}

Barring some more complex bug in the tool or Dart's filesystem abstractions, this strongly suggests that something happened to this directory in the time between the tool checking for its existence and trying to delete it.

While it could be argued that this deleteSync call should be replaced a ErrorHandlingFileSystem.deleteIfExists call, this still would not be sufficient to prevent the crash, because deleteIfExists will still throw if it encounters ERROR_PATH_NOT_FOUND:

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.
if (err.osError?.errorCode != kSystemCannotFindFile || _noExitOnFailure) {
rethrow;
}

Notice that if the error code is not kSystemCannotFindFile, we will rethrow. Here is the definition of this constant:

/// 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;

We should augment deleteIfExists to not rethrow in the presence of ERROR_PATH_NOT_FOUND on Windows.

Activity

  1. added
    toolAffects the "flutter" command-line tool. See also t: labels.
    P2Important issues not at the top of the work list
    team-toolOwned by Flutter Tool team
    triaged-toolTriaged by Flutter Tool team
    on Jun 24, 2024
  2. self-assigned this
    on Jun 24, 2024
  3. andrewkolos commented on Jun 24, 2024

    @andrewkolos
    ContributorAuthor

    This isn't an easy fix, because ErrorHandlingFileSystem.deleteIfExists does not know what the OS of the argued FileSystemEntity is, and FileSystemEntity (correctly IMO) does not expose this. The current implementation lucks out on the fact that system code 2 means more or less the same thing on Windows and POSIX file systems. However, this is not the case with code 3. On POSIX systems, 3 corresponds to "no such process."1 We could proceed with just catching 3 regardless since this error code is (probably?) entirely disjoint from file-system related issues. It would just be a bit of a smelly hack.

    I can't think of any "cleaner" ways to solve this. One way would be to modify ErrorHandlingFileSystem.deleteIfExists to have a required FileSystemStyle style parameter, would be annoying and possibly prone to error. Another way would be to ban direct operations on the File and Directory interfaces throughout the flutter tool. Instead, tool code would operate directly only on ErrorHandlingFile and ErrorHandlingDirectory, which would expose deleteIfExists and deleteIfExistsSync on their parent type (would would probably be a new type we introduce).

    Footnotes

    1. https://github.com/torvalds/linux/blob/master/include/uapi/asm-generic/errno-base.h#L7 ↩

  4. github-actions commented on Jul 10, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  5. locked as resolved and limited conversation to collaborators on Jul 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listteam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions