Skip to content

Lints to prefer project root path import #60667

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

Open
predatorx7 opened this issue May 2, 2025 · 2 comments
Open

Lints to prefer project root path import #60667

predatorx7 opened this issue May 2, 2025 · 2 comments
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package

Comments

@predatorx7
Copy link

I was wondering if you'd consider adding a new lint rule to package:lints that would encourage the use of project root path imports over relative or package imports.

Just to illustrate, currently Dart supports these import styles:

1. Relative import

import '../../core/logger.dart';

2. Package import

import 'package:core/logger.dart';

3. Project root import

import '/core/logger.dart';

I personally find the project root imports (/core/logger.dart) to be quite beneficial for code clarity and maintainability, especially in larger projects. They make it immediately clear where a file resides within the project structure, regardless of the current file's location.

Would the team be open to the idea of a lint rule that suggests (and ideally provides a dart fix for) preferring these project root path imports?

If you think this is something worth exploring and you'd like some help with it, I'd be happy to volunteer some time to contribute! 🙌🏼

Thanks for considering this! 🙏

@github-project-automation github-project-automation bot moved this to Triage backlog in Lints Triage May 2, 2025
@devoncarew devoncarew removed this from Lints Triage May 3, 2025
@devoncarew devoncarew transferred this issue from dart-lang/core May 3, 2025
@lrhn
Copy link
Member

lrhn commented May 3, 2025

Not a lint I'd ever recommend to anyone. It's just a style, it's not inherently better or worse.

Or rather, it's worse if it's enforced for all imports, including those in the same directory or a sub-directly.
It would presumably only apply to imports starting with ../. (Otherwise I'd be actively against such a lint.)

Using an absolute /core/logger.dart keeps working if you move the importing file.
Using a relative ../core/foo.dart keeps working if you move both files, by renaming a common parent directory.

@lrhn lrhn added devexp-linter Issues with the analyzer's support for the linter package area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. labels May 3, 2025
@predatorx7
Copy link
Author

It would presumably only apply to imports starting with ../. (Otherwise I'd be actively against such a lint.)

Yeah, I agree. I prefer this for imports starting with ../.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package
Projects
None yet
Development

No branches or pull requests

2 participants