Skip to content

Commit 8dc0ba1

Browse files
fix: stop flutter create --platforms from dropping existing platfor… (flutter#191573)
…ms 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 <!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> *Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent e707ecc commit 8dc0ba1

3 files changed

Lines changed: 225 additions & 3 deletions

File tree

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,31 @@ mixin CreateBase on FlutterCommand {
546546
final File metadataFile = globals.fs.file(
547547
globals.fs.path.join(projectDir.absolute.path, '.metadata'),
548548
);
549+
// Seed the migrate config with the platforms already tracked in an
550+
// existing .metadata file. Re-running `flutter create --platforms` must
551+
// add the newly requested platforms without dropping the previously
552+
// added ones, and without overwriting their recorded revisions.
553+
// See https://github.com/flutter/flutter/issues/191567.
554+
555+
var migrateConfig = MigrateConfig();
556+
if (metadataFile.existsSync()) {
557+
try {
558+
migrateConfig = FlutterProjectMetadata(
559+
metadataFile,
560+
globals.logger,
561+
extensionTemplateManager: null,
562+
).migrateConfig;
563+
} on Exception catch (error) {
564+
globals.logger.printTrace('Failed to parse existing .metadata file: $error');
565+
}
566+
}
567+
549568
final metadata = FlutterProjectMetadata.explicit(
550569
file: metadataFile,
551570
versionRevision: globals.flutterVersion.frameworkRevision,
552571
versionChannel: globals.flutterVersion.getBranchName(), // may contain PII
553572
projectType: projectType,
554-
migrateConfig: MigrateConfig(),
573+
migrateConfig: migrateConfig,
555574
logger: globals.logger,
556575
extensionTemplateManager: null,
557576
);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ class MigrateConfig {
387387

388388
/// Parses the project for all supported platforms and populates the [MigrateConfig]
389389
/// to reflect the project.
390+
///
391+
/// Platforms listed in [platforms] are the ones (re)generated by the current
392+
/// operation. When a platform is already tracked and [create] is true, its
393+
/// entry is overwritten with the current revisions instead of keeping stale
394+
/// ones or adding a duplicate entry.
390395
void populate({
391396
List<SupportedPlatform>? platforms,
392397
required Directory projectDirectory,
@@ -401,7 +406,16 @@ class MigrateConfig {
401406

402407
for (final SupportedPlatform platform in platforms) {
403408
if (platformConfigs.containsKey(platform)) {
404-
if (update) {
409+
if (create) {
410+
// The platform is being (re)created by this run: overwrite its
411+
// entry so the recorded revisions reflect this run. Creating the
412+
// same platform twice must not produce a duplicate entry.
413+
platformConfigs[platform] = MigratePlatformConfig(
414+
platform: platform,
415+
createRevision: createRevision,
416+
baseRevision: currentRevision,
417+
);
418+
} else if (update) {
405419
platformConfigs[platform]!.baseRevision = currentRevision;
406420
}
407421
} else {

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

Lines changed: 190 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import 'package:flutter_tools/src/commands/create.dart';
2929
import 'package:flutter_tools/src/commands/create_base.dart';
3030
import 'package:flutter_tools/src/dart/pub.dart';
3131
import 'package:flutter_tools/src/features.dart';
32-
import 'package:flutter_tools/src/flutter_project_metadata.dart' show FlutterTemplateType;
32+
import 'package:flutter_tools/src/flutter_project_metadata.dart';
3333
import 'package:flutter_tools/src/globals.dart' as globals;
3434
import 'package:flutter_tools/src/project.dart';
3535
import 'package:flutter_tools/src/version.dart';
@@ -67,6 +67,24 @@ FakePlatform _kNoColorTerminalMacOSPlatform() => FakePlatform.fromPlatform(const
6767
..stdoutSupportsAnsi = false
6868
..operatingSystem = 'macos';
6969

70+
/// A [FakeFlutterVersion] whose [FakeFlutterVersion.frameworkRevision] can be
71+
/// changed between `flutter create` runs, to simulate re-creating a platform
72+
/// with a newer Flutter SDK revision.
73+
class _MutableFakeFlutterVersion extends FakeFlutterVersion {
74+
_MutableFakeFlutterVersion(String revision, String branch)
75+
: _frameworkRevision = revision,
76+
super(frameworkRevision: revision, branch: branch);
77+
78+
String _frameworkRevision;
79+
80+
@override
81+
String get frameworkRevision => _frameworkRevision;
82+
83+
set frameworkRevision(String value) {
84+
_frameworkRevision = value;
85+
}
86+
}
87+
7088
final Map<Type, FakePlatform Function()> noColorTerminalOverride = {
7189
Platform: _kNoColorTerminalPlatform,
7290
};
@@ -979,6 +997,100 @@ void main() {
979997
expect(actualContents.contains('useAndroidX'), true);
980998
});
981999

1000+
testUsingContext('flutter create --platforms appends platforms to existing .metadata', () async {
1001+
// The revisions written to .metadata must look like git hashes; a purely
1002+
// numeric revision would be parsed back as a number by YAML and rejected
1003+
// by the migrate config validation.
1004+
final mutableFlutterVersion = _MutableFakeFlutterVersion(
1005+
'abcdef1234567890abcdef1234567890abcdef12',
1006+
frameworkChannel,
1007+
);
1008+
fakeFlutterVersion = mutableFlutterVersion;
1009+
1010+
final command = CreateCommand();
1011+
final CommandRunner<void> runner = createTestCommandRunner(command);
1012+
1013+
// Create the project with only the android platform enabled.
1014+
await runner.run(<String>['create', '--no-pub', '--platforms=android', projectDir.path]);
1015+
1016+
final File metadataFile = projectDir.childFile('.metadata');
1017+
expect(metadataFile.existsSync(), isTrue);
1018+
1019+
// .metadata is a YAML file, so read it back through FlutterProjectMetadata.
1020+
// Besides the requested platforms it always tracks a 'root' entry that
1021+
// records the revisions for the project itself.
1022+
final initialMetadata = FlutterProjectMetadata(
1023+
metadataFile,
1024+
globals.logger,
1025+
extensionTemplateManager: null,
1026+
);
1027+
const initialRevision = 'abcdef1234567890abcdef1234567890abcdef12';
1028+
expect(
1029+
initialMetadata.migrateConfig.platformConfigs.keys,
1030+
unorderedEquals(<SupportedPlatform>[SupportedPlatform.root, SupportedPlatform.android]),
1031+
);
1032+
expect(
1033+
initialMetadata.migrateConfig.platformConfigs[SupportedPlatform.android]!.createRevision,
1034+
initialRevision,
1035+
);
1036+
expect(
1037+
initialMetadata.migrateConfig.platformConfigs[SupportedPlatform.android]!.baseRevision,
1038+
initialRevision,
1039+
);
1040+
1041+
// Append the ios platform to the existing project, simulating a later
1042+
// run with a newer Flutter SDK revision. The platforms already recorded
1043+
// in the existing .metadata (android) must be preserved instead of being
1044+
// dropped.
1045+
// See https://github.com/flutter/flutter/issues/191567.
1046+
mutableFlutterVersion.frameworkRevision = 'fedcba9876543210fedcba9876543210fedcba98';
1047+
await runner.run(<String>['create', '--no-pub', '--platforms=ios', projectDir.path]);
1048+
1049+
final updatedMetadata = FlutterProjectMetadata(
1050+
metadataFile,
1051+
globals.logger,
1052+
extensionTemplateManager: null,
1053+
);
1054+
expect(
1055+
updatedMetadata.migrateConfig.platformConfigs.keys,
1056+
unorderedEquals(<SupportedPlatform>[
1057+
SupportedPlatform.root,
1058+
SupportedPlatform.android,
1059+
SupportedPlatform.ios,
1060+
]),
1061+
);
1062+
// Appending a new platform must not overwrite the revisions recorded for
1063+
// platforms that are not part of this run: the android entry keeps the
1064+
// revisions of the run that created it.
1065+
expect(
1066+
updatedMetadata.migrateConfig.platformConfigs[SupportedPlatform.android]!.createRevision,
1067+
initialRevision,
1068+
);
1069+
expect(
1070+
updatedMetadata.migrateConfig.platformConfigs[SupportedPlatform.android]!.baseRevision,
1071+
initialRevision,
1072+
);
1073+
// The root platform is part of every create run, so its entry reflects
1074+
// the most recent run, as does the newly added ios entry.
1075+
const latestRevision = 'fedcba9876543210fedcba9876543210fedcba98';
1076+
expect(
1077+
updatedMetadata.migrateConfig.platformConfigs[SupportedPlatform.root]!.createRevision,
1078+
latestRevision,
1079+
);
1080+
expect(
1081+
updatedMetadata.migrateConfig.platformConfigs[SupportedPlatform.root]!.baseRevision,
1082+
latestRevision,
1083+
);
1084+
expect(
1085+
updatedMetadata.migrateConfig.platformConfigs[SupportedPlatform.ios]!.createRevision,
1086+
latestRevision,
1087+
);
1088+
expect(
1089+
updatedMetadata.migrateConfig.platformConfigs[SupportedPlatform.ios]!.baseRevision,
1090+
latestRevision,
1091+
);
1092+
}, overrides: {FlutterVersion: () => fakeFlutterVersion});
1093+
9821094
testUsingContext('androidx is used by default in a module project', () async {
9831095
final command = CreateCommand();
9841096
final CommandRunner<void> runner = createTestCommandRunner(command);
@@ -1615,6 +1727,83 @@ void main() {
16151727
},
16161728
);
16171729

1730+
testUsingContext(
1731+
'flutter create --platforms overwrites the .metadata entry when the same platform is created twice',
1732+
() async {
1733+
// The revisions written to .metadata must look like git hashes; a
1734+
// purely numeric revision would be parsed back as a number by YAML and
1735+
// rejected by the migrate config validation.
1736+
final mutableFlutterVersion = _MutableFakeFlutterVersion(
1737+
'abcdef1234567890abcdef1234567890abcdef12',
1738+
frameworkChannel,
1739+
);
1740+
fakeFlutterVersion = mutableFlutterVersion;
1741+
final Directory tempDir = globals.fs.systemTempDirectory.createTempSync(
1742+
'flutter_create_overwrite_platform_',
1743+
);
1744+
final Directory projectDir = tempDir.childDirectory('myapp');
1745+
1746+
try {
1747+
// Create a project that supports the android platform.
1748+
final command = CreateCommand();
1749+
final CommandRunner<void> runner = createTestCommandRunner(command);
1750+
await runner.run(<String>['create', '--no-pub', '--platforms=android', projectDir.path]);
1751+
1752+
// Re-create the same android platform, simulating a later run with a
1753+
// newer Flutter SDK revision.
1754+
mutableFlutterVersion.frameworkRevision = 'fedcba9876543210fedcba9876543210fedcba98';
1755+
final repeatCommand = CreateCommand();
1756+
final CommandRunner<void> repeatRunner = createTestCommandRunner(repeatCommand);
1757+
await repeatRunner.run(<String>[
1758+
'create',
1759+
'--no-pub',
1760+
'--platforms=android',
1761+
projectDir.path,
1762+
]);
1763+
1764+
// Parse the migration section of the .metadata file with loadYaml,
1765+
// the same parser the tool uses when reading the file back.
1766+
final File metadataFile = projectDir.childFile('.metadata');
1767+
expect(metadataFile, exists);
1768+
1769+
final yamlMap = loadYaml(metadataFile.readAsStringSync()) as YamlMap;
1770+
final migration = yamlMap['migration'] as YamlMap;
1771+
final platforms = migration['platforms'] as YamlList;
1772+
final List<YamlMap> platformEntries = platforms.whereType<YamlMap>().toList();
1773+
1774+
// Each entry in the platforms list is a map that tracks the platform
1775+
// name along with the revisions it was created at; extract the names.
1776+
final actualPlatforms = <String>[
1777+
for (final YamlMap platform in platformEntries) platform['platform'] as String,
1778+
];
1779+
1780+
// Creating the same platform twice must not add a duplicate entry:
1781+
// the root platform and the android platform each appear exactly once.
1782+
expect(actualPlatforms, unorderedEquals(<String>['root', 'android']));
1783+
1784+
// The existing android entry is overwritten with the revisions of the
1785+
// most recent create run instead of keeping the old ones.
1786+
const latestRevision = 'fedcba9876543210fedcba9876543210fedcba98';
1787+
final YamlMap androidEntry = platformEntries.singleWhere(
1788+
(YamlMap platform) => platform['platform'] == 'android',
1789+
);
1790+
expect(androidEntry['create_revision'], latestRevision);
1791+
expect(androidEntry['base_revision'], latestRevision);
1792+
1793+
// The root platform is always part of every create run, so its entry
1794+
// is overwritten the same way.
1795+
final YamlMap rootEntry = platformEntries.singleWhere(
1796+
(YamlMap platform) => platform['platform'] == 'root',
1797+
);
1798+
expect(rootEntry['create_revision'], latestRevision);
1799+
expect(rootEntry['base_revision'], latestRevision);
1800+
} finally {
1801+
tryToDelete(tempDir);
1802+
}
1803+
},
1804+
overrides: {FlutterVersion: () => fakeFlutterVersion, Platform: _kNoColorTerminalPlatform},
1805+
);
1806+
16181807
testUsingContext('Correct info.plist key-value pairs for project.', () async {
16191808
final command = CreateCommand();
16201809
final CommandRunner<void> runner = createTestCommandRunner(command);

0 commit comments

Comments
 (0)