Files
sdk/pkg/linter/tool/util/path_utils.dart
T
Danny Tuppeny 22e3e969b3 [analysis_server] [linter] Fix some additional tests when running through the test runner
`Platform.script` doesn't work when run through `dart test` (see https://github.com/dart-lang/test/issues/110). This uses `Isolate.resolvePackageUriSync` to locate package roots instead.

Change-Id: Ieda05cd625cf07152d695daf94c99f1fad1cefe3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/419720
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
2025-04-01 14:59:19 -07:00

41 lines
1.2 KiB
Dart

// Copyright (c) 2023, 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 'dart:isolate';
import 'package:path/path.dart' as path;
String get linterPackageRoot => path.joinAll(_packageRoot);
List<String> get _packageRoot {
// Locate the root of the package without using `Platform.script` as it fails
// when run through the `dart test`.
// https://github.com/dart-lang/test/issues/110
var packageLibUri = Isolate.resolvePackageUriSync(
Uri.parse('package:linter/'),
);
var parts = path.split(path.dirname(packageLibUri!.toFilePath()));
while (parts.last != 'linter') {
parts.removeLast();
if (parts.isEmpty) {
throw StateError(
"Script is not located inside a 'linter' directory? "
"'${Platform.script.path}'",
);
}
}
return parts;
}
String pathRelativeToPackageRoot(Iterable<String> parts) =>
path.joinAll([..._packageRoot, ...parts]);
String pathRelativeToPkgDir(Iterable<String> parts) {
var pathParts = _packageRoot;
pathParts.replaceRange(pathParts.length - 1, pathParts.length, parts);
return path.joinAll(pathParts);
}