Skip to content

"Add 'x' to dependencies" quick-fix does not handle package URIs with multiple slashes correctly #5378

Closed

Description

@KaiGerdMueller

The problem my feature request is related to:

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:
image
(Perhaps even with any import?)

Tough, after a few seconds, things normalize regarding that:
image

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.

Activity

  1. added
    is enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.
    on Dec 31, 2024
  2. added
    in editorRelates to code editing or language features
    and removed
    is enhancementAn enhancement or improvement that should be listed in release notes but is not a bug fix.
    on Jan 6, 2025
  3. added this to the v3.104.0 milestone on Jan 6, 2025
  4. DanTup commented on Jan 6, 2025

    @DanTup
    Member

    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!

  5. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in editorRelates to code editing or language featuresis bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions