Sitelet https://github.com/flutter/flutter/pull/190540
Skip to content

[flutter_tools] Fix hot reload for workspace member packages in lib/ (#190284) - #190540

Merged
auto-submit[bot] merged 3 commits into
flutter:masterfrom
bkonyi:fix-190284
Aug 7, 2026
Merged

auto-submit[bot] merged 3 commits into
flutter:masterfrom
bkonyi:fix-190284

Conversation

@bkonyi

@bkonyi bkonyi commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #190284

When a pub workspace member package is located under the workspace root package's lib/ directory (e.g., workspace_root/lib/member_pkg/), standard PackageConfig.toPackageUri(fileUri) performs a prefix-tree lookup that matches shorter outer prefixes first (workspace_root). Modifying workspace_root/lib/member_pkg/lib/foo.dart resulted in package:workspace_root/member_pkg/lib/foo.dart instead of package:member_pkg/foo.dart. Because the incorrect URI does not exist in the active isolate's compilation graph, ResidentCompiler reported Reloaded 0 of N libraries and dropped the edit.

This PR adds PackageConfigWorkspaceExtension.toPackageUriForWorkspace in package_map.dart, implementing longest-prefix matching across all configured packages so that nested workspace member packages resolve to their correct package: URIs.

Testing

  • Added hermetic regression test incremental compile sends correct package URI for pub workspace member package located under root lib/ in packages/flutter_tools/test/general.shard/compile_incremental_test.dart.
  • Added unit test in packages/flutter_tools/test/general.shard/dart/package_map_test.dart.

…lutter#190284)

Fixes an issue where hot reload ignores file changes in pub workspace member packages located under the workspace root package's `lib/` directory (`Reloaded 0 of N libraries`).

When translating file URIs to `package:` URIs for the resident frontend server during hot reload, `PackageConfig.toPackageUri` matches the first package whose `packageUriRoot` is a prefix of the file URI. If a workspace member package is located under the root package's `lib/` directory, the root package is matched first, causing `file:///.../lib/member/lib/foo.dart` to be incorrectly resolved to `package:root/member/lib/foo.dart` instead of `package:member/foo.dart`.

This change introduces `PackageConfigWorkspaceExtension.toPackageUriForWorkspace`, which iterates through `PackageConfig.packages` to find the most specific package (longest matching `packageUriRoot` prefix) for a given file URI. Updates `ResidentCompiler` and web build/run targets to use `toPackageUriForWorkspace`.

Issue: flutter#190284
@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Aug 4, 2026
@github-actions github-actions Bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Aug 4, 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 adds a toPackageUriForWorkspace extension method to PackageConfig to correctly resolve file URIs to package URIs in nested pub workspaces, updating the compiler and web runner targets accordingly. The review feedback suggests optimizing this new method by storing the matched root path during the loop to avoid redundant string computations.

Comment on lines +146 to +168
final path = fileUri.toString();
Package? bestMatch;
var bestMatchLength = -1;

for (final Package package in packages) {
final rootPath = package.packageUriRoot.toString();
final rootPathWithSlash = rootPath.endsWith('/') ? rootPath : '$rootPath/';
if (path.startsWith(rootPathWithSlash)) {
if (rootPathWithSlash.length > bestMatchLength) {
bestMatch = package;
bestMatchLength = rootPathWithSlash.length;
}
}
}

if (bestMatch == null) {
return null;
}

final rootPath = bestMatch.packageUriRoot.toString();
final rootPathWithSlash = rootPath.endsWith('/') ? rootPath : '$rootPath/';
final String rest = path.substring(rootPathWithSlash.length);
return Uri(scheme: 'package', path: '${bestMatch.name}/$rest');

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.

medium

The current implementation re-computes rootPathWithSlash for the bestMatch package after the loop finishes, which is redundant since it was already computed during the iteration. We can optimize this by storing the bestMatchRoot string directly, which also simplifies the code and improves readability.

    final String path = fileUri.toString();
    Package? bestMatch;
    String? bestMatchRoot;

    for (final Package package in packages) {
      final String rootPath = package.packageUriRoot.toString();
      final String rootPathWithSlash = rootPath.endsWith('/') ? rootPath : '$rootPath/';
      if (path.startsWith(rootPathWithSlash)) {
        if (bestMatchRoot == null || rootPathWithSlash.length > bestMatchRoot.length) {
          bestMatch = package;
          bestMatchRoot = rootPathWithSlash;
        }
      }
    }

    if (bestMatch == null || bestMatchRoot == null) {
      return null;
    }

    final String rest = path.substring(bestMatchRoot.length);
    return Uri(scheme: 'package', path: '${bestMatch.name}/$rest');
References
  1. Suggest simplification and refactoring: Assess whether the code can be made simpler or refactored to enhance readability and maintainability. (link)

@bkonyi
bkonyi requested a review from chingjun August 6, 2026 21:30
@bkonyi bkonyi added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 7, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Aug 7, 2026
Merged via the queue into flutter:master with commit dba040a Aug 7, 2026
22 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Aug 7, 2026
@bkonyi bkonyi added the cp: stable cherry pick this pull request to stable release candidate branch label Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD cp: stable cherry pick this pull request to stable release candidate branch tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

2 participants