Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import priority tool #59702

Open
FMorschel opened this issue Dec 12, 2024 · 6 comments
Open

Import priority tool #59702

FMorschel opened this issue Dec 12, 2024 · 6 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. dart-cli-language-server Issues related to the 'dart language-server' tool P4 type-enhancement A request for a change that isn't a bug

Comments

@FMorschel
Copy link
Contributor

FMorschel commented Dec 12, 2024

In dart-lang/language#4112 one technique discussed/suggested is to export a library with show/hide.

and then import that everywhere you would otherwise import package:flutter/material.dart.

I think we need improved tooling for this.

When I once attempted something similar, the inconvenience of needing to select the correct import - and needing to remember it - is not great.

Perhaps we should have some kind of annotation we could use to say that "this export is preferred over importing directly"? Or perhaps that should be its own issue? I'm not sure where it would go though.

Originally posted by @TekExplorer in dart-lang/language#4112 (comment)

In my company, we have an internal rule. Say we have the following project structure:

lib/
├── a.dart
├── b.dart
├── f1/
│   ├── a1.dart
│   ├── b1.dart
│   └── f1.dart // barrel file exporting a1, b1
└── f2/
    ├── a2.dart
    ├── b2.dart
    ├── f2.dart // barrel file exporting a2, b2, f3/f3
    └── f3/
        ├── a3.dart
        └── f3.dart // barrel file exporting a3
  • If we are in either a or b, we'd import the barrel files for f1 or f2.
  • If we are in a1 and we need something from b1 we'd import it directly, if we need anything else we'd import the closest file with that declaration (meaning a, b or f2/f2)
  • If we are in a2 we'd follow the same rule as above (for files inside f3 we'd import its barrel)
  • If we are in f3 we'd import only barrel files or a/b.

I'm sure other projects have other rules for this but the point stands that importing the right file is a bit hard and a better tool for this would be a great addition!

Not sure what to suggest besides the above but I'm opening this as a place for this conversation.

P.S.: If this is not the right place to open this please say so.

@dart-github-bot
Copy link
Collaborator

Summary: User requests improved tooling for managing Dart imports, suggesting annotations to prioritize specific exports or a tool to help choose the correct import based on project structure and internal import rules.

@dart-github-bot dart-github-bot added area-dart-cli Use area-dart-cli for issues related to the 'dart' command like tool. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Dec 12, 2024
@bkonyi bkonyi added the dart-cli-language-server Issues related to the 'dart language-server' tool label Dec 12, 2024
@khoadng
Copy link

khoadng commented Dec 13, 2024

I have the same issue

lib/
├── core/
│   ├── a/
│   │   ├── a1.dart
│   │   ├── a2.dart
│   │   └── a.dart  // barrel file exporting a1, a2
│   ├── b/
│   │   ├── b1.dart
│   │   ├── b2.dart
│   │   └── b.dart // barrel file exporting b1, b2
├── feats/
│   └── f1/
│       └── f1_a.dart // import a, b

Every time I want to import something from a1, I have to choose between four options: relative or full path, and barrel or direct file. This creates significant mental overhead. To make matters worse, code completion randomly picks imports, sometimes choosing a relative import with the direct file, and other times a full path import from the barrel file. Fixing these inconsistencies is extremely frustrating.

@Levi-Lesches
Copy link

For your particular issue, might I suggest instead lib/core.dart and lib/feats.dart? That way you can always use package imports without touching src, giving you one option for importing anything. It's possible, without knowing details of your case, that making these barrel files for every folder within core might be overkill

@khoadng
Copy link

khoadng commented Dec 13, 2024

@Levi-Lesches
I had tried something similar before, and it caused the analyzer to slow to a crawl because everything was interdependent. The project consisted of 100k LOC spread across more than 1,100 files.

Related issues:
#50369
#56307

@FMorschel
Copy link
Contributor Author

FMorschel commented Dec 13, 2024

Every time I want to import something from a1, I have to choose between four options: relative or full path, and barrel or direct file.

Not long ago I've closed an issue where now if you have one of the lints enabled it should only suggest that kind of import (relative or package) but I've also added a new version for "with show" so the number is the same with more meaning:

These should land on Dart 3.7.

@FMorschel
Copy link
Contributor Author

@lrhn lrhn added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. and removed area-dart-cli Use area-dart-cli for issues related to the 'dart' command like tool. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. labels Dec 17, 2024
@keertip keertip added the P4 label Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. dart-cli-language-server Issues related to the 'dart language-server' tool P4 type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants