Dart-Code icon indicating copy to clipboard operation
Dart-Code copied to clipboard

"Only analyze projects with open files" results in import resolution errors if you jump into Flutter without having run "flutter update-packages" (and possibly pub packages in pub cache)

Open helloworld200111 opened this issue 2 years ago • 10 comments

Describe the bug When I look at the library source code in VSCODE, some imports get this error, but not in IDEA.

To Reproduce Steps to reproduce the behavior:

CTRL + Mouse to enter the import and view the source code.

Expected behavior don't show this error

Screenshots image image

  • Operating System and version: Windows 11
  • VS Code version: win 1.85.0
  • Dart extension version:3.78.0
  • Dart/Flutter SDK version: Dart (3.2.3),Flutter (3.16.3)

helloworld200111 avatar Dec 08 '23 04:12 helloworld200111

How have you opened this file/project in VS Code? The dart:ui and package:flutter imports are only valid in the context of a Flutter project. If you open a Flutter project in VS Code and then navigate to these files, it should work fine.

If you think there's still a bug, please provide exact steps to reproduce this (starting with opening VS Code) and I'll take a look. Thanks!

DanTup avatar Dec 09 '23 15:12 DanTup

I created the Flutter project in VS code, and here is the problem replication video.

https://github.com/Dart-Code/Dart-Code/assets/74097223/2d0bf656-8876-400d-967a-152f5aedcac6

helloworld200111 avatar Dec 10 '23 01:12 helloworld200111

Is it possible you have two different versions of Flutter on your machine?

Can you try:

  • run where.exe flutter in the terminal in VS Code
  • Run the Flutter: Change SDK setting from the VS Code command palette (F1) and see which SDK is selected (take a screenshot, then press Escape to cancel)

Please provide screenshots of both

where.exe

image

Change SDK

image

image

Thanks!

DanTup avatar Dec 11 '23 11:12 DanTup

where.exe image

Change SDK image

helloworld200111 avatar Dec 11 '23 14:12 helloworld200111

Thanks, looks like they are the same SDK. I've still not been able to reproduce. Could you try capturing a log?

  • Open an empty folder in VS Code
  • Run the Dart: Capture Analyzer Logs command from the command palette (F1)
  • Now run flutter create . in the terminal
  • Open lib/main.dart
  • ctrl+click on package:flutter/material.dart
  • ctrl+click on src/material/about.dart
  • Wait for errors to appear
  • Click Cancel on the logging notification
  • Attach the log file here

Here is a video showing the steps (although the error does not occur for me).

https://github.com/Dart-Code/Dart-Code/assets/1078012/21388dd6-4c55-4cde-b5cd-589e84eb83f4

If you can reproduce and attach the log file, I might be able to understand what's happening.

Thanks!

DanTup avatar Dec 11 '23 16:12 DanTup

I have found the cause of the problem. I checked this option: image

After I unchecked this option and restart VS Code, everything is fine.

I think this option causes files are same to not being in the context of the flutter project.

Thank you very much for your previous reply.

helloworld200111 avatar Dec 12 '23 04:12 helloworld200111

I thought you might have that option ticked, however I tested with and without that option but had not been able to reproduce the issue.

If you can reproduce (even with that setting enabled), I would still be interested in the log noted above so I can do some debugging and see whether I can fix it.

(although I'll note, that I do recommend keeping that setting unticked and ticking it only for the specific workspaces that need it if required)

DanTup avatar Dec 13 '23 15:12 DanTup

Have you restarted VS Code after checking that option? I need to restart VS code to reproduce the issue.

And here is the log file (ticked option and reproduce the issue):

Dart-Code-Log-2023-11-04 10-42-36.txt

This log file is only 500+ KB, but during the process of capturing logs, there were several instances where I recorded 40+MB of log files. They are beyond the size of the attached the file.

I took a brief look. There are hundreds of “Target of URI doesn't exist” records in the big log files, and there are only three log files uploaded, which is the three of about.dart displayed.

helloworld200111 avatar Dec 14 '23 03:12 helloworld200111

This issue has been marked stale because it is tagged awaiting-info for 20 days with no activity. Remove the stale label or comment to prevent the issue being closed in 10 days.

github-actions[bot] avatar Jan 04 '24 00:01 github-actions[bot]

Thanks - I understand what's happened now. Because I contribute to Flutter, I've previously run flutter update-packages on my machine, which means there is a .dart_tool/package_config.json file inside my package:flutter. This means all of the paths can be resolved.

If I delete that folder then step into some of the framework files (with the option above set), I'll see the same issue:

image

So this issue occurs if:

  • you are using "only analyze projects with open files"
  • have not run flutter update-packages (so that the Flutter package analyzes cleanly in isolation)
  • you step into a framework file that references a package:flutter library

A possible fix for this is to have "only analyze files from open projects" to only consider files that are also inside the workspace. I don't know if that would impact other users of this though and will need to think more about it.

For now, I would recommend not using that setting (or running flutter update-packages to set up your Flutter SDK for contribution).

DanTup avatar Jan 10 '24 17:01 DanTup