From d19a405abb8997397db70c01ec3dc9d2cfe8e13e Mon Sep 17 00:00:00 2001 From: Sigmund Cherem Date: Fri, 21 Jan 2022 16:42:40 +0000 Subject: [PATCH] [test_runner]: launch dart2js via 'dart compile js'. We will be deprecating the dart2js script shipped with the sdk under dart-sdk/bin/. This change starts using 'dart compile js' when running dart2js in `useSdk` mode. For host-asserts, we continue to use a helper script that invokes dart2js from sources. We will continue to support this helper script, but it will become an internal only tool. It is a bit confusing that such script lives under sdk/bin/dart2js and sdk/bin/dart2js_developer. We may consider renaming this in a later CL or moving it to a new location (e.g. under tools). Change-Id: I4f426936f5e0e0a1a7854f4f555198d5ae382079 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/229060 Reviewed-by: Alexander Thomas --- pkg/test_runner/lib/src/command.dart | 68 +++++++++++++++++-- .../lib/src/compiler_configuration.dart | 44 ++++++------ pkg/test_runner/lib/src/test_case.dart | 2 +- 3 files changed, 84 insertions(+), 30 deletions(-) diff --git a/pkg/test_runner/lib/src/command.dart b/pkg/test_runner/lib/src/command.dart index da28355f4c7..c14af03eecb 100644 --- a/pkg/test_runner/lib/src/command.dart +++ b/pkg/test_runner/lib/src/command.dart @@ -124,7 +124,9 @@ class ProcessCommand extends Command { (io.Platform.operatingSystem == 'windows') ? env.write('set $key=${escapeCommandLineArgument(value)} & ') : env.write('$key=${escapeCommandLineArgument(value)} ')); - var command = ([executable]..addAll(batchArguments)..addAll(arguments)) + var command = ([executable] + ..addAll(nonBatchArguments) + ..addAll(arguments)) .map(escapeCommandLineArgument) .join(' '); if (workingDirectory != null) { @@ -135,10 +137,13 @@ class ProcessCommand extends Command { bool get outputIsUpToDate => false; - /// Arguments that are passed to the process when starting batch mode. - /// - /// In non-batch mode, they should be passed before [arguments]. + /// Additional arguments to prepend before [arguments] when running the + /// process in batch mode. List get batchArguments => const []; + + /// Additional arguments to prepend before [arguments] when running the + /// process in non-batch mode. + List get nonBatchArguments => const []; } class CompilationCommand extends ProcessCommand { @@ -243,6 +248,61 @@ class CompilationCommand extends ProcessCommand { deepJsonCompare(_bootstrapDependencies, other._bootstrapDependencies); } +class Dart2jsCompilationCommand extends CompilationCommand { + final bool useSdk; + + Dart2jsCompilationCommand( + String outputFile, + List bootstrapDependencies, + String executable, + List arguments, + Map environmentOverrides, + {this.useSdk, + bool alwaysCompile, + String workingDirectory, + int index = 0}) + : super("dart2js", outputFile, bootstrapDependencies, executable, + arguments, environmentOverrides, + alwaysCompile: alwaysCompile || true, + workingDirectory: workingDirectory, + index: index); + + @override + CommandOutput createOutput(int exitCode, bool timedOut, List stdout, + List stderr, Duration time, bool compilationSkipped, + [int pid = 0]) { + return Dart2jsCompilerCommandOutput( + this, exitCode, timedOut, stdout, stderr, time, compilationSkipped); + } + + @override + List get batchArguments { + return [ + if (useSdk) ...['compile', 'js'], + ...super.batchArguments, + ]; + } + + @override + List get nonBatchArguments { + return [ + if (useSdk) ...['compile', 'js'], + ...super.nonBatchArguments, + ]; + } + + @override + void _buildHashCode(HashCodeBuilder builder) { + super._buildHashCode(builder); + builder.addJson(useSdk); + } + + @override + bool _equal(Dart2jsCompilationCommand other) { + return super._equal(other) && useSdk == other.useSdk; + } +} + class FastaCompilationCommand extends CompilationCommand { final Uri _compilerLocation; diff --git a/pkg/test_runner/lib/src/compiler_configuration.dart b/pkg/test_runner/lib/src/compiler_configuration.dart index 3a47af84c18..1ef049d399f 100644 --- a/pkg/test_runner/lib/src/compiler_configuration.dart +++ b/pkg/test_runner/lib/src/compiler_configuration.dart @@ -386,27 +386,26 @@ class Dart2jsCompilerConfiguration extends CompilerConfiguration { : super._subclass(configuration); String computeCompilerPath() { - var prefix = 'sdk/bin'; - var suffix = shellScriptExtension; - - if (_isHostChecked) { - if (_useSdk) { - // Note: when [_useSdk] is true, dart2js is run from a snapshot that was - // built without checked mode. The VM cannot make such snapshot run in - // checked mode later. These two flags could be used together if we also - // build an sdk with checked snapshots. - throw "--host-checked and --use-sdk cannot be used together"; - } - // The script dart2js_developer is not included in the - // shipped SDK, that is the script is not installed in - // "$buildDir/dart-sdk/bin/" - return '$prefix/dart2js_developer$suffix'; + if (_isHostChecked && _useSdk) { + // When [_useSdk] is true, dart2js is compiled into a snapshot that was + // built without checked mode. The VM cannot make such snapshot run in + // checked mode later. These two flags could be used together if we also + // build an sdk with checked snapshots. + throw "--host-checked and --use-sdk cannot be used together"; } if (_useSdk) { - prefix = '${_configuration.buildDirectory}/dart-sdk/bin'; + var dartSdk = '${_configuration.buildDirectory}/dart-sdk'; + // When using the shipped sdk, we invoke dart2js via the dart CLI using + // `dart compile js`. The additional `compile js` arguments are added + // within [Dart2jsCompilationCommand]. This is because the arguments are + // added differently depending on whether the command is executed in batch + // mode or not. + return '$dartSdk/bin/dart$executableExtension'; + } else { + var scriptName = _isHostChecked ? 'dart2js_developer' : 'dart2js'; + return 'sdk/bin/$scriptName$shellScriptExtension'; } - return '$prefix/dart2js$suffix'; } Command computeCompilationCommand(String outputFileName, @@ -414,14 +413,9 @@ class Dart2jsCompilerConfiguration extends CompilerConfiguration { arguments = arguments.toList(); arguments.add('--out=$outputFileName'); - return CompilationCommand( - 'dart2js', - outputFileName, - bootstrapDependencies(), - computeCompilerPath(), - arguments, - environmentOverrides, - alwaysCompile: !_useSdk); + return Dart2jsCompilationCommand(outputFileName, bootstrapDependencies(), + computeCompilerPath(), arguments, environmentOverrides, + useSdk: _useSdk, alwaysCompile: !_useSdk); } List bootstrapDependencies() { diff --git a/pkg/test_runner/lib/src/test_case.dart b/pkg/test_runner/lib/src/test_case.dart index 2597e45782d..72ed102acb0 100644 --- a/pkg/test_runner/lib/src/test_case.dart +++ b/pkg/test_runner/lib/src/test_case.dart @@ -260,7 +260,7 @@ class RunningProcess { _commandComplete(0); } else { var processEnvironment = _createProcessEnvironment(); - var args = command.arguments; + var args = [...command.nonBatchArguments, ...command.arguments]; var processFuture = io.Process.start(command.executable, args, environment: processEnvironment, workingDirectory: command.workingDirectory);