packageRoot top-level property

String get packageRoot

Returns a path to the directory containing source code for packages such as kernel, front_end, and analyzer.

Implementation

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);
}