Skip to content

Commit 27e7f1b

Browse files
committed
fix: stop flutter create --platforms from dropping existing platforms in .metadata
Fixes an issue where running `flutter create --platforms=<new-platform> .` overwrites the existing platform list in `.metadata` instead of appending to it. Fixes flutter#191567
1 parent c948881 commit 27e7f1b

3 files changed

Lines changed: 66 additions & 2 deletions

File tree

‎.vscode/settings.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"githubPullRequests.ignoredPullRequestBranches": [
66
"master"
77
],
8-
"files.trimTrailingWhitespace": true
8+
"files.trimTrailingWhitespace": true,
9+
"dart.showTodos": false
910
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,12 +534,20 @@ mixin CreateBase on FlutterCommand {
534534
final File metadataFile = globals.fs.file(
535535
globals.fs.path.join(projectDir.absolute.path, '.metadata'),
536536
);
537+
// Seed the migrate config with the platforms already tracked in an
538+
// existing .metadata file. Re-running `flutter create --platforms` must
539+
// add the newly requested platforms without dropping the previously
540+
// added ones, and without overwriting their recorded revisions.
541+
// See https://github.com/flutter/flutter/issues/191567.
542+
final MigrateConfig migrateConfig = metadataFile.existsSync()
543+
? FlutterProjectMetadata(metadataFile, globals.logger).migrateConfig
544+
: MigrateConfig();
537545
final metadata = FlutterProjectMetadata.explicit(
538546
file: metadataFile,
539547
versionRevision: globals.flutterVersion.frameworkRevision,
540548
versionChannel: globals.flutterVersion.getBranchName(), // may contain PII
541549
projectType: projectType,
542-
migrateConfig: MigrateConfig(),
550+
migrateConfig: migrateConfig,
543551
logger: globals.logger,
544552
);
545553
metadata.populate(

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,6 +1615,61 @@ void main() {
16151615
},
16161616
);
16171617

1618+
testUsingContext(
1619+
'flutter create --platforms appends platforms to .metadata without dropping existing ones',
1620+
() async {
1621+
// The revisions written to .metadata must look like git hashes; a
1622+
// purely numeric revision would be parsed back as a number by YAML and
1623+
// rejected by the migrate config validation.
1624+
fakeFlutterVersion = FakeFlutterVersion(
1625+
frameworkRevision: 'abcdef1234567890abcdef1234567890abcdef12',
1626+
branch: frameworkChannel,
1627+
);
1628+
final Directory tempDir = globals.fs.systemTempDirectory.createTempSync(
1629+
'flutter_create_append_platforms_',
1630+
);
1631+
final Directory projectDir = tempDir.childDirectory('myapp');
1632+
1633+
try {
1634+
// Create a project that only supports the android platform.
1635+
final command = CreateCommand();
1636+
final CommandRunner<void> runner = createTestCommandRunner(command);
1637+
await runner.run(<String>['create', '--no-pub', '--platforms=android', projectDir.path]);
1638+
1639+
// Append the ios platform to the existing project.
1640+
final appendCommand = CreateCommand();
1641+
final CommandRunner<void> appendRunner = createTestCommandRunner(appendCommand);
1642+
await appendRunner.run(<String>['create', '--no-pub', '--platforms=ios', projectDir.path]);
1643+
1644+
// Parse the migration section of the .metadata file with loadYaml,
1645+
// the same parser the tool uses when reading the file back.
1646+
final File metadataFile = projectDir.childFile('.metadata');
1647+
expect(metadataFile, exists);
1648+
1649+
final yamlMap = loadYaml(metadataFile.readAsStringSync()) as YamlMap;
1650+
final migration = yamlMap['migration'] as YamlMap;
1651+
final platforms = migration['platforms'] as YamlList;
1652+
1653+
// Each entry in the platforms list is a map that tracks the platform
1654+
// name along with the revisions it was created at; extract the names.
1655+
final actualPlatforms = <String>[
1656+
for (final YamlMap platform in platforms.whereType<YamlMap>())
1657+
platform['platform'] as String,
1658+
];
1659+
1660+
// The root platform is always tracked, and neither the previously
1661+
// added android platform nor the newly added ios platform may be lost.
1662+
expect(actualPlatforms, unorderedEquals(<String>['root', 'android', 'ios']));
1663+
} finally {
1664+
tryToDelete(tempDir);
1665+
}
1666+
},
1667+
overrides: {
1668+
FlutterVersion: () => fakeFlutterVersion,
1669+
Platform: _kNoColorTerminalPlatform,
1670+
},
1671+
);
1672+
16181673
testUsingContext('Correct info.plist key-value pairs for project.', () async {
16191674
final command = CreateCommand();
16201675
final CommandRunner<void> runner = createTestCommandRunner(command);

0 commit comments

Comments
 (0)