Open Rockvole opened 6 years ago
This has come up a bunch. Thanks for the report!
cc @bwilkerson
The most critical piece is that you be consistent when a library inside lib
imports another library within lib
. You can either always use package:
URIs or you can always use relative paths. Either will work as far as Dart is concerned. If you want to minimize the issues you see when using the analyzer, then I recommend using package:
everywhere.
I have only seen such issues in Flutter projects, where the entry-point file is in lib/
(like lib/main.dart
) and this file contains relative imports.
This is because Flutter violates the pub package convention by having entry-point files in lib/
.
I haven't experienced or seen mentioned issues where package and relative imports were mixed within lib/
anywhere else.
The most critical piece is that you be consistent when a library inside lib imports another library within lib. You can either always use package: URIs or you can always use relative paths. Either will work as far as Dart is concerned. If you want to minimize the issues you see when using the analyzer, then I recommend using package: everywhere.
Honestly if this is a requirement, in my opinion the only sane solution is to remove relative imports from the language entirely, because the cause of potential issues far outweighs the benefit of relative imports. Not even linter rules that point out conflicts could make up for that.
Unfortunately, relative imports are required when a library outside of lib
needs to import another library outside of lib
(such as a test importing test support code).
In light of that, how about a rule that flags all relative imports to files in lib
?
I still think the only issue is the invalid entry point file flutter expects in lib/
while otherwise they are only supported in bin/
, tool/
, example/
and web/
.
Canonicalizing package and relative imports from files inside lib/
shouldn't be a problem and I think to remember that this was fixed in DartEditor times.
related dart-lang/sdk#33076
Any progress or more thoughts on this? I would really like a lint to only allow package imports in /lib.
I'm not opposed to having such a lint, similar to prefer_relative_imports
, but it just hasn't made it to the top of the priority list. Contributions are always welcome.
I have some code which confused me for a while.
main.dart
import 'package:kbml_viewer/globals.dart';
import 'preferences/preferences_description_list.dart';
preferences_description_list.dart
import '../globals.dart';
The main page displays the preferences_description_list (a list of descriptions). The main page, has a preferences page button and that preferences page also displays a variant of the same list (e.g. can also delete items from the list).
When displaying the list from the main page I get an error :
When I display the list from the preferences page it works just fine. It confused me for a while since I am new to dart I thought I didnt write the singleton correctly.
I can fix the error, by either changing to :
import 'package:kbml_viewer/preferences/preferences_description_list.dart';
in the main page, or :import 'package:kbml_viewer/globals.dart';
I realise I should not be mixing styles, but for beginners it would be nice to get some advice on the best style.