Skip to content

Commit af913a7

Browse files
authored
Use ErrorHandlingFileSystem.deleteIfExists when deleting .plugin_symlinks (#151073)
Fixes flutter/flutter#137168.
1 parent acb41f3 commit af913a7

File tree

4 files changed

+75
-6
lines changed

4 files changed

+75
-6
lines changed

packages/flutter_tools/lib/src/base/error_handling_io.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ class ErrorHandlingFileSystem extends ForwardingFileSystem {
9595
}
9696
if (entity.existsSync()) {
9797
throwToolExit(
98-
'The Flutter tool tried to delete the file or directory ${entity.path} but was '
99-
"unable to. This may be due to the file and/or project's location on a read-only "
100-
'volume. Consider relocating the project and trying again',
98+
'Unable to delete file or directory at "${entity.path}". '
99+
'This may be due to the project being in a read-only '
100+
'volume. Consider relocating the project and trying again.',
101101
);
102102
}
103103
}

packages/flutter_tools/lib/src/flutter_plugins.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -966,9 +966,9 @@ void handleSymlinkException(FileSystemException e, {
966966
///
967967
/// If [force] is true, the directory will be created only if missing.
968968
void _createPlatformPluginSymlinks(Directory symlinkDirectory, List<Object?>? platformPlugins, {bool force = false}) {
969-
if (force && symlinkDirectory.existsSync()) {
969+
if (force) {
970970
// Start fresh to avoid stale links.
971-
symlinkDirectory.deleteSync(recursive: true);
971+
ErrorHandlingFileSystem.deleteIfExists(symlinkDirectory, recursive: true);
972972
}
973973
symlinkDirectory.createSync(recursive: true);
974974
if (platformPlugins == null) {

packages/flutter_tools/test/general.shard/cache_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ void main() {
909909
handler.addError(webCacheDirectory, FileSystemOp.delete, const FileSystemException('', '', OSError('', 2)));
910910

911911
await expectLater(() => webSdk.updateInner(artifactUpdater, fileSystem, FakeOperatingSystemUtils()), throwsToolExit(
912-
message: RegExp('The Flutter tool tried to delete the file or directory cache/bin/cache/flutter_web_sdk but was unable to'),
912+
message: RegExp('Unable to delete file or directory at "cache/bin/cache/flutter_web_sdk"'),
913913
));
914914
});
915915

packages/flutter_tools/test/general.shard/plugins_test.dart

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
// found in the LICENSE file.
44

55
import 'dart:convert';
6+
import 'dart:io';
67

78
import 'package:file/file.dart';
89
import 'package:file/memory.dart';
910
import 'package:file_testing/file_testing.dart';
11+
import 'package:flutter_tools/src/base/error_handling_io.dart';
1012
import 'package:flutter_tools/src/base/os.dart';
1113
import 'package:flutter_tools/src/base/platform.dart';
1214
import 'package:flutter_tools/src/base/time.dart';
@@ -1752,6 +1754,73 @@ The Flutter Preview device does not support the following plugins from your pubs
17521754
);
17531755
});
17541756
});
1757+
1758+
testUsingContext('exits tool when deleting .plugin_symlinks fails', () async {
1759+
final FakeFlutterProject flutterProject = FakeFlutterProject()
1760+
..directory = globals.fs.currentDirectory.childDirectory('app');
1761+
final FakeFlutterManifest flutterManifest = FakeFlutterManifest();
1762+
final Directory windowsManagedDirectory = flutterProject.directory
1763+
.childDirectory('windows')
1764+
.childDirectory('flutter');
1765+
final FakeWindowsProject windowsProject = FakeWindowsProject()
1766+
..managedDirectory = windowsManagedDirectory
1767+
..cmakeFile = windowsManagedDirectory.parent.childFile('CMakeLists.txt')
1768+
..generatedPluginCmakeFile =
1769+
windowsManagedDirectory.childFile('generated_plugins.mk')
1770+
..pluginSymlinkDirectory = windowsManagedDirectory
1771+
.childDirectory('ephemeral')
1772+
.childDirectory('.plugin_symlinks')
1773+
..exists = true;
1774+
1775+
flutterProject
1776+
..manifest = flutterManifest
1777+
..flutterPluginsFile =
1778+
flutterProject.directory.childFile('.flutter-plugins')
1779+
..flutterPluginsDependenciesFile =
1780+
flutterProject.directory.childFile('.flutter-plugins-dependencies')
1781+
..windows = windowsProject;
1782+
1783+
flutterProject.directory.childFile('.packages').createSync(recursive: true);
1784+
1785+
createPluginSymlinks(
1786+
flutterProject,
1787+
force: true,
1788+
featureFlagsOverride: TestFeatureFlags(isWindowsEnabled: true),
1789+
);
1790+
1791+
expect(
1792+
() => createPluginSymlinks(
1793+
flutterProject,
1794+
force: true,
1795+
featureFlagsOverride: TestFeatureFlags(isWindowsEnabled: true),
1796+
),
1797+
throwsToolExit(
1798+
message: RegExp('Unable to delete file or directory at '
1799+
r'"C:\\app\\windows\\flutter\\ephemeral\\\.plugin_symlinks"')),
1800+
);
1801+
}, overrides: <Type, Generator>{
1802+
FileSystem: () {
1803+
final FileExceptionHandler handle = FileExceptionHandler();
1804+
final ErrorHandlingFileSystem fileSystem = ErrorHandlingFileSystem(
1805+
platform: FakePlatform(),
1806+
delegate: MemoryFileSystem.test(
1807+
style: FileSystemStyle.windows,
1808+
opHandle: handle.opHandle,
1809+
),
1810+
);
1811+
const String symlinkDirectoryPath = r'C:\app\windows\flutter\ephemeral\.plugin_symlinks';
1812+
handle.addError(
1813+
fileSystem.directory(symlinkDirectoryPath),
1814+
FileSystemOp.delete,
1815+
const PathNotFoundException(
1816+
symlinkDirectoryPath,
1817+
OSError('The system cannot find the path specified.', 3),
1818+
),
1819+
);
1820+
return fileSystem;
1821+
},
1822+
ProcessManager: () => FakeProcessManager.empty(),
1823+
});
17551824
}
17561825

17571826
class FakeFlutterManifest extends Fake implements FlutterManifest {

0 commit comments

Comments
 (0)