Skip to content

Commit 30692ee

Browse files
authored
make --dart-define override redundant values in --dart-define-from-file (flutter#131088)
Fixes flutter#130604
1 parent b238134 commit 30692ee

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

packages/flutter_tools/lib/src/runner/flutter_command.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,8 @@ abstract class FlutterCommand extends Command<void> {
646646
help:
647647
'The path of a .json or .env file containing key-value pairs that will be available as environment variables.\n'
648648
'These can be accessed using the String.fromEnvironment, bool.fromEnvironment, and int.fromEnvironment constructors.\n'
649-
'Multiple defines can be passed by repeating "--${FlutterOptions.kDartDefineFromFileOption}" multiple times.',
649+
'Multiple defines can be passed by repeating "--${FlutterOptions.kDartDefineFromFileOption}" multiple times.\n'
650+
'Entries from "--${FlutterOptions.kDartDefinesOption}" with identical keys take precedence over entries from these files.',
650651
valueHelp: 'use-define-config.json|.env',
651652
splitCommas: false,
652653
);
@@ -1351,14 +1352,14 @@ abstract class FlutterCommand extends Command<void> {
13511352
List<String> extractDartDefines({required Map<String, Object?> defineConfigJsonMap}) {
13521353
final List<String> dartDefines = <String>[];
13531354

1354-
if (argParser.options.containsKey(FlutterOptions.kDartDefinesOption)) {
1355-
dartDefines.addAll(stringsArg(FlutterOptions.kDartDefinesOption));
1356-
}
1357-
13581355
defineConfigJsonMap.forEach((String key, Object? value) {
13591356
dartDefines.add('$key=$value');
13601357
});
13611358

1359+
if (argParser.options.containsKey(FlutterOptions.kDartDefinesOption)) {
1360+
dartDefines.addAll(stringsArg(FlutterOptions.kDartDefinesOption));
1361+
}
1362+
13621363
return dartDefines;
13631364
}
13641365

@@ -1372,9 +1373,8 @@ abstract class FlutterCommand extends Command<void> {
13721373

13731374
for (final String path in configFilePaths) {
13741375
if (!globals.fs.isFileSync(path)) {
1375-
throwToolExit('Json config define file "--${FlutterOptions
1376-
.kDartDefineFromFileOption}=$path" is not a file, '
1377-
'please fix first!');
1376+
throwToolExit('Did not find the file passed to "--${FlutterOptions
1377+
.kDartDefineFromFileOption}". Path: $path');
13781378
}
13791379

13801380
final String configRaw = globals.fs.file(path).readAsStringSync();

packages/flutter_tools/test/commands.shard/hermetic/assemble_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ void main() {
302302
'debug_macos_bundle_flutter_assets',
303303
'--dart-define=k=v',
304304
'--dart-define-from-file=config']),
305-
throwsToolExit(message: 'Json config define file "--dart-define-from-file=config" is not a file, please fix first!'));
305+
throwsToolExit(message: 'Did not find the file passed to "--dart-define-from-file". Path: config'));
306306
}, overrides: <Type, Generator>{
307307
Cache: () => Cache.test(processManager: FakeProcessManager.any()),
308308
FileSystem: () => MemoryFileSystem.test(),

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,42 @@ void main() {
559559
ProcessManager: () => FakeProcessManager.any(),
560560
});
561561

562+
testUsingContext('values from --dart-define supersede values from --dart-define-from-file', () async {
563+
globals.fs
564+
.file(globals.fs.path.join('lib', 'main.dart'))
565+
.createSync(recursive: true);
566+
globals.fs.file('pubspec.yaml').createSync();
567+
globals.fs.file('.packages').createSync();
568+
globals.fs.file('.env').writeAsStringSync('''
569+
MY_VALUE=VALUE_FROM_ENV_FILE
570+
''');
571+
final CommandRunner<void> runner =
572+
createTestCommandRunner(BuildBundleCommand(
573+
logger: BufferLogger.test(),
574+
));
575+
576+
await runner.run(<String>[
577+
'bundle',
578+
'--no-pub',
579+
'--dart-define=MY_VALUE=VALUE_FROM_COMMAND',
580+
'--dart-define-from-file=.env',
581+
]);
582+
583+
}, overrides: <Type, Generator>{
584+
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true),
585+
(Target target, Environment environment) {
586+
expect(
587+
_decodeDartDefines(environment),
588+
containsAllInOrder(const <String>[
589+
'MY_VALUE=VALUE_FROM_ENV_FILE',
590+
'MY_VALUE=VALUE_FROM_COMMAND',
591+
]),
592+
);
593+
}),
594+
FileSystem: fsFactory,
595+
ProcessManager: () => FakeProcessManager.any(),
596+
});
597+
562598
testUsingContext('--dart-define-from-file correctly parses a valid env file', () async {
563599
globals.fs
564600
.file(globals.fs.path.join('lib', 'main.dart'))
@@ -763,7 +799,7 @@ void main() {
763799
'bundle',
764800
'--no-pub',
765801
'--dart-define-from-file=config',
766-
]), throwsToolExit(message: 'Json config define file "--dart-define-from-file=config" is not a file, please fix first!'));
802+
]), throwsToolExit(message: 'Did not find the file passed to "--dart-define-from-file". Path: config'));
767803
}, overrides: <Type, Generator>{
768804
FileSystem: fsFactory,
769805
BuildSystem: () => TestBuildSystem.all(BuildResult(success: true)),

0 commit comments

Comments
 (0)