Skip to content

Commit fdbb6f0

Browse files
authored
[flutter_tools] Avoid unlinking native_assets.json and handle EXDEV (flutter#196134)
Previously, `installCodeAssets` invoked `_copyNativeCodeAssetsForOS` on every `flutter test` run, which listed `build/native_assets/<os>/` and deleted `native_assets.json` right before `_writeNativeAssetsJson` truncated and overwrote that same file in place. On macOS external volumes (for example `/Volumes/...` where endpoint-security or recycle-bin interposers intercept `unlink(2)` and attempt a cross-volume `rename(2)`), unlinking `native_assets.json` failed with `FileSystemException (OS Error: Cross-device link, errno = 18)` (`EXDEV`). In addition, `_handlePosixException` did not map `EXDEV` (`errno = 18`) to a `ToolExit`. This change: 1. Skips deleting `native_assets.json` in `_copyNativeCodeAssetsForOS` when it already exists on disk as a regular file, since `_writeNativeAssetsJson` immediately truncates and overwrites it in place (while still deleting stale bundled native asset artifacts, directories, and symlinks). 2. Handles POSIX `EXDEV` (`errno = 18`) in `_handlePosixException` on Linux and macOS by throwing an actionable `ToolExit` instead of crashing with an unhandled `FileSystemException`. Reproduction notes: Both output pairs below were captured on a Linux host, which cannot reproduce the macOS external-volume behavior directly. The first pair comes from a small Dart script (`repro_exdev.dart`) calling `ErrorHandlingFileSystem.file(...).renameSync(...)` from `/tmp` (`tmpfs`) to an `ext4` directory to trigger a real kernel `EXDEV` (`errno = 18`). The second pair comes from running `BOT=true flutter test --no-pub -r expanded` on a minimal app after making `build/native_assets/linux` read-only (`chmod 0555`) while leaving `native_assets.json` writable (`0644`), so `unlink(2)` fails while in-place truncation succeeds; with a writable `build/native_assets/linux` directory, the unfixed tool passes with the same 3-line test output. Local workspace paths inside the fenced output blocks are preserved verbatim to match exact command output. Previous output: ``` Unhandled exception: FileSystemException: Cannot rename file to '/usr/local/google/home/bkonyi/.gemini/jetski/brain/0a669702-1e2d-46a6-9c37-fdc4d2ca98a9/scratch/repro_196121_dst.tmp', path = '/tmp/repro_196121_src.tmp' (OS Error: Invalid cross-device link, errno = 18) #0 _File.throwIfError (dart:io/file_impl.dart:779:7) #1 _File.renameSync (dart:io/file_impl.dart:396:5) #2 ForwardingFileSystemEntity.renameSync (package:file/src/forwarding/forwarding_file_system_entity.dart:50:49) #3 ErrorHandlingFile.renameSync.<anonymous closure> (package:flutter_tools/src/base/error_handling_io.dart:430:31) #4 _runSync (package:flutter_tools/src/base/error_handling_io.dart:1116:16) #5 ErrorHandlingFile.renameSync (package:flutter_tools/src/base/error_handling_io.dart:429:12) #6 main (file:///usr/local/google/home/bkonyi/.gemini/jetski/brain/0a669702-1e2d-46a6-9c37-fdc4d2ca98a9/scratch/repro_exdev.dart:24:37) #7 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:313:19) #8 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12) ``` Current output: ``` Flutter failed to rename file at "/tmp/repro_196121_src.tmp" to "/usr/local/google/home/bkonyi/.gemini/jetski/brain/0a669702-1e2d-46a6-9c37-fdc4d2ca98a9/scratch/repro_196121_dst.tmp". A file system operation failed across different volumes or mount points. FileSystemException: Cannot rename file to '/usr/local/google/home/bkonyi/.gemini/jetski/brain/0a669702-1e2d-46a6-9c37-fdc4d2ca98a9/scratch/repro_196121_dst.tmp', path = '/tmp/repro_196121_src.tmp' (OS Error: Invalid cross-device link, errno = 18) Ensure that the project, build output, and temporary directories are on the same writable volume, or check external volume and security software settings. ``` Previous output (flutter test overwriting existing native_assets.json): ``` Flutter failed to delete file at "/usr/local/google/home/bkonyi/.gemini/jetski/brain/0a669702-1e2d-46a6-9c37-fdc4 d2ca98a9/scratch/repro_app/build/native_assets/linux/native_assets.json". Please ensure that the SDK and/or project is installed in a location that has read/write permissions for the current user. Try running: sudo chown -R $(whoami) /usr/local/google/home/bkonyi/.gemini/jetski/brain/0a669702-1e2d-46a6-9c37-fdc 4d2ca98a9/scratch/repro_app/build/native_assets/linux/native_assets.json ``` Current output (flutter test overwriting existing native_assets.json): ``` 00:00 +0: loading /usr/local/google/home/bkonyi/.gemini/jetski/brain/0a669702-1e2d-46a6-9c37-fdc4d2ca98a9/scratch/repro_app/test/smoke_test.dart 00:00 +0: smoke test 00:00 +1: All tests passed! ``` Fixes flutter#196121
1 parent e63a332 commit fdbb6f0

4 files changed

Lines changed: 247 additions & 1 deletion

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,7 @@ void _handlePosixException(
13831383
const eperm = 1;
13841384
const enoent = 2;
13851385
const eacces = 13;
1386+
const exdev = 18;
13861387
const enospc = 28;
13871388
const erofs = 30;
13881389
// Catch errors and bail when:
@@ -1392,6 +1393,12 @@ void _handlePosixException(
13921393
'\n$e\n'
13931394
'This can sometimes happen if the file was deleted or moved while the tool was running.'
13941395
' Try running "flutter clean" and try again.',
1396+
exdev =>
1397+
'${message != null && message.isNotEmpty ? '$message.\n' : ''}'
1398+
'A file system operation failed across different volumes or mount points.\n'
1399+
'$e\n'
1400+
'Ensure that the project, build output, and temporary directories are on the same '
1401+
'writable volume, or check external volume and security software settings.',
13951402
enospc =>
13961403
'$message. The target device is full.'
13971404
'\n$e\n'

‎packages/flutter_tools/lib/src/isolated/native_assets/native_assets.dart‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,7 @@ Future<List<File>> installCodeAssets({
527527
codesignIdentity,
528528
flutterTester,
529529
logger: logger,
530+
nativeAssetsFileUri: nativeAssetsFileUri,
530531
processManager: processManager,
531532
);
532533
final manifest = NativeAssetsManifest.fromTargetLocations(assetTargetLocations);
@@ -812,6 +813,7 @@ Future<List<File>> _copyNativeCodeAssetsForOS(
812813
String? codesignIdentity,
813814
bool flutterTester, {
814815
required Logger logger,
816+
required Uri nativeAssetsFileUri,
815817
required ProcessManager processManager,
816818
}) async {
817819
// We only have to copy code assets that are bundled within the app.
@@ -828,7 +830,13 @@ Future<List<File>> _copyNativeCodeAssetsForOS(
828830
if (!targetDir.existsSync()) {
829831
targetDir.createSync(recursive: true);
830832
}
831-
for (final FileSystemEntity entity in await targetDir.list().toList()) {
833+
final String nativeAssetsFilePath = fileSystem.file(nativeAssetsFileUri).path;
834+
for (final FileSystemEntity entity in await targetDir.list(followLinks: false).toList()) {
835+
if (entity is File && fileSystem.path.equals(entity.path, nativeAssetsFilePath)) {
836+
// _writeNativeAssetsJson immediately truncates and overwrites this
837+
// manifest file in place, so avoid unlinking it unnecessarily.
838+
continue;
839+
}
832840
ErrorHandlingFileSystem.deleteIfExists(entity, recursive: true);
833841
}
834842

‎packages/flutter_tools/test/general.shard/base/error_handling_io_test.dart‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ void main() {
481481
const eperm = 1;
482482
const enospc = 28;
483483
const eacces = 13;
484+
const exdev = 18;
484485
const erofs = 30;
485486

486487
late FileExceptionHandler exceptionHandler;
@@ -765,12 +766,69 @@ void main() {
765766
throwsToolExit(message: 'The file or directory could not be found'),
766767
);
767768
});
769+
770+
testWithoutContext('when a cross-device link error occurs (exdev)', () async {
771+
final fileSystem = ErrorHandlingFileSystem(
772+
delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle),
773+
platform: linuxPlatform,
774+
);
775+
final Directory directory = fileSystem.directory('dir')..createSync();
776+
final File file = directory.childFile('file')..createSync();
777+
778+
exceptionHandler
779+
..addError(
780+
file,
781+
FileSystemOp.delete,
782+
FileSystemException(
783+
'Cannot delete file',
784+
file.path,
785+
const OSError('Cross-device link', exdev),
786+
),
787+
)
788+
..addError(
789+
file,
790+
FileSystemOp.copy,
791+
FileSystemException(
792+
'Cannot copy file',
793+
file.path,
794+
const OSError('Cross-device link', exdev),
795+
),
796+
)
797+
..addError(
798+
file,
799+
FileSystemOp.write,
800+
FileSystemException(
801+
'Cannot write file',
802+
file.path,
803+
const OSError('Cross-device link', exdev),
804+
),
805+
);
806+
807+
const expectedCrossDeviceMessage =
808+
'A file system operation failed across different volumes or mount points.';
809+
await expectLater(() => file.delete(), throwsToolExit(message: expectedCrossDeviceMessage));
810+
expect(() => file.deleteSync(), throwsToolExit(message: expectedCrossDeviceMessage));
811+
expect(
812+
() => ErrorHandlingFileSystem.deleteIfExists(file, recursive: true),
813+
throwsToolExit(message: expectedCrossDeviceMessage),
814+
);
815+
await expectLater(
816+
() => file.copy('dir/copied'),
817+
throwsToolExit(message: expectedCrossDeviceMessage),
818+
);
819+
expect(() => file.writeAsStringSync(''), throwsToolExit(message: expectedCrossDeviceMessage));
820+
expect(
821+
() => ErrorHandlingFileSystem.noExitOnFailure(() => file.deleteSync()),
822+
throwsFileSystemException(),
823+
);
824+
});
768825
});
769826

770827
group('throws ToolExit on macOS', () {
771828
const eperm = 1;
772829
const enospc = 28;
773830
const eacces = 13;
831+
const exdev = 18;
774832
const erofs = 30;
775833
late FileExceptionHandler exceptionHandler;
776834

@@ -1053,6 +1111,54 @@ void main() {
10531111
throwsToolExit(message: 'The flutter tool cannot access the file or directory'),
10541112
);
10551113
});
1114+
1115+
testWithoutContext('when a cross-device link error occurs (exdev)', () async {
1116+
final fileSystem = ErrorHandlingFileSystem(
1117+
delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle),
1118+
platform: macOSPlatform,
1119+
);
1120+
final Directory directory = fileSystem.directory('dir')..createSync();
1121+
final File file = directory.childFile('file')..createSync();
1122+
1123+
exceptionHandler
1124+
..addError(
1125+
file,
1126+
FileSystemOp.delete,
1127+
FileSystemException(
1128+
'Cannot delete file',
1129+
file.path,
1130+
const OSError('Cross-device link', exdev),
1131+
),
1132+
)
1133+
..addError(
1134+
file,
1135+
FileSystemOp.write,
1136+
FileSystemException(
1137+
'Cannot write file',
1138+
file.path,
1139+
const OSError('Cross-device link', exdev),
1140+
),
1141+
);
1142+
1143+
const expectedDeleteMessage =
1144+
'Flutter failed to delete file at "dir/file".\n'
1145+
'A file system operation failed across different volumes or mount points.';
1146+
await expectLater(() => file.delete(), throwsToolExit(message: expectedDeleteMessage));
1147+
expect(() => file.deleteSync(), throwsToolExit(message: expectedDeleteMessage));
1148+
expect(
1149+
() => ErrorHandlingFileSystem.deleteIfExists(file, recursive: true),
1150+
throwsToolExit(message: expectedDeleteMessage),
1151+
);
1152+
1153+
const expectedWriteMessage =
1154+
'Flutter failed to write to a file at "dir/file".\n'
1155+
'A file system operation failed across different volumes or mount points.';
1156+
await expectLater(
1157+
() => file.writeAsString(''),
1158+
throwsToolExit(message: expectedWriteMessage),
1159+
);
1160+
expect(() => file.writeAsStringSync(''), throwsToolExit(message: expectedWriteMessage));
1161+
});
10561162
});
10571163

10581164
testWithoutContext('Caches path context correctly', () {

‎packages/flutter_tools/test/general.shard/isolated/native_assets_test.dart‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:file/memory.dart';
88
import 'package:file_testing/file_testing.dart';
99
import 'package:flutter_tools/src/artifacts.dart';
1010
import 'package:flutter_tools/src/base/config.dart';
11+
import 'package:flutter_tools/src/base/error_handling_io.dart';
1112
import 'package:flutter_tools/src/base/file_system.dart';
1213
import 'package:flutter_tools/src/base/logger.dart';
1314
import 'package:flutter_tools/src/base/platform.dart';
@@ -535,6 +536,130 @@ CMAKE_LINKER:FILEPATH=/usr/bin/ld.ldd
535536
},
536537
);
537538

539+
group('installCodeAssets manifest overwrite', () {
540+
const exdev = 18;
541+
542+
Future<List<File>> runInstall({
543+
required FileSystem fileSystem,
544+
required Directory targetDirectory,
545+
Uri? nativeAssetsFileUri,
546+
}) {
547+
return installCodeAssets(
548+
dartHookResult: DartHooksResult.empty(),
549+
environmentDefines: <String, String>{kBuildMode: BuildMode.debug.cliName},
550+
targetPlatform: .tester,
551+
projectUri: fileSystem.directory('/project').uri,
552+
fileSystem: fileSystem,
553+
logger: BufferLogger.test(),
554+
processManager: FakeProcessManager.empty(),
555+
nativeAssetsFileUri:
556+
nativeAssetsFileUri ??
557+
targetDirectory.childFile(InstallCodeAssets.nativeAssetsFilename).uri,
558+
targetUri: targetDirectory.uri,
559+
);
560+
}
561+
562+
testWithoutContext('overwrites existing native_assets.json in place without deleting it '
563+
'while still deleting stale code assets '
564+
'(regression test for https://github.com/flutter/flutter/issues/196121)', () async {
565+
final exceptionHandler = FileExceptionHandler();
566+
final errorHandlingFs = ErrorHandlingFileSystem(
567+
delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle),
568+
platform: FakePlatform(operatingSystem: 'macos'),
569+
);
570+
final Directory targetDirectory = errorHandlingFs.directory(
571+
'/project/build/native_assets/macos',
572+
)..createSync(recursive: true);
573+
final File existingManifest = targetDirectory.childFile(
574+
InstallCodeAssets.nativeAssetsFilename,
575+
)..writeAsStringSync('{"stale": true}');
576+
final File staleDylib = targetDirectory.childFile('libstale.dylib')
577+
..writeAsBytesSync(<int>[1, 2, 3]);
578+
579+
// Simulate macOS external volume failure if installCodeAssets attempts
580+
// to unlink native_assets.json before overwriting it.
581+
exceptionHandler.addError(
582+
existingManifest,
583+
FileSystemOp.delete,
584+
FileSystemException(
585+
'Deletion failed',
586+
existingManifest.path,
587+
const OSError('Cross-device link', exdev),
588+
),
589+
);
590+
591+
await runInstall(
592+
fileSystem: errorHandlingFs,
593+
targetDirectory: targetDirectory,
594+
nativeAssetsFileUri: existingManifest.uri,
595+
);
596+
597+
expect(staleDylib, isNot(exists));
598+
expect(existingManifest, exists);
599+
expect(existingManifest.readAsStringSync(), isNot(contains('"stale"')));
600+
});
601+
602+
testWithoutContext(
603+
'replaces native_assets.json if it is a directory or symlink on disk',
604+
() async {
605+
final FileSystem fs = MemoryFileSystem.test();
606+
final Directory targetDirectory = fs.directory('/project/build/native_assets/macos')
607+
..createSync(recursive: true);
608+
final File manifestFile = targetDirectory.childFile(InstallCodeAssets.nativeAssetsFilename);
609+
610+
// Case 1: native_assets.json is unexpectedly a non-empty directory.
611+
targetDirectory
612+
.childDirectory(InstallCodeAssets.nativeAssetsFilename)
613+
.childFile('unexpected.txt')
614+
.createSync(recursive: true);
615+
616+
await runInstall(fileSystem: fs, targetDirectory: targetDirectory);
617+
expect(fs.typeSync(manifestFile.path, followLinks: false), FileSystemEntityType.file);
618+
619+
// Case 2: native_assets.json is a symlink.
620+
manifestFile.deleteSync();
621+
final File linkTarget = fs.file('/project/other.json')..writeAsStringSync('{}');
622+
targetDirectory
623+
.childLink(InstallCodeAssets.nativeAssetsFilename)
624+
.createSync(linkTarget.path);
625+
626+
await runInstall(fileSystem: fs, targetDirectory: targetDirectory);
627+
expect(fs.typeSync(manifestFile.path, followLinks: false), FileSystemEntityType.file);
628+
expect(linkTarget.readAsStringSync(), '{}');
629+
},
630+
);
631+
632+
testWithoutContext('throws ToolExit when deleting a stale asset fails with EXDEV', () async {
633+
final exceptionHandler = FileExceptionHandler();
634+
final errorHandlingFs = ErrorHandlingFileSystem(
635+
delegate: MemoryFileSystem.test(opHandle: exceptionHandler.opHandle),
636+
platform: FakePlatform(operatingSystem: 'macos'),
637+
);
638+
final Directory targetDirectory = errorHandlingFs.directory(
639+
'/project/build/native_assets/macos',
640+
)..createSync(recursive: true);
641+
final File staleDylib = targetDirectory.childFile('libstale.dylib')
642+
..writeAsBytesSync(<int>[1, 2, 3]);
643+
644+
exceptionHandler.addError(
645+
staleDylib,
646+
FileSystemOp.delete,
647+
FileSystemException(
648+
'Deletion failed',
649+
staleDylib.path,
650+
const OSError('Cross-device link', exdev),
651+
),
652+
);
653+
654+
await expectLater(
655+
() => runInstall(fileSystem: errorHandlingFs, targetDirectory: targetDirectory),
656+
throwsToolExit(
657+
message: 'A file system operation failed across different volumes or mount points.',
658+
),
659+
);
660+
});
661+
});
662+
538663
group('findRunPackageName', () {
539664
testWithoutContext('prefers direct root URI match over manifestAppName', () {
540665
final FileSystem fs = MemoryFileSystem.test();

0 commit comments

Comments
 (0)