Skip to content

Commit 7f723f4

Browse files
bkonyicommit-bot@chromium.org
authored andcommitted
[ VM / CLI ] Fix issue where VM flags would be parsed from the option
list for a script. Fixes #43487 Change-Id: I6f0860e0a43055aaab775a28baa660d60fb1c47e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/169782 Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Ben Konyi <[email protected]>
1 parent 195820e commit 7f723f4

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

pkg/dartdev/lib/dartdev.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,16 @@ Future<void> runDartdev(List<String> args, SendPort port) async {
8989
args = List.from(args)..remove('--disable-dartdev-analytics');
9090
}
9191

92-
// These flags have a format that can't be handled by package:args, so while
93-
// they are valid flags we'll assume the VM has verified them by this point.
94-
args = args
95-
.where(
96-
(element) => !(element.contains('--observe') ||
97-
element.contains('--enable-vm-service')),
98-
)
99-
.toList();
92+
if (args.contains('run')) {
93+
// These flags have a format that can't be handled by package:args, so while
94+
// they are valid flags we'll assume the VM has verified them by this point.
95+
args = args
96+
.where(
97+
(element) => !(element.contains('--observe') ||
98+
element.contains('--enable-vm-service')),
99+
)
100+
.toList();
101+
}
100102

101103
// If ... help pub ... is in the args list, remove 'help', and add '--help'
102104
// to the end of the list. This will make it possible to use the help

pkg/dartdev/test/commands/run_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,18 @@ void run() {
221221
expect(result.stderr, contains('Unhandled exception'));
222222
expect(result.exitCode, 255);
223223
});
224+
225+
test('does not interpret VM flags provided after script', () async {
226+
p = project(mainSrc: 'void main() { assert(false); }');
227+
228+
// Any VM flags passed after the script shouldn't be interpreted by the VM.
229+
ProcessResult result = p.runSync('run', [
230+
p.relativeFilePath,
231+
'--enable-asserts',
232+
]);
233+
234+
expect(result.stdout, isEmpty);
235+
expect(result.stderr, isEmpty);
236+
expect(result.exitCode, 0);
237+
});
224238
}

runtime/bin/main_options.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,18 @@ int Options::ParseArguments(int argc,
599599
// dartdev command (e.g., --enable-vm-service, --observe, etc).
600600
if (!run_script) {
601601
int tmp_i = i;
602+
// We only run the CLI implicitly if the service is enabled and the user
603+
// didn't run with the 'run' command. If they did provide a command, we need
604+
// to skip it here to continue parsing VM flags.
605+
if (!implicitly_use_dart_dev) {
606+
tmp_i++;
607+
}
602608
while (tmp_i < argc) {
609+
// Check if this flag is a potentially valid VM flag. If not, we've likely
610+
// hit a script name and are done parsing VM flags.
611+
if (!OptionProcessor::IsValidFlag(argv[tmp_i], kPrefix, kPrefixLen)) {
612+
break;
613+
}
603614
OptionProcessor::TryProcess(argv[tmp_i], vm_options);
604615
tmp_i++;
605616
}

0 commit comments

Comments
 (0)