@@ -47,6 +47,21 @@ class InspectorPreferencesController extends DisposableController
47
47
'inspector.defaultDetailsViewType' ;
48
48
static const _customPubRootDirectoriesStoragePrefix =
49
49
'inspector.customPubRootDirectories' ;
50
+
51
+ static const _dartToolDir = '.dart_tool' ;
52
+ static const _libDir = 'lib' ;
53
+ static const _webDir = 'web' ;
54
+ static const _packagesDir = 'packages' ;
55
+
56
+ /// The following are directories that DevTools can use to determine the
57
+ /// correct pub root directory.
58
+ static const _knownDirectories = {
59
+ _dartToolDir,
60
+ _libDir,
61
+ _webDir,
62
+ _packagesDir,
63
+ };
64
+
50
65
String ? _mainScriptDir;
51
66
bool _checkedFlutterPubRoot = false ;
52
67
@@ -300,21 +315,8 @@ class InspectorPreferencesController extends DisposableController
300
315
// /third_party/dart):
301
316
if (isGoogle3Path (parts)) {
302
317
pubRootDirectory = _pubRootDirectoryForGoogle3 (parts);
303
- } else {
304
- final parts = fileUriString.split ('/' );
305
-
306
- for (int i = parts.length - 1 ; i >= 0 ; i-- ) {
307
- final part = parts[i];
308
- if (part == 'lib' || part == 'web' ) {
309
- pubRootDirectory = parts.sublist (0 , i).join ('/' );
310
- break ;
311
- }
312
-
313
- if (part == 'packages' ) {
314
- pubRootDirectory = parts.sublist (0 , i + 1 ).join ('/' );
315
- break ;
316
- }
317
- }
318
+ } else if (_pathContainsKnownDirectories (parts)) {
319
+ pubRootDirectory = _determinePubRootFromKnownDirectories (parts);
318
320
}
319
321
pubRootDirectory ?? = (parts..removeLast ()).join ('/' );
320
322
// Make sure the root directory ends with /, otherwise we will patch with
@@ -326,6 +328,34 @@ class InspectorPreferencesController extends DisposableController
326
328
return pubRootDirectory;
327
329
}
328
330
331
+ bool _pathContainsKnownDirectories (List <String > pathParts) =>
332
+ _knownDirectories.any ((dir) => pathParts.contains (dir));
333
+
334
+ String ? _determinePubRootFromKnownDirectories (List <String > pathParts) {
335
+ pathParts = _maybeRemoveDartToolDirectory (pathParts);
336
+ for (int i = pathParts.length - 1 ; i >= 0 ; i-- ) {
337
+ final part = pathParts[i];
338
+
339
+ if (part == _libDir || part == _webDir) {
340
+ pathParts = pathParts.sublist (0 , i);
341
+ break ;
342
+ }
343
+
344
+ if (part == _packagesDir) {
345
+ pathParts = pathParts.sublist (0 , i + 1 );
346
+ break ;
347
+ }
348
+ }
349
+ return pathParts.join ('/' );
350
+ }
351
+
352
+ List <String > _maybeRemoveDartToolDirectory (List <String > pathParts) {
353
+ if (pathParts.contains (_dartToolDir)) {
354
+ return pathParts.sublist (0 , pathParts.indexOf (_dartToolDir));
355
+ }
356
+ return pathParts;
357
+ }
358
+
329
359
String ? _pubRootDirectoryForGoogle3 (List <String > pathParts) {
330
360
final strippedParts = stripGoogle3 (pathParts);
331
361
if (strippedParts.isEmpty) return null ;
0 commit comments