Files
sdk/pkg/analyzer_utilities/lib/package_root.dart
T
Danny Tuppeny 605e89d3bd [analyzer_utilities] Find pkg folder from working directory
When running tests from analysis_server using "dart test", the Platform.script is a temporary file and this code fails like:

```
00:01 +0 -1: FlutterSnippetCompletionWithoutNullSafetyTest | test_snippets_flutterStateless_notAvailable_notTopLevel [E]
  Bad state: Unable to find sdk/pkg/ in /var/folders/b1/ftm2whhj4mb3_c4nfjhq23z00000gn/T/dart_test.kernel.VYZHoV/test.dart_1.dill
```

This change allows using the working directory to find `pkg` if all of the previous attempts fail, before giving up.

Change-Id: Idb29029e6f14ec4bab3569018771ed0e10586ecc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/307120
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
2023-06-02 18:24:00 +00:00

55 lines
1.9 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 pathos;
/// 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 it.
const pkgRootVar =
bool.hasEnvironment('pkgRoot') ? String.fromEnvironment('pkgRoot') : null;
if (pkgRootVar != null) {
var path = pathos.join(Directory.current.path, pkgRootVar);
if (!path.endsWith(pathos.separator)) path += pathos.separator;
return path;
}
// Otherwise try to guess based on the script path.
var scriptPath = pathos.fromUri(Platform.script);
var pathFromScript = _tryGetPkgRoot(scriptPath);
if (pathFromScript != null) {
return pathFromScript;
}
// Try google3 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.
final runFiles = Platform.environment['RUNFILES'];
final analyzerPackagesRoot = Platform.environment['ANALYZER_PACKAGES_ROOT'];
if (runFiles != null && analyzerPackagesRoot != null) {
return pathos.join(runFiles, analyzerPackagesRoot);
}
// Finally, try the current working directory.
var pathFromCwd = _tryGetPkgRoot(Directory.current.path);
if (pathFromCwd != null) {
return pathFromCwd;
}
throw StateError('Unable to find sdk/pkg/ in $scriptPath');
}
/// Try to find the path to the pkg folder from [path].
String? _tryGetPkgRoot(String path) {
var parts = pathos.split(path);
var pkgIndex = parts.indexOf('pkg');
if (pkgIndex != -1) {
return pathos.joinAll(parts.sublist(0, pkgIndex + 1)) + pathos.separator;
}
return null;
}