Skip to content

Commit 5691baa

Browse files
bkonyisuryaKommana2662
authored andcommitted
[flutter_tools] Pass relative archive paths to pub cache preload (flutter#195077)
In `_DefaultPub._preloadPubCache`, `flutter_tools` previously passed the full path of every `.tar.gz` archive in `$FLUTTER_ROOT/.pub-preload-cache` to a single `dart pub cache preload` command. On Windows, when `FLUTTER_ROOT` is located in a deeply nested directory (~145+ characters), repeating the full archive path for all ~170 preloaded archives causes the command line to exceed Windows' `CreateProcessW` 32,767-character limit and fail with OS error code `206` (`ERROR_FILENAME_EXCED_RANGE`: `The filename or extension is too long`). Run `dart pub cache preload` with `workingDirectory: preloadCacheDir.path` and pass only the archive basenames via `_fileSystem.path.basename` so the command line remains well below the Windows command-line length limit, and skip invoking `dart pub cache preload` if no `.tar.gz` archives are present in `.pub-preload-cache`. ### Previous output ```text Flutter failed to run "C:\Users\User\AppData\Local\Temp\...\bin\cache\dart-sdk\bin\dart pub --suppress-analytics cache preload C:\Users\User\AppData\Local\Temp\...\.pub-preload-cache\...". The filename or extension is too long. ProcessException: The filename or extension is too long Command: C:\Users\User\AppData\Local\Temp\... ``` ### Current output ```text Installed a.tar.gz in cache as a 1.0.0. Installed b.tar.gz in cache as b 1.0.0. ``` Fixes flutter#194570
1 parent 9ef3bb6 commit 5691baa

2 files changed

Lines changed: 73 additions & 6 deletions

File tree

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,22 @@ class _DefaultPub implements Pub {
685685
/// We only want to inform about existing caches on first run of a freshly
686686
/// downloaded Flutter SDK. Therefore it is conditioned on the existence
687687
/// of the .pub-preload-cache dir.
688-
final Iterable<String> cacheFiles = preloadCacheDir
688+
// Pass archive names relative to the preload cache directory. Absolute
689+
// paths can exceed the Windows command line length limit when FLUTTER_ROOT
690+
// is deeply nested. See https://github.com/flutter/flutter/issues/194570.
691+
final List<String> cacheFiles = preloadCacheDir
689692
.listSync()
690-
.map((FileSystemEntity f) => f.path)
691-
.where((String path) => path.endsWith('.tar.gz'));
692-
_processManager.runSync(<String>[..._pubCommand, 'cache', 'preload', ...cacheFiles]);
693+
.map((FileSystemEntity f) => _fileSystem.path.basename(f.path))
694+
.where((String path) => path.endsWith('.tar.gz'))
695+
.toList();
696+
if (cacheFiles.isNotEmpty) {
697+
_processManager.runSync(<String>[
698+
..._pubCommand,
699+
'cache',
700+
'preload',
701+
...cacheFiles,
702+
], workingDirectory: preloadCacheDir.path);
703+
}
693704
_tryDeleteDirectory(preloadCacheDir, _logger);
694705
}
695706
}

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

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,9 +1041,10 @@ exit code: 66
10411041
'--suppress-analytics',
10421042
'cache',
10431043
'preload',
1044-
'.pub-preload-cache/a.tar.gz',
1045-
'.pub-preload-cache/b.tar.gz',
1044+
'a.tar.gz',
1045+
'b.tar.gz',
10461046
],
1047+
workingDirectory: '.pub-preload-cache',
10471048
),
10481049
FakeCommand(
10491050
command: const <String>[
@@ -1084,6 +1085,61 @@ exit code: 66
10841085
expect(preloadCache.existsSync(), false);
10851086
});
10861087

1088+
testWithoutContext(
1089+
'pub cache preload is not invoked when .pub-preload-cache has no archives',
1090+
() async {
1091+
final FileSystem fileSystem = MemoryFileSystem.test();
1092+
final Directory preloadCache = fileSystem.currentDirectory.childDirectory(
1093+
'.pub-preload-cache',
1094+
)..createSync(recursive: true);
1095+
preloadCache.childFile('README.txt').createSync();
1096+
fileSystem.file('bin/cache/flutter.version.json')
1097+
..createSync(recursive: true)
1098+
..writeAsStringSync(_generateFlutterVersionJson('1.0.0'));
1099+
1100+
final processManager = FakeProcessManager.list(<FakeCommand>[
1101+
FakeCommand(
1102+
command: const <String>[
1103+
'bin/cache/dart-sdk/bin/dart',
1104+
'pub',
1105+
'--suppress-analytics',
1106+
'--directory',
1107+
'.',
1108+
'get',
1109+
'--example',
1110+
],
1111+
environment: const <String, String>{
1112+
'FLUTTER_ROOT': '',
1113+
'PUB_ENVIRONMENT': 'flutter_cli:flutter_tests',
1114+
},
1115+
onRun: (_) => writePackageConfigFiles(
1116+
directory: fileSystem.currentDirectory,
1117+
mainLibName: 'my_app',
1118+
),
1119+
),
1120+
]);
1121+
1122+
final Platform platform = FakePlatform(environment: <String, String>{'HOME': '/global'});
1123+
final logger = BufferLogger.test();
1124+
final pub = Pub.test(
1125+
platform: platform,
1126+
fileSystem: fileSystem,
1127+
logger: logger,
1128+
processManager: processManager,
1129+
botDetector: const FakeBotDetector(false),
1130+
stdio: FakeStdio(),
1131+
);
1132+
1133+
await pub.get(
1134+
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
1135+
context: PubContext.flutterTests,
1136+
);
1137+
1138+
expect(processManager, hasNoRemainingExpectations);
1139+
expect(preloadCache.existsSync(), false);
1140+
},
1141+
);
1142+
10871143
testUsingContext('pub cache in environment is used', () async {
10881144
final FileSystem fileSystem = MemoryFileSystem.test();
10891145
fileSystem.directory('custom/pub-cache/path').createSync(recursive: true);

0 commit comments

Comments
 (0)