Skip to content

Support recursively deleting long file names on Windows #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/io_file/lib/src/vm_windows_file_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ final class WindowsFileSystem extends FileSystem {
void removeDirectoryTree(String path) => using((arena) {
_primeGetLastError();

path = _extendedPath(path, arena).toDartString();
final findData = arena<win32.WIN32_FIND_DATA>();
final searchPath = p.join(path, '*');

Expand Down
103 changes: 65 additions & 38 deletions pkgs/io_file/test/remove_directory_tree_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,49 +44,40 @@ void main() {
);
});

test(
'absolute path, long directory name',
() {
// On Windows:
// When using an API to create a directory, the specified path cannot be
// so long that you cannot append an 8.3 file name (that is, the
// directory name cannot exceed MAX_PATH minus 12).
final dirname =
'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);
final path = p.join(tmp, dirname);
io.Directory(path).createSync();
io.File('$path/file').writeAsStringSync('Hello World!');
test('absolute path, long directory name', () {
// On Windows:
// When using an API to create a directory, the specified path cannot be
// so long that you cannot append an 8.3 file name (that is, the
// directory name cannot exceed MAX_PATH minus 12).
final dirname = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);
final path = p.join(tmp, dirname);
io.Directory(path).createSync();
io.File('$path/file').writeAsStringSync('Hello World!');

fileSystem.removeDirectoryTree(path);
fileSystem.removeDirectoryTree(path);

expect(
io.FileSystemEntity.typeSync(path),
io.FileSystemEntityType.notFound,
);
},
skip: io.Platform.isWindows ? 'TODO(bquinlan): make this pass' : false,
);
expect(
io.FileSystemEntity.typeSync(path),
io.FileSystemEntityType.notFound,
);
});

test(
'relative path, long directory name',
() {
// On Windows:
// When using an API to create a directory, the specified path cannot be
// so long that you cannot append an 8.3 file name (that is, the
// directory name cannot exceed MAX_PATH minus 12).
final path = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);
io.Directory(path).createSync();
io.File('$path/file').writeAsStringSync('Hello World!');
test('relative path, long directory name', () {
// On Windows:
// When using an API to create a directory, the specified path cannot be
// so long that you cannot append an 8.3 file name (that is, the
// directory name cannot exceed MAX_PATH minus 12).
final path = 'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);
io.Directory(path).createSync();
io.File('$path/file').writeAsStringSync('Hello World!');

fileSystem.removeDirectoryTree(path);
fileSystem.removeDirectoryTree(path);

expect(
io.FileSystemEntity.typeSync(path),
io.FileSystemEntityType.notFound,
);
},
skip: io.Platform.isWindows ? 'TODO(bquinlan) make this pass' : false,
);
expect(
io.FileSystemEntity.typeSync(path),
io.FileSystemEntityType.notFound,
);
});

test('contains single file', () {
final path = '$tmp/dir';
Expand Down Expand Up @@ -156,6 +147,42 @@ void main() {
);
});

test('complex tree of long paths', () {
// On Windows:
// When using an API to create a directory, the specified path cannot be
// so long that you cannot append an 8.3 file name (that is, the
// directory name cannot exceed MAX_PATH minus 12).
void createTree(String path, int depth) {
io.Directory(path).createSync();

final filePath = 'f' * 255;
final fileLinkPath = 'l' * 255;
final directoryPath =
'd' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);
final directoryLinkPath =
's' * (io.Platform.isWindows ? win32.MAX_PATH - 12 : 255);

io.File('$path/$filePath').writeAsStringSync('Hello World');
io.Link('$path/$fileLinkPath').createSync('$path/file1');
io.Link('$path/$directoryLinkPath').createSync(path);

if (depth > 0) {
createTree('$path/$directoryPath', depth - 1);
}
}

final path = '$tmp/dir';
// macOS has a maximum path length of 1024 characters.
createTree(path, 2);

fileSystem.removeDirectoryTree(path);

expect(
io.FileSystemEntity.typeSync(path),
io.FileSystemEntityType.notFound,
);
});

test(
'unremoveable file',
() {
Expand Down
Loading