Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inspector infers correct pub root directory for widget previews #8808

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ class InspectorPreferencesController extends DisposableController
'inspector.defaultDetailsViewType';
static const _customPubRootDirectoriesStoragePrefix =
'inspector.customPubRootDirectories';

static const _dartToolDir = '.dart_tool';
static const _libDir = 'lib';
static const _webDir = 'web';
static const _packagesDir = 'packages';

/// The following are directories that DevTools can use to determine the
/// correct pub root directory.
static const _knownDirectories = {
_dartToolDir,
_libDir,
_webDir,
_packagesDir,
};

String? _mainScriptDir;
bool _checkedFlutterPubRoot = false;

Expand Down Expand Up @@ -300,21 +315,8 @@ class InspectorPreferencesController extends DisposableController
// /third_party/dart):
if (isGoogle3Path(parts)) {
pubRootDirectory = _pubRootDirectoryForGoogle3(parts);
} else {
final parts = fileUriString.split('/');

for (int i = parts.length - 1; i >= 0; i--) {
final part = parts[i];
if (part == 'lib' || part == 'web') {
pubRootDirectory = parts.sublist(0, i).join('/');
break;
}

if (part == 'packages') {
pubRootDirectory = parts.sublist(0, i + 1).join('/');
break;
}
}
} else if (_pathContainsKnownDirectories(parts)) {
pubRootDirectory = _determinePubRootFromKnownDirectories(parts);
}
pubRootDirectory ??= (parts..removeLast()).join('/');
// Make sure the root directory ends with /, otherwise we will patch with
Expand All @@ -326,6 +328,34 @@ class InspectorPreferencesController extends DisposableController
return pubRootDirectory;
}

bool _pathContainsKnownDirectories(List<String> pathParts) =>
_knownDirectories.any((dir) => pathParts.contains(dir));

String? _determinePubRootFromKnownDirectories(List<String> pathParts) {
pathParts = _maybeRemoveDartToolDirectory(pathParts);
for (int i = pathParts.length - 1; i >= 0; i--) {
final part = pathParts[i];

if (part == _libDir || part == _webDir) {
pathParts = pathParts.sublist(0, i);
break;
}

if (part == _packagesDir) {
pathParts = pathParts.sublist(0, i + 1);
break;
}
}
return pathParts.join('/');
}

List<String> _maybeRemoveDartToolDirectory(List<String> pathParts) {
if (pathParts.contains(_dartToolDir)) {
return pathParts.sublist(0, pathParts.indexOf(_dartToolDir));
}
return pathParts;
}

String? _pubRootDirectoryForGoogle3(List<String> pathParts) {
final strippedParts = stripGoogle3(pathParts);
if (strippedParts.isEmpty) return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ void main() {
'my_user/google3/dart_apps/test_app/lib/main.dart': '/dart_apps/',
'my_user/google3/third_party/dart/dart_apps/test_app/lib/main.dart':
'/third_party/dart/',
'my_user/fake_app/.dart_tool/widget_preview_scaffold':
'my_user/fake_app/',
'my_user/fake_app/lib/.dart_tool/widget_preview_scaffold':
'my_user/fake_app/',
};

for (final MapEntry(key: rootLib, value: expectedPubRoot)
Expand Down
Loading