connectedAppPackageRoot method
Returns the package root URI for the connected app.
This should be the directory up the tree from the debugging target that contains the .dart_tool/package_config.json file.
This method contains special logic for detecting the package root for
test targets (i.e., a VM service connections spawned from dart test
or
flutter test
). This is because the main isolate for test targets is
running the test runner, and not the test library itself, so we have to do
some extra work to find the package root of the test target.
Implementation
Future<Uri?> connectedAppPackageRoot(DTDManager dtdManager) async {
var packageRootUriString =
await rootPackageDirectoryForMainIsolate(dtdManager);
_log.fine(
'[connectedAppPackageRoot] root package directory for main isolate: '
'$packageRootUriString',
);
// If a Dart library URI was returned, this may be a test target (i.e. a
// VM service connection spawned from `dart test` or `flutter test`).
if (packageRootUriString?.endsWith('.dart') ?? false) {
final rootLibrary = await _mainIsolateRootLibrary();
if (rootLibrary != null) {
packageRootUriString = (await _lookupPackageRootByEval(rootLibrary)) ??
// TODO(kenz): remove this fallback once all test bootstrap
// generators include the `packageConfigLocation` constant we
// can evaluate.
await _lookupPackageRootByImportPrefix(
rootLibrary,
dtdManager,
);
}
}
_log.fine(
'[connectedAppPackageRoot] package root for test target: '
'$packageRootUriString',
);
return packageRootUriString == null
? null
: Uri.parse(packageRootUriString);
}