Skip to content

Directory.rename should not delete the target if it exists #47653

Description

@brianquinlan

Change

Currently Directory.rename() has two undesirable properties on Windows:

  1. if the target path is an existing directory then it is deleted
  2. if the target path matches the current path, then the directory is deleted

I propose that we remove both of those behaviors i.e. it is an no-op if the src and target paths are identical and it is an error if the target path is an existing directory.

See https://dart-review.googlesource.com/c/sdk/+/219501

Rationale

This change will bring the semantics of Directory.rename() on Windows inline with the semantics on every other platform - except that it will be an error to rename to a existing empty directory, which is allowed on POSIX systems.

This is inline with the behavior offered in other programming languages:
https://en.cppreference.com/w/cpp/filesystem/rename
https://docs.python.org/3/library/os.html#os.rename
https://docs.oracle.com/javase/7/docs/api/java/io/File.html#renameTo(java.io.File)
https://doc.rust-lang.org/std/fs/fn.rename.html

Impact

It is possible (but not likely IMO) that some dart Windows-only programs rely on this behavior but it is more likely that existing programs have a large unintended footgun and this will remove it.

Mitigation

Users can first remove the target directory before rename i.e.

Directory(target).deleteSync()
Directory(src).renameSync(target)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.breaking-change-requestThis tracks requests for feedback on breaking changeslibrary-ioos-windows

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions