[dart2js] Allow for consistent CLI flags for staged compilations.


Issue: https://github.com/dart-lang/sdk/issues/60353
Change-Id: I58f9f17b6916fec0d6279e1659618950aa695f81
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/416327
Reviewed-by: Mayank Patke <fishythefish@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
diff --git a/pkg/compiler/lib/src/options.dart b/pkg/compiler/lib/src/options.dart
index 8989560..941b673 100644
--- a/pkg/compiler/lib/src/options.dart
+++ b/pkg/compiler/lib/src/options.dart
@@ -32,6 +32,7 @@
       CompilerPhase.codegen,
       CompilerPhase.emitJs,
     },
+    canCompileFromEntryUri: true,
   ),
   dumpInfoAll(
     'dump-info-all',
@@ -43,8 +44,9 @@
       CompilerPhase.emitJs,
       CompilerPhase.dumpInfo,
     },
+    canCompileFromEntryUri: true,
   ),
-  cfe('cfe', phases: {CompilerPhase.cfe}),
+  cfe('cfe', phases: {CompilerPhase.cfe}, canCompileFromEntryUri: true),
   deferredLoadIds(
     'deferred-load-ids',
     dataOutputName: 'deferred_load_ids.data',
@@ -80,11 +82,13 @@
     this._stageFlag, {
     this.dataOutputName,
     required this.phases,
+    this.canCompileFromEntryUri = false,
   });
 
   final Set<CompilerPhase> phases;
   final String _stageFlag;
   final String? dataOutputName;
+  final bool canCompileFromEntryUri;
 
   bool get emitsJs => phases.contains(CompilerPhase.emitJs);
   bool get shouldOnlyComputeDill => this == CompilerStage.cfe;
@@ -329,12 +333,12 @@
 
   /// Returns the compilation target specified by these options.
   Uri get compilationTarget =>
-      _inputDillUri ?? entryUri ?? _defaultInputDillUri;
+      _inputDillUri ??
+      (stage.canCompileFromEntryUri ? entryUri : null) ??
+      _defaultInputDillUri;
 
-  bool get shouldLoadFromDill {
-    final targetPath = (_inputDillUri ?? entryUri)?.path;
-    return targetPath == null || targetPath.endsWith('.dill');
-  }
+  bool get shouldLoadFromDill =>
+      entryUri == null || compilationTarget.path.endsWith('.dill');
 
   /// Location of the package configuration file.
   Uri? packageConfig;
@@ -655,11 +659,11 @@
   /// If specified, a bundle of optimizations to enable (or disable).
   int? optimizationLevel;
 
-  /// The shard to serialize when using [Flags.writeCodegen].
+  /// The shard to serialize when running the codegen phase.
   int? codegenShard;
 
-  /// The number of shards to serialize when using [Flags.writeCodegen] or to
-  /// deserialize when using [Flags.readCodegen].
+  /// The number of shards to serialize when running the codegen phase or to
+  /// deserialize when running the emit-js phase.
   int? codegenShards;
 
   /// Arguments passed to the front end about how it is invoked.
diff --git a/pkg/compiler/test/end_to_end/command_line_test.dart b/pkg/compiler/test/end_to_end/command_line_test.dart
index ef998c9..85ad4ce 100644
--- a/pkg/compiler/test/end_to_end/command_line_test.dart
+++ b/pkg/compiler/test/end_to_end/command_line_test.dart
@@ -176,6 +176,10 @@
       '${Flags.stage}=closed-world',
       '--out=/some/path/prefix-',
     ], writeClosedWorld: '/some/path/prefix-world.data');
+    await test([
+      '${Flags.stage}=closed-world',
+      'foo.dart',
+    ], writeClosedWorld: 'world.data');
 
     // Run global inference only
     await test(
@@ -234,6 +238,11 @@
       readClosedWorld: '/some/path/foo.dataworld.data',
       writeData: '/some/path/foo.dataglobal.data',
     );
+    await test(
+      ['foo.dart', '${Flags.stage}=global-inference'],
+      readClosedWorld: 'world.data',
+      writeData: 'global.data',
+    );
 
     // Run codegen only
     await test(
@@ -361,6 +370,19 @@
       codegenShard: 10,
       codegenShards: 11,
     );
+    await test(
+      [
+        '${Flags.stage}=codegen',
+        '${Flags.codegenShard}=10',
+        '${Flags.codegenShards}=11',
+        'foo.dart',
+      ],
+      readClosedWorld: 'world.data',
+      readData: 'global.data',
+      writeCodegen: 'codegen',
+      codegenShard: 10,
+      codegenShards: 11,
+    );
 
     // Run emitter only
     await test(
@@ -450,6 +472,14 @@
       codegenShards: 11,
       out: '/some/path/prefix-out.js',
     );
+    await test(
+      ['${Flags.stage}=emit-js', '${Flags.codegenShards}=11', 'foo.dart'],
+      readClosedWorld: 'world.data',
+      readData: 'global.data',
+      readCodegen: 'codegen',
+      codegenShards: 11,
+      out: 'out.js',
+    );
 
     // Run codegen and emitter only
     await test(
@@ -503,6 +533,12 @@
       readData: '/some/path/prefix-global.data',
       out: '/some/path/prefix-out.js',
     );
+    await test(
+      ['${Flags.stage}=codegen-emit-js', 'foo.dart'],
+      readClosedWorld: 'world.data',
+      readData: 'global.data',
+      out: 'out.js',
+    );
 
     // Invalid states with stage flag
     // Codegen stage