You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Add 'x' to dependencies" quick-fix does not handle package URIs with multiple slashes correctly #5378
I am using the standard VS-Code setup to develop a flutter app.
Recently I needed imports of the form:
import 'package:highlight/languages/java.dart';
So not like:
import 'package:<package_name>/<file_name>.dart';
But like:
import 'package:<package_name>/<folder_name>/<file_name>.dart';
I tried to go about it the usual way, simply using this suggestion to automatically import, but it failed:
Firstly, if you just start with such an import in an empty project, you sometimes don't even get that option:
(Perhaps even with any import?)
Tough, after a few seconds, things normalize regarding that:
So that's not that much of a problem in my opinion.
Now for the real "issue", if you just try the "auto-import" suggestion, you get:
[flutter_application_1] flutter pub add highlight/languages
Using a naked argument for directory is deprecated and will stop working in a future Flutter release.
Use --directory instead.
Expected to find project root in highlight/languages.
exit code 1
If you modify the import to look like this, however:
import 'package:highlight/sth.dart';
(With some nonexistent file name)
Then the import works as expected, and you can change the import back to:
import 'package:highlight/languages/java.dart';
which then works.
It seems to me, the layman, that importing only part of a library is not possible (anymore?) for some reason, causing the "auto-import" to needlessly fail, when it could instead just try to import the whole library, not just the specific folder.
The "easy" solution:
From my layman perspective, it seems like a simple solution would be for the auto-importer to simply always import the whole library.
Possible issues with the "easy" solution:
As I'm not that experienced with dart and flutter, it may be that in other use cases, people actually need the auto-import to work this way, and I simply don't know it. In this case, it would probably be best to have auto-import first try to work as it does now, and then have it work as in the "easy" solution if and only if the other way does not work.
Thanks for the easy repro! The issue here was actually the regular expression that extracts the package name from the import. It was trying to add a package named "highlight/languages" (using pub add) when it should have been using just highlight (which is the name of the package). It worked fine for package:foo/bar.dart where there was only one slash, but when there were multiple slashes, it was being greedy and matching the last slash instead of the first.
I've pushed a fix that will be included in the next release of the extension. Thanks!
changed the title [-]Automatic import works (at least from my layman's perspective) quite suboptimal.[/-][+]"Add 'x' to dependencies" quick-fix does not handle package URIs with multiple slashes correctly[/+]on Jan 7, 2025
Activity
DanTup commentedon Jan 6, 2025
Thanks for the easy repro! The issue here was actually the regular expression that extracts the package name from the import. It was trying to add a package named "highlight/languages" (using
pub add) when it should have been using justhighlight(which is the name of the package). It worked fine forpackage:foo/bar.dartwhere there was only one slash, but when there were multiple slashes, it was being greedy and matching the last slash instead of the first.I've pushed a fix that will be included in the next release of the extension. Thanks!
[-]Automatic import works (at least from my layman's perspective) quite suboptimal.[/-][+]"Add 'x' to dependencies" quick-fix does not handle package URIs with multiple slashes correctly[/+]Fix extraction of the package name from import URIs