Skip to content

Commit 69e68f6

Browse files
authored
Catch any FileSystemException thrown when trying to read the template manifest during flutter create (#145620)
Fixes flutter/flutter#145423
1 parent cc9ac7d commit 69e68f6

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

packages/flutter_tools/lib/src/commands/create_base.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,18 @@ abstract class CreateBase extends FlutterCommand {
669669
'templates',
670670
'template_manifest.json',
671671
);
672+
final String manifestFileContents;
673+
try {
674+
manifestFileContents = globals.fs.file(manifestPath).readAsStringSync();
675+
} on FileSystemException catch (e) {
676+
throwToolExit(
677+
'Unable to read the template manifest at path "$manifestPath".\n'
678+
'Make sure that your user account has sufficient permissions to read this file.\n'
679+
'Exception details: $e',
680+
);
681+
}
672682
final Map<String, Object?> manifest = json.decode(
673-
globals.fs.file(manifestPath).readAsStringSync(),
683+
manifestFileContents,
674684
) as Map<String, Object?>;
675685
return Set<Uri>.from(
676686
(manifest['files']! as List<Object?>).cast<String>().map<Uri>(

packages/flutter_tools/test/commands.shard/permeable/create_test.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:convert';
77
import 'dart:io' as io;
88

99
import 'package:args/command_runner.dart';
10+
import 'package:file/file.dart';
1011
import 'package:file/memory.dart';
1112
import 'package:file_testing/file_testing.dart';
1213
import 'package:flutter_tools/src/android/gradle_utils.dart' show templateAndroidGradlePluginVersion, templateAndroidGradlePluginVersionForModule, templateDefaultGradleVersion;
@@ -3749,6 +3750,40 @@ void main() {
37493750

37503751
expect(rawManifestJson.contains(expectedDescription), isTrue);
37513752
});
3753+
3754+
testUsingContext('flutter create should tool exit if the template manifest cannot be read', () async {
3755+
globals.fs.file(globals.fs.path.join(
3756+
Cache.flutterRoot!,
3757+
'packages',
3758+
'flutter_tools',
3759+
'templates',
3760+
'template_manifest.json',
3761+
)).createSync(recursive: true);
3762+
3763+
final CreateCommand command = CreateCommand();
3764+
final CommandRunner<void> runner = createTestCommandRunner(command);
3765+
3766+
await expectLater(
3767+
runner.run(<String>[
3768+
'create',
3769+
'--no-pub',
3770+
'--template=plugin',
3771+
'--project-name=test',
3772+
projectDir.path,
3773+
]),
3774+
throwsToolExit(message: 'Unable to read the template manifest at path'),
3775+
);
3776+
}, overrides: <Type, Generator>{
3777+
FileSystem: () => MemoryFileSystem.test(
3778+
opHandle: (String context, FileSystemOp operation) {
3779+
if (operation == FileSystemOp.read && context.contains('template_manifest.json')) {
3780+
throw io.PathNotFoundException(
3781+
context, const OSError(), 'Cannot open file');
3782+
}
3783+
},
3784+
),
3785+
ProcessManager: () => fakeProcessManager,
3786+
});
37523787
}
37533788

37543789
Future<void> _createProject(

0 commit comments

Comments
 (0)