Repository navigation
Analysis server sometimes fails to resolve flutter_gen package after running "pub get" #3438
Description
Describe the bug
Almost every hour there's a situation when after getting packages or updating the .arb files the analyzer hangs for a second and then looses information about the Flutter-generated localization files.
This is partially related to #3270 as sometimes this issue occurs after Flutter extension tries to format the file. In this particular case however, it occurred after adding new package.
To Reproduce
Steps to reproduce the behavior:
- Open project and add new package in pubspec.yaml
- it may be significant that the package is a local flutter package stored in
./packages/package_namesubdirectory
- it may be significant that the package is a local flutter package stored in
- Run
Pub: Get packagesin command palette - Try to reference the new package somewhere in the widget tree
- Notice it doesn't get imported
- Run
Pub: Get packagesagain - Analyzer restarts and after that it looses information about
AppLocalizations, runningPub: Get packagesdoesn't help and only way out of that is either restarting analyzer or VS Code entirely
I was able to reproduce it in my project just by removing package from pubspec.yaml, calling Pub Get Packages several times, and then adding the package again.
Expected behavior
The package should be available immediately after getting packages for the first time.
The AppLocalizations should not be lost, as they are still present in the .dart-tool directory.
Logs
I sent the Dart instrumentation and analysis logs to [email protected]
Versions (please complete the following information):
[✓] Flutter (Channel stable, 2.2.2, on macOS 11.4 20F71 darwin-x64, locale pl-PL)
• Flutter version 2.2.2 at /Users/dominik/fvm/versions/stable
• Framework revision d79295af24 (3 weeks ago), 2021-06-11 08:56:01 -0700
• Engine revision 91c9fc8fe0
• Dart version 2.13.3
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at /Users/dominik/Library/Android/sdk
• Platform android-30, build-tools 30.0.3
• ANDROID_HOME = /Users/dominik/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.5.1, Build version 12E507
• CocoaPods version 1.10.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 4.2)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
[✓] VS Code (version 1.57.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.23.0
[✓] Connected device (3 available)
• SM T500 (mobile) • R9TR105TMWJ • android-arm64 • Android 11 (API 30)
• macOS (desktop) • macos • darwin-x64 • macOS 11.4 20F71 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 91.0.4472.114
• No issues found!

Activity
DanTup commentedon Jun 29, 2021
Can you reliably reproduce this? I tried, but couldn't - though the package appeared in the
importcompletion immediately after the first Pub: Get Packages run.I'd be interested to know:
flutter_genlisted in.dart_tool/package_config.jsonwhen this happens?AppLocalizationsnot appearing).Thanks!
orestesgaolin commentedon Jun 29, 2021
Yes, it's there
Yes, I sent the logs to [email protected]
flutter get packagesrunsPub: Get packagesPub: Get packagesPub: Get packagesI tried to reproduce in newly created project and after setting the l10n.yaml the same way as in my project, but it couldn't fail on its own even I had it open for many hours. I will try now with the package removing and adding.
orestesgaolin commentedon Jun 29, 2021
Success. I was able to reproduce it in an example project.
Steps to reproduce
Pub: Get Packagesonce or twiceRepository:
https://github.com/orestesgaolin/l10n_troubleshooting
Logs
Dart-Code-Log-2021-05-02 12-54-10.txt
DanTup commentedon Jun 29, 2021
@orestesgaolin thanks for the steps - although unfortunately I'm still not able to repro. I tried with the same Flutter version:
You mentioned
flutter_genwas in your.dart_tool/package_config.json- although there should be an entry specifically for"flutter_gen"which doesn't seem to be the case from your screenshot. If that's really missing, could you try:"flutter_gen"is explicitly inpackage_config.json"flutter_gen"has disappeared from thepackage_config.jsonfilelarssn commentedon Jun 29, 2021
I can reproduce it too, but on an old Mac. And I see it daily anyway.
Maybe there's a race condition, or a simple timer that expects
pub getto be done before it actually is?orestesgaolin commentedon Jun 30, 2021
flutter_gen,flutter_gen_coreandflutter_gen_runnerare present both in failed and working state, the file seems not to change while the problem happens, I can see it being regenerated but to the exact same state as previouslyThe
flutter_genentry seems to remain inpackage_config.jsonno matter how many times I runpub get.DanTup commentedon Jun 30, 2021
Thanks for confirming. Could you try the following (after you're in the state where the invalid errors are showing):
PackageMapUriResolverflutter_genis listed in that section (note: the package list is its own scrollable window so you may need to scroll down within that page list to see them all)Is it possible you could also send me both your
.packagesfile and.dart_tool/package_config.jsonfiles? (you can send them to [email protected]) when you're in this state?Thanks!
orestesgaolin commentedon Jul 2, 2021
Seems like
flutter_genitself is not present for the app inPackageMapUriResolver:As requested I sent the files to [email protected]
DanTup commentedon Jul 8, 2021
Sorry for the delay!
@bwilkerson do you have any thoughts on what might trigger this? There's a generated project at
.dart_tool/flutter_genwhich is listed in.dart_tool/package_config.json(but not.packages- I don't know if that's correct, but it doesn't generally cause issues since the former is used).Sometimes for @orestesgaolin running
pub getcauses theflutter_genpackage to disappear from thePackageMapUriResolverlist in the servers diagnostic page and it starts reporting errors forpackage:flutter_genURIs.I had a thought that maybe
pub getwas modifying.packagesand maybe (based on timing) the server would use that (which doesn't containflutter_gen). However, if I manually modify just.packagesmyself, that doesn't seem to trigger the issue for me (that is,flutter_genremains in the analyzersPackageMapUriResolverlist).DanTup commentedon Jul 8, 2021
Actually, I have managed to reproduce this locally (although I do still see
flutter_geninpackage_config.json- it's not listed in the diagnostics page and has generated diagnostics though). I'll add some logging into the server and see if I can track down what's happening.DanTup commentedon Jul 8, 2021
So, this is weird.. I added some logging in the server to dump the
package_json.configfile as it reads it. The log contains the whole file but it is missingflutter_gen. It ends like this:But if I open the file I now have on disk, it ends like this:
The timestamps are identical, but the one the server read was missing the package (it was also formatted differently). My suspicion is that this file is being written twice, once containing only the explicit packages, and then again with
flutter_genadded. The server picked up the first one, and the second one was written while/before it set up its file watcher, so it didn't reload on the second one.If it's
pubwriting this file twice, maybe it's not so difficult to fix. Ifpubis writing it and then callingflutterand its re-writing it, then it might be a little trickier.DanTup commentedon Jul 8, 2021
It does appear to be Flutter adding this in:
https://github.com/flutter/flutter/blob/88f38110553e5c407649ff59f43ac476c5a64aeb/packages/flutter_tools/lib/src/dart/pub.dart#L463
@bwilkerson I think what's happening here is:
pub getwrites thepackage_config.jsonfile with the pubspec dependenciesflutter_geninto the fileI wonder if the server could either read the package file after creating the watcher, or check it wasn't modified in the meantime after creating the watcher. If I've understood correctly, I think the package file is being read in the call from line 415 here, and then line 436 sets up the watcher:
https://github.com/dart-lang/sdk/blob/ccb3943e48524a993b6ca6e91cc39dabd360797f/pkg/analysis_server/lib/src/context_manager.dart#L415-L437
@bwilkerson @scheglov I'm interested in your thoughts (if it might not be a trivial change and I should move this to the SDK repo for tracking, let me know).
bwilkerson commentedon Jul 8, 2021
I think that @scheglov did some work on this fairly recently. Is this reproducible on master?
If so, then it seems reasonable to me to validate that nothing changes after the files were read the first time, but it will have a performance impact as a result of reading files twice. It's probably not too concerning given that this only happens when we're re-building contexts, which is presumably a low frequency event.
I don't know whether it's significant, but I noticed that the file on disk doesn't contain a
packageUrikey forflutter_gen. Is it possible that server is just discarding that element as a result of thinking that it's malformed (and that it isn't a timing issue)?13 remaining items
orestesgaolin commentedon Sep 2, 2021
Just wanted to update that after recent update of VS Code extension
v3.26.0and bumping Flutter to2.5.0-5.2.preit still happens the same way as before :/felix-ht commentedon Nov 29, 2021
A hack that works for me - i pinned
.dart_tool/package_config.jsonin vscode. Whenever the error happens i just save the file and the errors disappear. No need to reload the windows any longer.Would still be nice if a proper fix would land soon.
orestesgaolin commentedon Dec 9, 2021
Thanks, seems to do the job! The issue still persists on Flutter 2.8.0 and Flutter extension version 3.29.0
felix-ht commentedon Dec 10, 2021
Somehow this got way worse for me, so i wrote a simple script that watches '.dart_tool/package_config.json' and resets the modified date after the file has becomes stable.
This has no external dependencies and you can simply copy and paste it into a .dart file and run it with dart.
e.g.:
dart refresher.dartIt expects to be run from within the root folder of your flutter project. (tested only on mac)
As a file for convenience:
refresher.dart.zip
[analysis_server] Rebuild analysis contexts if changes occur between …
DanTup commentedon Jan 17, 2022
Sorry for the slow progress on this, it was a little involved. I believe the issue to be fixed by dart-lang/sdk@650b962, so once that gets into an SDK release you should no longer see this.
(If you do, please let me know!).