2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
+ import 'dart:io' ;
5
6
import 'dart:typed_data' ;
6
7
7
8
import 'package:dwds/src/debugging/dart_runtime_debugger.dart' ;
@@ -20,7 +21,7 @@ abstract class LoadStrategy {
20
21
LoadStrategy (
21
22
this ._assetReader, {
22
23
String ? packageConfigPath,
23
- }) : _packageConfigPath = packageConfigPath;
24
+ }) : _packageConfigPath = packageConfigPath ?? _findPackageConfigFilePath () ;
24
25
25
26
/// The ID for this strategy.
26
27
///
@@ -83,6 +84,30 @@ abstract class LoadStrategy {
83
84
'package_config.json' ,
84
85
);
85
86
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
+
86
111
/// Returns the bootstrap required for this [LoadStrategy] .
87
112
///
88
113
/// The bootstrap is appended to the end of the entry point module.
0 commit comments