1188d014a9
Work towards https://github.com/dart-lang/sdk/issues/61597 The analyzer_testing package's mock packages are "data" or "resource" files which are notoriously hard to find on disk. No dart tooling I know of can give me "the root directory of a package which is referenced in a Dart program via "package:". Konstantin notes that a program may be compiled ahead of time, and so the running VM itself does not know where source files originally came from. When users of the analyzer_testing package try to use `addFlutterPackageDep` in a test, which attempts to locate the Dart SDK's `pkg/` root directory, they get a StateError. This CL adds more information and context to that error. Change-Id: I3ae55cd580c8fa16ca86d66e6f7b655b8f9bcc42 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/452532 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Commit-Queue: Samuel Rawlins <srawlins@google.com>
64 lines
2.2 KiB
Dart
64 lines
2.2 KiB
Dart
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:path/path.dart' as path;
|
|
|
|
/// Returns a path to the directory containing source code for packages such as
|
|
/// kernel, front_end, and analyzer.
|
|
String get packageRoot {
|
|
// If the package root directory is specified on the command line using
|
|
// `-DpkgRoot=...`, use that.
|
|
const pkgRootVar = bool.hasEnvironment('pkgRoot')
|
|
? String.fromEnvironment('pkgRoot')
|
|
: null;
|
|
if (pkgRootVar != null) {
|
|
var pkgRootPath = path.join(Directory.current.path, pkgRootVar);
|
|
if (!pkgRootPath.endsWith(path.separator)) pkgRootPath += path.separator;
|
|
return pkgRootPath;
|
|
}
|
|
// Otherwise try to guess based on the script path.
|
|
var scriptPath = path.fromUri(Platform.script);
|
|
var pathFromScript = _tryGetPkgRoot(scriptPath);
|
|
if (pathFromScript != null) {
|
|
return pathFromScript;
|
|
}
|
|
|
|
// Try a Bazel environment. We expect that all packages that will be
|
|
// accessed via this root are configured in the BUILD file, and located
|
|
// inside this single root.
|
|
var runFiles = Platform.environment['TEST_SRCDIR'];
|
|
var analyzerPackagesRoot = Platform.environment['ANALYZER_PACKAGES_ROOT'];
|
|
if (runFiles != null && analyzerPackagesRoot != null) {
|
|
return path.join(runFiles, analyzerPackagesRoot);
|
|
}
|
|
|
|
// Finally, try the current working directory.
|
|
var pathFromCwd = _tryGetPkgRoot(Directory.current.path);
|
|
if (pathFromCwd != null) {
|
|
return pathFromCwd;
|
|
}
|
|
|
|
var exceptionMessage =
|
|
'Unable to find the Dart SDK package root directory ("sdk/pkg/") in ';
|
|
if (pkgRootVar != null) {
|
|
exceptionMessage += '"$pkgRootVar" (specified via `-DpkgRoot=`), or ';
|
|
}
|
|
exceptionMessage +=
|
|
'in an ancestor of either "$pathFromScript", or "$pathFromCwd".';
|
|
|
|
throw StateError(exceptionMessage);
|
|
}
|
|
|
|
/// Tries to find the path to the 'pkg' folder from [searchPath].
|
|
String? _tryGetPkgRoot(String searchPath) {
|
|
var parts = path.split(searchPath);
|
|
var pkgIndex = parts.indexOf('pkg');
|
|
if (pkgIndex != -1) {
|
|
return path.joinAll(parts.sublist(0, pkgIndex + 1)) + path.separator;
|
|
}
|
|
return null;
|
|
}
|