Dart-Code: Analysis server sometimes fails to resolve flutter_gen package after running "pub get"

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:

  1. 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_name subdirectory
  2. Run Pub: Get packages in command palette
  3. Try to reference the new package somewhere in the widget tree
  4. Notice it doesn’t get imported
  5. Run Pub: Get packages again
  6. Analyzer restarts and after that it looses information about AppLocalizations, running Pub: Get packages doesn’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.

Screenshots CleanShot 2021-06-29 at 11 23 33@2x

Logs

I sent the Dart instrumentation and analysis logs to logs@dartcode.org

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!

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 16
  • Comments: 27 (12 by maintainers)

Commits related to this issue

Most upvoted comments

A hack that works for me - i pinned .dart_tool/package_config.json in 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.

Just wanted to update that after recent update of VS Code extension v3.26.0 and bumping Flutter to 2.5.0-5.2.pre it still happens the same way as before 😕

Sorry for the slow progress on this, it was a little involved. I believe the issue to be fixed by https://github.com/dart-lang/sdk/commit/650b9627b7a7c05d9d2a489a23b2eae98c9aef95, so once that gets into an SDK release you should no longer see this.

(If you do, please let me know!).

A hack that works for me - i pinned .dart_tool/package_config.json in vscode. Whenever the error happens i just save the file and the errors disappear. No need to reload the windows any longer.

Thanks, seems to do the job! The issue still persists on Flutter 2.8.0 and Flutter extension version 3.29.0

@maks if adding the import manually works then I don’t think it’s this issue (this issue prevents the analysis server from being able to resolve package:flutter_gen to a location on disk so would report uri_not_found errors on the import).

I just tried with flutter_gallery and I also don’t get quick-fixes to import package:flutter_gen/gen_l10n/gallery_localizations.dart. IIRC, completion/quick-fix usually only suggest from packages that are explicitly listed in pubspec.yaml (eg. not transitive dependencies) so I wonder if that’s why this doesn’t work here. I’m not sure what the fix is, but it’s probably worth filing an SDK issue about that.