Skip to content

Commit 00bd762

Browse files
authored
[flutter_tools] Safely handle broken symlinks and existing files during plugin symlink creation (#191496)
## Description Fixes a crash during plugin symlink creation on Windows/Linux desktop builds (`FileSystemException: Creation failed, OS Error: A file cannot be created if it already exists., errno = 183` / `errno = 17`). ### Cause Previously, `_createPlatformPluginSymlinks` checked `if (link.existsSync()) continue;` before calling `link.createSync(path)`. 1. If a non-link entity (such as a regular directory or file from manual extraction, git checkout, or previous builds) occupied the symlink path, `Link.existsSync()` returned `false`. The tool then called `link.createSync(path)`, crashing with `ERROR_ALREADY_EXISTS` (`errno = 183`) on Windows or `EEXIST` (`errno = 17`) on POSIX. 2. `Link.existsSync()` did not check if an existing symlink pointed to the expected plugin target path. If a plugin's path changed (e.g. a version upgrade in pub cache), existing symlinks pointing to outdated paths were skipped. ### Fix - Uses `typeSync(link.path, followLinks: false)` to inspect disk state type-agnostically before symlink creation. - Canonicalizes and compares `link.targetSync()` with `path`. If matching, creation is skipped. - Deletes stale or mismatched links via `ErrorHandlingFileSystem.deleteIfExists(link)`, and removes conflicting non-link entries before calling `link.createSync(path)`. ## Related Issues * Fixes #90028 * Related to #131978 * Related to #74227 ## Tests - `packages/flutter_tools/test/general.shard/plugins_test.dart`: - `createPluginSymlinks repairs broken symlinks without failing` - `createPluginSymlinks replaces existing files with symlinks without failing`
1 parent 1d790af commit 00bd762

2 files changed

Lines changed: 85 additions & 2 deletions

File tree

‎packages/flutter_tools/lib/src/flutter_plugins.dart‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,8 +1248,26 @@ void _createPlatformPluginSymlinks(
12481248
final name = pluginInfo[_kFlutterPluginsNameKey]! as String;
12491249
final path = pluginInfo[_kFlutterPluginsPathKey]! as String;
12501250
final Link link = symlinkDirectory.childLink(name);
1251-
if (link.existsSync()) {
1252-
continue;
1251+
// Inspect the entity on disk without following links. Link.existsSync()
1252+
// only returns true if the entity is specifically a link; if a conflicting
1253+
// non-link file or directory occupies link.path, or if an existing link
1254+
// points to an outdated target, it must be cleaned up before creating the
1255+
// new link to avoid FileSystemException collisions (such as
1256+
// ERROR_ALREADY_EXISTS on Windows or EEXIST on POSIX).
1257+
final FileSystemEntityType entityType = link.fileSystem.typeSync(link.path, followLinks: false);
1258+
if (entityType == FileSystemEntityType.link) {
1259+
try {
1260+
final String target = link.targetSync();
1261+
if (link.fileSystem.path.canonicalize(target) == link.fileSystem.path.canonicalize(path) &&
1262+
link.existsSync()) {
1263+
continue;
1264+
}
1265+
} on FileSystemException {
1266+
// Fall through to delete and recreate if resolving target throws.
1267+
}
1268+
ErrorHandlingFileSystem.deleteIfExists(link);
1269+
} else if (entityType != FileSystemEntityType.notFound) {
1270+
ErrorHandlingFileSystem.deleteIfExists(link, recursive: true);
12531271
}
12541272
try {
12551273
link.createSync(path);

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,71 @@ flutter:
20092009
FeatureFlags: () => featureFlags,
20102010
},
20112011
);
2012+
2013+
testUsingContext(
2014+
'createPluginSymlinks repairs broken symlinks without failing',
2015+
() async {
2016+
linuxProject.exists = true;
2017+
windowsProject.exists = true;
2018+
final Directory pluginDir = createFakePlugin(fs);
2019+
await refreshPluginsList(flutterProject);
2020+
2021+
final links = <Link>[
2022+
linuxProject.pluginSymlinkDirectory.childLink('some_plugin'),
2023+
windowsProject.pluginSymlinkDirectory.childLink('some_plugin'),
2024+
];
2025+
for (final link in links) {
2026+
link.deleteSync();
2027+
link.createSync('/non_existent_target_path');
2028+
}
2029+
createPluginSymlinks(flutterProject);
2030+
2031+
for (final link in links) {
2032+
expect(link, exists);
2033+
expect(fs.path.normalize(link.targetSync()), fs.path.normalize(pluginDir.path));
2034+
}
2035+
},
2036+
overrides: <Type, Generator>{
2037+
FileSystem: () => fs,
2038+
ProcessManager: () => FakeProcessManager.any(),
2039+
FeatureFlags: () => featureFlags,
2040+
},
2041+
);
2042+
2043+
testUsingContext(
2044+
'createPluginSymlinks replaces existing files with symlinks without failing',
2045+
() async {
2046+
linuxProject.exists = true;
2047+
windowsProject.exists = true;
2048+
final Directory pluginDir = createFakePlugin(fs);
2049+
await refreshPluginsList(flutterProject);
2050+
2051+
final files = <File>[
2052+
linuxProject.pluginSymlinkDirectory.childFile('some_plugin'),
2053+
windowsProject.pluginSymlinkDirectory.childFile('some_plugin'),
2054+
];
2055+
for (final file in files) {
2056+
ErrorHandlingFileSystem.deleteIfExists(file, recursive: true);
2057+
file.createSync(recursive: true);
2058+
file.writeAsStringSync('stale content');
2059+
}
2060+
createPluginSymlinks(flutterProject);
2061+
2062+
final links = <Link>[
2063+
linuxProject.pluginSymlinkDirectory.childLink('some_plugin'),
2064+
windowsProject.pluginSymlinkDirectory.childLink('some_plugin'),
2065+
];
2066+
for (final link in links) {
2067+
expect(link, exists);
2068+
expect(fs.path.normalize(link.targetSync()), fs.path.normalize(pluginDir.path));
2069+
}
2070+
},
2071+
overrides: <Type, Generator>{
2072+
FileSystem: () => fs,
2073+
ProcessManager: () => FakeProcessManager.any(),
2074+
FeatureFlags: () => featureFlags,
2075+
},
2076+
);
20122077
});
20132078

20142079
group('pubspec', () {

0 commit comments

Comments
 (0)