Skip to content

Resolve injected client using package_config in AOT mode#2801

Closed
jyameo wants to merge 4 commits intodart-lang:mainfrom
jyameo:jy-fix-webdev
Closed

Resolve injected client using package_config in AOT mode#2801
jyameo wants to merge 4 commits intodart-lang:mainfrom
jyameo:jy-fix-webdev

Conversation

@jyameo
Copy link
Copy Markdown
Contributor

@jyameo jyameo commented Mar 25, 2026

Description

Fixes a StateError: Cannot resolve "package:dwds/src/injected/client.js" that occurs when using webdev serve in an AOT-compiled environment (e.g. dart pub global activate webdev).

Because Dart strips the default package configuration mapping inside AOT snapshots, Isolate.resolvePackageUri() evaluates to null. This PR introduces a graceful natively-supported fallback via package_config that reads the active snapshot's URI via Platform.script to accurately locate the host's .dart_tool/package_config.json. This successfully resolves the injected debugging client out of .pub-cache.

Changes

  • dwds/lib/src/handlers/injector.dart: Implemented a findPackageConfig fallback block for when Isolate.resolvePackageUri natively fails.

Testing

  • Verified AOT fallback by manually triggering new code path to validate the fix (unfortunately it's not trivial to write an automated unit test for this fallback because dart test executes test isolates from temporary OS cache directories that physically lack a .dart_tool/package_config.json folder hierarchy, which inherently breaks the Platform.script upwards traversal).

Related to #2761

// snapshot.
try {
if (Platform.script.scheme == 'file') {
final packageConfig = await findPackageConfig(
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.

What packageConfig does this actually find here? I'm confused about how there could be one when this is running in AOT mode so it makes me suspect this is just finding some random packageConfig on your local machine that happens to some entry in it that works.

That could explain why this doesn't work on CI when the test runs since it is starting from a clean state.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nick's right. This method checks for .dart_tool/package_config.json in the CWD, and if it can't find it, it checks for .dart_tool/package_config.json in the parent directory. If it still can't find it, it repeats the process until we hit the root directory.

We somehow need to be able to determine the package:dwds version being used, find the PUB_CACHE, and search for the directory for the correct version of DWDS. That's unfortunately going to be brittle since an installed, precompiled webdev instance doesn't require its dependencies to still be present in the PUB_CACHE.

@dcharkes, this seems like something that should be solved by packaging client.js with webdev as part of dart install. Can you provide any guidance here? 😄

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.

I don't have enough context here. DWDS is called from webdev directly?
Why are you finding the package config here? For what purpose?

I believe webdev was partially fixed to be able to work via dart install:

With dart install you cannot rely on:

  • Having access to your own source code.
  • Having access to a Dart SDK. (You can dart install something and then remove the Dart or Flutter SDK and the installed apps should keep working.)

Can you explain in more detail what dwds is trying to do here?

@dcharkes, this seems like something that should be solved by packaging client.js with webdev as part of dart install. Can you provide any guidance here? 😄

I have no idea what this means.

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.

You are absolutely right guys! My apologies. I had mistakenly assumed that globally activated packages would preserve their .dart_tool/package_config.json layout relative to the generated snapshot inside ~/.pub-cache/. I'll continue to investigate this

Copy link
Copy Markdown
Contributor

@dcharkes dcharkes Mar 26, 2026

Choose a reason for hiding this comment

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

Dart data assets are not here yet:

As discussed offline with @bkonyi, until data assets are in Dart standalone, the best way to bundle a file is to just have it as a string or base64 encoded byte array.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

After talking with @dcharkes, I think that #2768 is going to be the right path forward.

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.

Thanks for the feedback guys! I will use the embedded string approach moving forward.

@jyameo
Copy link
Copy Markdown
Contributor Author

jyameo commented Mar 26, 2026

Closing this PR. Will send out another PR using the approach discussed above.

@jyameo jyameo closed this Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants