Skip to content

Commit e0d3d54

Browse files
committed
[ Tool ] Correctly select entrypoint target for web build from positional argument list
`BuildWebCommand` was accessing the value of `target` directly from the argument parser rather than using the `targetFile` getter defined on `FlutterCommand` which handles file paths provided in the positional arugment list. Fixes #136830
1 parent 9870afd commit e0d3d54

2 files changed

Lines changed: 106 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ class BuildWebCommand extends BuildSubCommand {
208208
];
209209
}
210210

211-
final String target = stringArg('target')!;
212211
final BuildInfo buildInfo = await getBuildInfo();
213212
final String? baseHref = stringArg('base-href');
214213
if (baseHref != null && !(baseHref.startsWith('/') && baseHref.endsWith('/'))) {
@@ -246,7 +245,7 @@ class BuildWebCommand extends BuildSubCommand {
246245
);
247246
await webBuilder.buildWeb(
248247
project,
249-
target,
248+
targetFile,
250249
buildInfo,
251250
ServiceWorkerStrategy.fromCliName(stringArg('pwa-strategy')),
252251
compilerConfigs: compilerConfigs,

‎packages/flutter_tools/test/commands.shard/hermetic/build_web_test.dart‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ void main() {
4343
writePackageConfigFile(mainLibName: 'foo', directory: fileSystem.currentDirectory);
4444
fileSystem.file(fileSystem.path.join('web', 'index.html')).createSync(recursive: true);
4545
fileSystem.file(fileSystem.path.join('lib', 'main.dart')).createSync(recursive: true);
46+
fileSystem.file(fileSystem.path.join('lib', 'a.dart')).createSync(recursive: true);
4647
logger = BufferLogger.test();
4748
processManager = FakeProcessManager.any();
4849
});
@@ -155,6 +156,110 @@ void main() {
155156
},
156157
);
157158

159+
testUsingContext(
160+
'Infers target entrypoint correctly from --target',
161+
() async {
162+
// Regression test for https://github.com/flutter/flutter/issues/136830.
163+
final BuildCommand buildCommand = BuildCommand(
164+
androidSdk: FakeAndroidSdk(),
165+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
166+
fileSystem: fileSystem,
167+
logger: logger,
168+
osUtils: FakeOperatingSystemUtils(),
169+
);
170+
final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
171+
setupFileSystemForEndToEndTest(fileSystem);
172+
await runner.run(<String>[
173+
'build',
174+
'web',
175+
'--no-pub',
176+
'--no-web-resources-cdn',
177+
'--target=lib/a.dart',
178+
]);
179+
180+
final Directory buildDir = fileSystem.directory(fileSystem.path.join('build', 'web'));
181+
expect(buildDir.existsSync(), true);
182+
expect(testLogger.statusText, contains('Compiling lib/a.dart for the Web...'));
183+
expect(testLogger.statusText, contains('✓ Built ${buildDir.path}'));
184+
},
185+
overrides: <Type, Generator>{
186+
Platform: () => fakePlatform,
187+
FileSystem: () => fileSystem,
188+
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
189+
ProcessManager: () => processManager,
190+
BuildSystem:
191+
() => TestBuildSystem.all(BuildResult(success: true), (
192+
Target target,
193+
Environment environment,
194+
) {
195+
expect(environment.defines, <String, String>{
196+
'TargetFile': 'lib/a.dart',
197+
'HasWebPlugins': 'true',
198+
'ServiceWorkerStrategy': 'offline-first',
199+
'BuildMode': 'release',
200+
'DartDefines':
201+
'RkxVVFRFUl9WRVJTSU9OPTAuMC4w,RkxVVFRFUl9DSEFOTkVMPW1hc3Rlcg==,RkxVVFRFUl9HSVRfVVJMPWh0dHBzOi8vZ2l0aHViLmNvbS9mbHV0dGVyL2ZsdXR0ZXIuZ2l0,RkxVVFRFUl9GUkFNRVdPUktfUkVWSVNJT049MTExMTE=,RkxVVFRFUl9FTkdJTkVfUkVWSVNJT049YWJjZGU=,RkxVVFRFUl9EQVJUX1ZFUlNJT049MTI=',
202+
'DartObfuscation': 'false',
203+
'TrackWidgetCreation': 'false',
204+
'TreeShakeIcons': 'true',
205+
'UseLocalCanvasKit': 'true',
206+
});
207+
}),
208+
},
209+
);
210+
211+
testUsingContext(
212+
'Infers target entrypoint correctly from positional argument list',
213+
() async {
214+
// Regression test for https://github.com/flutter/flutter/issues/136830.
215+
final BuildCommand buildCommand = BuildCommand(
216+
androidSdk: FakeAndroidSdk(),
217+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
218+
fileSystem: fileSystem,
219+
logger: logger,
220+
osUtils: FakeOperatingSystemUtils(),
221+
);
222+
final CommandRunner<void> runner = createTestCommandRunner(buildCommand);
223+
setupFileSystemForEndToEndTest(fileSystem);
224+
await runner.run(<String>[
225+
'build',
226+
'web',
227+
'--no-pub',
228+
'--no-web-resources-cdn',
229+
'lib/a.dart',
230+
]);
231+
232+
final Directory buildDir = fileSystem.directory(fileSystem.path.join('build', 'web'));
233+
expect(buildDir.existsSync(), true);
234+
expect(testLogger.statusText, contains('Compiling lib/a.dart for the Web...'));
235+
expect(testLogger.statusText, contains('✓ Built ${buildDir.path}'));
236+
},
237+
overrides: <Type, Generator>{
238+
Platform: () => fakePlatform,
239+
FileSystem: () => fileSystem,
240+
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
241+
ProcessManager: () => processManager,
242+
BuildSystem:
243+
() => TestBuildSystem.all(BuildResult(success: true), (
244+
Target target,
245+
Environment environment,
246+
) {
247+
expect(environment.defines, <String, String>{
248+
'TargetFile': 'lib/a.dart',
249+
'HasWebPlugins': 'true',
250+
'ServiceWorkerStrategy': 'offline-first',
251+
'BuildMode': 'release',
252+
'DartDefines':
253+
'RkxVVFRFUl9WRVJTSU9OPTAuMC4w,RkxVVFRFUl9DSEFOTkVMPW1hc3Rlcg==,RkxVVFRFUl9HSVRfVVJMPWh0dHBzOi8vZ2l0aHViLmNvbS9mbHV0dGVyL2ZsdXR0ZXIuZ2l0,RkxVVFRFUl9GUkFNRVdPUktfUkVWSVNJT049MTExMTE=,RkxVVFRFUl9FTkdJTkVfUkVWSVNJT049YWJjZGU=,RkxVVFRFUl9EQVJUX1ZFUlNJT049MTI=',
254+
'DartObfuscation': 'false',
255+
'TrackWidgetCreation': 'false',
256+
'TreeShakeIcons': 'true',
257+
'UseLocalCanvasKit': 'true',
258+
});
259+
}),
260+
},
261+
);
262+
158263
testUsingContext(
159264
'Does not allow -O0 optimization level',
160265
() async {

0 commit comments

Comments
 (0)