Skip to content

Commit ccb5514

Browse files
committed
added findPackageConfigFilePath to find the package_config.json file
1 parent 616da45 commit ccb5514

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

dwds/lib/src/loaders/strategy.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:io';
56
import 'dart:typed_data';
67

78
import 'package:dwds/src/debugging/dart_runtime_debugger.dart';
@@ -20,7 +21,7 @@ abstract class LoadStrategy {
2021
LoadStrategy(
2122
this._assetReader, {
2223
String? packageConfigPath,
23-
}) : _packageConfigPath = packageConfigPath;
24+
}) : _packageConfigPath = packageConfigPath ?? _findPackageConfigFilePath();
2425

2526
/// The ID for this strategy.
2627
///
@@ -83,6 +84,30 @@ abstract class LoadStrategy {
8384
'package_config.json',
8485
);
8586

87+
/// Returns the absolute file path of the `package_config.json` file in the `.dart_tool`
88+
/// directory, searching recursively from the current directory hierarchy.
89+
/// If `_packageConfigPath` is already set, it returns the cached value immediately.
90+
static String? _findPackageConfigFilePath() {
91+
var candidateDir = Directory(DartUri.currentDirectory).absolute;
92+
93+
while (true) {
94+
final candidatePackageConfigFile =
95+
File(p.join(candidateDir.path, '.dart_tool', 'package_config.json'));
96+
97+
if (candidatePackageConfigFile.existsSync()) {
98+
return candidatePackageConfigFile.path;
99+
}
100+
101+
final parentDir = candidateDir.parent;
102+
if (parentDir.path == candidateDir.path) {
103+
// We've reached the root directory
104+
return null;
105+
}
106+
107+
candidateDir = parentDir;
108+
}
109+
}
110+
86111
/// Returns the bootstrap required for this [LoadStrategy].
87112
///
88113
/// The bootstrap is appended to the end of the entry point module.

0 commit comments

Comments
 (0)