Skip to content

Commit 060e4e2

Browse files
srawlinsCommit Queue
authored and
Commit Queue
committed
dartdev: Support running the AOT analyis_server snapshot for analyze command
The `dart analyze` command is quite separate from the `dart language-server` command. This CL adds support for `dart analyze`. Work towards #50498 Change-Id: I60a846ae5d3452c2bb050bd07502084ff44b82c0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/425188 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Siva Annamalai <[email protected]>
1 parent d7b3562 commit 060e4e2

File tree

9 files changed

+54
-50
lines changed

9 files changed

+54
-50
lines changed

pkg/analysis_server/lib/src/utilities/sdk.dart

+1-8
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,7 @@ class Sdk {
3030

3131
/// Path to the 'dart' executable in the Dart SDK.
3232
String get dart {
33-
var basename = path.basename(Platform.executable);
34-
// It's possible that `Platform.executable` won't include the '.exe'
35-
// extension on Windows (e.g., launching `dart` from `cmd.exe` where `dart`
36-
// is on the `PATH`). Append '.exe' in this case so the
37-
// `checkArtifactExists` check won't fail.
38-
if (Platform.isWindows && !basename.endsWith('.exe')) {
39-
basename += '.exe';
40-
}
33+
var basename = Platform.isWindows ? 'dart.exe' : 'dart';
4134
return path.absolute(
4235
_runFromBuildRoot ? sdkPath : path.absolute(sdkPath, 'bin'),
4336
basename,

pkg/dartdev/lib/src/analysis_server.dart

+30-18
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class AnalysisServer {
3737
this.enabledExperiments = const [],
3838
this.disableStatusNotificationDebouncing = false,
3939
this.suppressAnalytics = false,
40-
});
40+
bool useAotSnapshot = false,
41+
}) : _useAotSnapshot = useAotSnapshot;
4142

4243
final String? cacheDirectoryPath;
4344
final File? packagesFile;
@@ -48,6 +49,7 @@ class AnalysisServer {
4849
final List<String> enabledExperiments;
4950
final bool disableStatusNotificationDebouncing;
5051
final bool suppressAnalytics;
52+
final bool _useAotSnapshot;
5153

5254
Process? _process;
5355

@@ -107,24 +109,8 @@ class AnalysisServer {
107109
/// Starts the process and returns the pid for it.
108110
Future<int> start({bool setAnalysisRoots = true}) async {
109111
preAnalysisServerStart?.call(commandName, analysisRoots, argResults);
110-
final command = [
111-
sdk.analysisServerSnapshot,
112-
if (suppressAnalytics) '--${Driver.SUPPRESS_ANALYTICS_FLAG}',
113-
'--${Driver.CLIENT_ID}=dart-$commandName',
114-
'--disable-server-feature-completion',
115-
'--disable-server-feature-search',
116-
if (disableStatusNotificationDebouncing)
117-
'--disable-status-notification-debouncing',
118-
'--disable-silent-analysis-exceptions',
119-
'--sdk',
120-
sdkPath.path,
121-
if (cacheDirectoryPath != null) '--cache=$cacheDirectoryPath',
122-
if (packagesFile != null) '--packages=${packagesFile!.path}',
123-
if (enabledExperiments.isNotEmpty)
124-
'--$experimentFlagName=${enabledExperiments.join(',')}'
125-
];
126112

127-
final process = await startDartProcess(sdk, command);
113+
final process = await _startProcess();
128114
_process = process;
129115
_shutdownResponseReceived = false;
130116
// This callback hookup can't throw.
@@ -206,6 +192,32 @@ class AnalysisServer {
206192
return process.pid;
207193
}
208194

195+
Future<Process> _startProcess() {
196+
final executable = _useAotSnapshot ? sdk.dartAotRuntime : sdk.dart;
197+
final arguments = [
198+
if (_useAotSnapshot)
199+
sdk.analysisServerAotSnapshot
200+
else
201+
sdk.analysisServerSnapshot,
202+
if (suppressAnalytics) '--${Driver.SUPPRESS_ANALYTICS_FLAG}',
203+
'--${Driver.CLIENT_ID}=dart-$commandName',
204+
'--disable-server-feature-completion',
205+
'--disable-server-feature-search',
206+
if (disableStatusNotificationDebouncing)
207+
'--disable-status-notification-debouncing',
208+
'--disable-silent-analysis-exceptions',
209+
'--sdk',
210+
sdkPath.path,
211+
if (cacheDirectoryPath != null) '--cache=$cacheDirectoryPath',
212+
if (packagesFile != null) '--packages=${packagesFile!.path}',
213+
if (enabledExperiments.isNotEmpty)
214+
'--$experimentFlagName=${enabledExperiments.join(',')}',
215+
];
216+
217+
log.trace('$executable ${arguments.join(' ')}');
218+
return Process.start(executable, arguments);
219+
}
220+
209221
Future<String> getVersion() {
210222
return _sendCommand('server.getVersion')
211223
.then((response) => response['version']);

pkg/dartdev/lib/src/commands/analyze.dart

+12-3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ class AnalyzeCommand extends DartdevCommand {
9090
help: 'The path to the Dart SDK.',
9191
hide: !verbose,
9292
)
93+
..addFlag(
94+
useAotSnapshotFlag,
95+
help: 'Use the AOT analysis server snapshot',
96+
hide: true,
97+
)
9398
..addExperimentalFlags();
9499
}
95100

@@ -129,20 +134,23 @@ class AnalyzeCommand extends DartdevCommand {
129134
final printMemory = args.flag('memory') && jsonFormat;
130135

131136
io.Directory sdkPath;
137+
final useAotSnapshot = args.flag(useAotSnapshotFlag);
132138
if (args.wasParsed('sdk-path')) {
133139
sdkPath = io.Directory(args.option('sdk-path')!);
134140
if (!sdkPath.existsSync()) {
135141
usageException('Invalid Dart SDK path: ${sdkPath.path}');
136142
}
143+
final snapshotName = useAotSnapshot
144+
? 'analysis_server_aot.dart.snapshot'
145+
: 'analysis_server.dart.snapshot';
137146
final snapshotPath = path.join(
138147
sdkPath.path,
139148
'bin',
140149
'snapshots',
141-
'analysis_server.dart.snapshot',
150+
snapshotName,
142151
);
143152
if (!io.File(snapshotPath).existsSync()) {
144-
usageException(
145-
'Invalid Dart SDK path has no analysis_server.dart.snapshot file: '
153+
usageException("Invalid Dart SDK path has no '$snapshotName' file: "
146154
'${sdkPath.path}');
147155
}
148156
} else {
@@ -180,6 +188,7 @@ class AnalyzeCommand extends DartdevCommand {
180188
disableStatusNotificationDebouncing: true,
181189
enabledExperiments: args.enabledExperiments,
182190
suppressAnalytics: suppressAnalytics,
191+
useAotSnapshot: useAotSnapshot,
183192
);
184193

185194
server.onErrors.listen((FileAnalysisErrors fileErrors) {

pkg/dartdev/lib/src/commands/language_server.dart

+3-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ For more information about the server's capabilities and configuration, see:
3535
usageLineLength: dartdevUsageLineLength,
3636
includeHelpFlag: false,
3737
defaultToLsp: true,
38-
)..addFlag(_useAotSnapshotFlag,
38+
)..addFlag(useAotSnapshotFlag,
3939
help: 'Use the AOT analysis server snapshot', hide: true);
4040
}
4141

@@ -49,11 +49,11 @@ For more information about the server's capabilities and configuration, see:
4949
args = [...args, '--$protocol=$lsp'];
5050
}
5151
try {
52-
if (argResults!.flag(_useAotSnapshotFlag)) {
52+
if (argResults!.flag(useAotSnapshotFlag)) {
5353
if (!Sdk.checkArtifactExists(sdk.dartAotRuntime)) {
5454
return _genericErrorExitCode;
5555
}
56-
args.remove('--$_useAotSnapshotFlag');
56+
args.remove('--$useAotSnapshotFlag');
5757
VmInteropHandler.run(
5858
sdk.dartAotRuntime,
5959
[sdk.analysisServerAotSnapshot, ...args],
@@ -77,7 +77,5 @@ For more information about the server's capabilities and configuration, see:
7777
}
7878
}
7979

80-
static const _useAotSnapshotFlag = 'use-aot-snapshot';
81-
8280
static const _genericErrorExitCode = 255;
8381
}

pkg/dartdev/lib/src/core.dart

-14
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import 'package:cli_util/cli_logging.dart';
1212
import 'package:path/path.dart' as path;
1313

1414
import 'experiments.dart';
15-
import 'sdk.dart';
1615
import 'utils.dart';
1716

1817
// Initialize a default logger. We'll replace this with a verbose logger if
@@ -86,19 +85,6 @@ extension DartDevCommand<T> on Command<T> {
8685
globalResults!.multiOption(experimentFlagName);
8786
}
8887

89-
/// A utility method to start a Dart VM instance with the given arguments and an
90-
/// optional current working directory.
91-
///
92-
/// [arguments] should contain the snapshot path.
93-
Future<Process> startDartProcess(
94-
Sdk sdk,
95-
List<String> arguments, {
96-
String? cwd,
97-
}) {
98-
log.trace('${sdk.dart} ${arguments.join(' ')}');
99-
return Process.start(sdk.dart, arguments, workingDirectory: cwd);
100-
}
101-
10288
Future<int> runProcess(
10389
List<String> command, {
10490
bool logToTrace = false,

pkg/dartdev/lib/src/sdk.dart

+2
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,5 @@ class Runtime {
288288
return Runtime._(version, channel);
289289
}
290290
}
291+
292+
const useAotSnapshotFlag = 'use-aot-snapshot';

sdk/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ declare_args() {
4242
# ......utils/gen_snapshot or utils/gen_snapshot.exe (if not on ia32)
4343
# ......snapshots/
4444
# ........analysis_server.dart.snapshot
45+
# ........analysis_server_aot.dart.snapshot (AOT snapshot, if not on ia32)
4546
# ........dart2bytecode.snapshot (AOT snapshot, for selected targets)
4647
# ........dart2js_aot.dart.snapshot (AOT snapshot, if not on ia32)
4748
# ........dart2js.dart.snapshot (JIT snapshot only on ia32)

utils/analysis_server/BUILD.gn

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ aot_snapshot("analysis_server_aot") {
99
main_dart = "../../pkg/analysis_server/bin/server.dart"
1010
name = "analysis_server_aot"
1111
output = "$root_gen_dir/analysis_server_aot.dart.snapshot"
12-
vm_args = [ "-Dbuilt_as_aot=true" ]
12+
args = [ "-Dbuilt_as_aot=true" ]
1313
}
1414

1515
aot_snapshot("analysis_server_aot_product") {
1616
main_dart = "../../pkg/analysis_server/bin/server.dart"
1717
name = "analysis_server_aot_product"
1818
output = "$root_gen_dir/analysis_server_aot_product.dart.snapshot"
19+
args = [ "-Dbuilt_as_aot=true" ]
1920

2021
# dartaotruntime in the dart sdk has dart_product_config applied to it,
2122
# so it is built in product mode in both release and
@@ -32,5 +33,4 @@ application_snapshot("analysis_server") {
3233
"--sdk=" + rebase_path("../../sdk/"),
3334
"--train-using=" + rebase_path("../../pkg/compiler/lib"),
3435
]
35-
vm_args = [ "-Dbuilt_as_aot=false" ]
3636
}

utils/aot_snapshot.gni

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ template("aot_snapshot") {
9898
if (product_mode) {
9999
args += [ "-Ddart.vm.product=true" ]
100100
}
101+
if (defined(invoker.args)) {
102+
args += invoker.args
103+
}
101104
}
102105

103106
# Whether to build an AOT snapshot, which can be opened by dlopen.

0 commit comments

Comments
 (0)