Skip to content

[flutter_tools] Resolve workspace root when running widget preview from member package - #190952

Open
bkonyi wants to merge 2 commits into
flutter:masterfrom
bkonyi:issue-190692
Open

[flutter_tools] Resolve workspace root when running widget preview from member package#190952
bkonyi wants to merge 2 commits into
flutter:masterfrom
bkonyi:issue-190692

Conversation

@bkonyi

@bkonyi bkonyi commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

When flutter widget-preview start is run from a member package in a pub workspace, redirect getRootProject() to the workspace root FlutterProject. This ensures that the widget preview scaffold is generated at the workspace root and has dependency overrides for all member packages in the workspace.

Fixes #190692

…om member package

When `flutter widget-preview start` is run from a member package in a pub
workspace, redirect `getRootProject()` to the workspace root `FlutterProject`.
This ensures that the widget preview scaffold is generated at the workspace
root and has dependency overrides for all member packages in the workspace.

Fixes flutter#190692
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 11, 2026
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Aug 11, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the widget preview command to run from the workspace root when executed within a member package of a workspace. It introduces a workspaceRoot getter on FlutterProject to locate the workspace root by traversing parent directories and matching against workspace glob patterns. Feedback suggests optimizing the workspace membership check in _findWorkspaceRoot by using Glob.matches on the relative path in-memory, avoiding expensive filesystem scanning and potential crashes from other invalid sibling packages.

Comment on lines +128 to +137
if (candidateProject.manifest.workspace.isNotEmpty) {
if (candidateProject.workspaceProjects.any(
(FlutterProject p) => fileSystem.path.equals(
fileSystem.path.normalize(p.directory.absolute.path),
normalizedPath,
),
)) {
return candidateProject;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using candidateProject.workspaceProjects to check membership is highly inefficient and fragile:

  1. Performance Bottleneck: Accessing workspaceProjects triggers eager filesystem scanning (listFileSystemSync) and instantiates a FlutterProject for every member package in the workspace. In large monorepos, this can be extremely slow.
  2. Fragility: If any other sibling package in the workspace has a malformed pubspec.yaml or is temporarily invalid, instantiating its FlutterProject will throw an exception. This causes _findWorkspaceRoot to catch the exception and fail to recognize the workspace root entirely, even if the current package is perfectly valid.

Solution:
Use Glob.matches on the relative path of the current project to determine membership in-memory. This avoids filesystem listing and instantiating other projects entirely.

Suggested change
if (candidateProject.manifest.workspace.isNotEmpty) {
if (candidateProject.workspaceProjects.any(
(FlutterProject p) => fileSystem.path.equals(
fileSystem.path.normalize(p.directory.absolute.path),
normalizedPath,
),
)) {
return candidateProject;
}
}
if (candidateProject.manifest.workspace.isNotEmpty) {
final String relativePath = fileSystem.path.relative(normalizedPath, from: candidate.path);
final bool isMember = candidateProject.manifest.workspace.any((String entry) {
final Glob glob = Glob(entry, context: fileSystem.path);
return glob.matches(relativePath);
});
if (isMember) {
return candidateProject;
}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in f86ddee. Updated _findWorkspaceRoot to use in-memory Glob.matches on the relative path and added test coverage for malformed sibling package resilience.

…rror resilience

Use in-memory `Glob.matches` on the relative project path instead of
eagerly instantiating and traversing all sibling `workspaceProjects`.
Also guard child project instantiation in `_setManifest` against malformed
sibling packages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Widget Preview] Pub Workspace preview fails to resolve a member package when started from the workspace root

1 participant