@@ -12,15 +12,64 @@ import 'package:path/path.dart' as path;
1212
1313import 'utils.dart' ;
1414
15+ String getDTDSnapshotDir () {
16+ // This logic is originally from pkg/dartdev/lib/src/sdk.dart
17+ //
18+ // Find SDK path.
19+ (String , bool )? trySDKPath (String executablePath) {
20+ // The common case, and how cli_util.dart computes the Dart SDK directory,
21+ // [path.dirname] called twice on Platform.executable. We confirm by
22+ // asserting that the directory `./bin/snapshots/` exists in this directory:
23+ var sdkPath = path.absolute (path.dirname (path.dirname (executablePath)));
24+ var snapshotsDir = path.join (sdkPath, 'bin' , 'snapshots' );
25+ var runFromBuildRoot = false ;
26+ final type = FileSystemEntity .typeSync (snapshotsDir);
27+ if (type != FileSystemEntityType .directory &&
28+ type != FileSystemEntityType .link) {
29+ // This is the less common case where the user is in
30+ // the checked out Dart SDK, and is executing `dart` via:
31+ // ./out/ReleaseX64/dart ... or in google3.
32+ sdkPath = path.absolute (path.dirname (executablePath));
33+ snapshotsDir = sdkPath;
34+ runFromBuildRoot = true ;
35+ }
36+
37+ // Try to locate the DartDev snapshot to determine if we're able to find
38+ // the SDK snapshots with this SDK path. This is meant to handle
39+ // non-standard SDK layouts that can involve symlinks (e.g., Brew
40+ // installations, google3 tests, etc).
41+ if (! File (
42+ path.join (snapshotsDir, 'dartdev.dart.snapshot' ),
43+ ).existsSync ()) {
44+ return null ;
45+ }
46+ return (sdkPath, runFromBuildRoot);
47+ }
48+
49+ final (sdkPath, runFromBuildRoot) = trySDKPath (Platform .resolvedExecutable) ??
50+ trySDKPath (Platform .executable)! ;
51+
52+ final String snapshotDir;
53+ if (runFromBuildRoot) {
54+ snapshotDir = sdkPath;
55+ } else {
56+ snapshotDir = path.absolute (sdkPath, 'bin' , 'snapshots' );
57+ }
58+
59+ return snapshotDir;
60+ }
61+
1562Future <DtdInfo ?> startDtd ({
1663 required bool machineMode,
1764 required bool printDtdUri,
1865}) async {
19- final sdkPath = File (Platform .resolvedExecutable).parent.parent.path;
20- final dtdSnapshot = path.absolute (
21- sdkPath,
22- 'bin' ,
23- 'snapshots' ,
66+ final snapshotDir = getDTDSnapshotDir ();
67+ final dtdAotSnapshot = path.absolute (
68+ snapshotDir,
69+ 'dart_tooling_daemon_aot.dart.snapshot' ,
70+ );
71+ final dtdSnapshot = path.absolute (
72+ snapshotDir,
2473 'dart_tooling_daemon.dart.snapshot' ,
2574 );
2675
@@ -65,15 +114,29 @@ Future<DtdInfo?> startDtd({
65114 });
66115
67116 try {
117+ // Try to spawn an isolate using the AOT snapshot of the tooling daemon.
68118 await Isolate .spawnUri (
69- Uri .file (dtdSnapshot ),
119+ Uri .file (dtdAotSnapshot ),
70120 ['--machine' ],
71121 receivePort.sendPort,
72122 onExit: exitPort.sendPort,
73123 onError: errorPort.sendPort,
74124 );
75125 } catch (_, __) {
76- completeForError ();
126+ // Spawning an isolate using the AOT snapshot of the tooling daemon failed,
127+ // we are probably in a JIT VM, try again using the JIT snapshot of the
128+ // tooling daemon.
129+ try {
130+ await Isolate .spawnUri (
131+ Uri .file (dtdSnapshot),
132+ ['--machine' ],
133+ receivePort.sendPort,
134+ onExit: exitPort.sendPort,
135+ onError: errorPort.sendPort,
136+ );
137+ } catch (_, __) {
138+ completeForError ();
139+ }
77140 }
78141
79142 final result = await completer.future.timeout (
0 commit comments