dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.59k forks source link

Add lint warning about recommended import style #57700

Open Rockvole opened 6 years ago

Rockvole commented 6 years ago

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 :

E/flutter ( 3086): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 3086): FileSystemException: Directory listing failed, path = 'null/keyboards/' (OS Error: No such file or directory, errno = 2)
E/flutter ( 3086): #0      FileTools.directoriesInDirectory (package:keyboard/src/file_tools.dart:21:5)
E/flutter ( 3086): <asynchronous suspension>
E/flutter ( 3086): dart-lang/sdk#57147      _PreferencesDescriptionsState._onRefresh (package:kbml_viewer/preferences/preferences_description_list.dart:47:42)
E/flutter ( 3086): <asynchronous suspension>
E/flutter ( 3086): dart-lang/sdk#57148      _PreferencesDescriptionsState.initState (package:kbml_viewer/preferences/preferences_description_list.dart:72:5)
E/flutter ( 3086): dart-lang/sdk#57149      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3743:58)
E/flutter ( 3086): dart-lang/sdk#57150      ComponentElement.mount (package:flutter/src/widgets/framework.dart:3609:5)
E/flutter ( 3086): dart-lang/sdk#57151      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2899:14)
E/flutter ( 3086): dart-lang/sdk#57152      Element.updateChild (package:flutter/src/widgets/framework.dart:2702:12)
E/flutter ( 3086): dart-lang/sdk#57153      SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4646:14)
E/flutter ( 3086): dart-lang/sdk#57154      Element.inflateWidget (package:flutter/src/widgets/framework.dart:2899:14)
E/flutter ( 3086): dart-lang/sdk#57155      Element.updateChild (package:flutter/src/widgets/framework.dart:2702:12)
E/flutter ( 3086): dart-lang/sdk#57156     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
E/flutter ( 3086): dart-lang/sdk#57157     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
E/flutter ( 3086): dart-lang/sdk#57158     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3614:5)
E/flutter ( 3086): dart-lang/sdk#57159     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3609:5)
E/flutter ( 3086): dart-lang/sdk#57160     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2899:14)
E/flutter ( 3086): dart-lang/sdk#57161     Element.updateChild (package:flutter/src/widgets/framework.dart:2702:12)
E/flutter ( 3086): dart-lang/sdk#57162     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4646:14)
E/flutter ( 3086): dart-lang/sdk#57163     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2899:14)
E/flutter ( 3086): dart-lang/sdk#57164     Element.updateChild (package:flutter/src/widgets/framework.dart:2702:12)
E/flutter ( 3086): dart-lang/sdk#57165     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4646:14)
E/flutter ( 3086): dart-lang/sdk#57166     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2899:14)
E/flutter ( 3086): dart-lang/sdk#57167     Element.updateChild (package:flutter/src/widgets/framework.dart:2702:12)
E/flutter ( 3086): dart-lang/sdk#57168     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4646:14)
E/flutter ( 3086): dart-lang/sdk#57169     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2899:14)
E/flutter ( 3086): dart-lang/sdk#57170     Element.updateChild (package:flutter/src/widgets/framework.dart:2702:12)
E/flutter ( 3086): dart-lang/sdk#57171     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:4646:14)
E/flutter ( 3086): dart-lang/sdk#57172     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2899:14)
E/flutter ( 3086): dart-lang/sdk#57173     Element.updateChild (package:flutter/src/widgets/framework.dart:2702:12)
E/flutter ( 3086): dart-lang/sdk#57174     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
E/flutter ( 3086): dart-lang/sdk#57175     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
E/flutter ( 3086): dart-lang/sdk#57176     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3614:5)
E/flutter ( 3086): dart-lang/sdk#57177     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:3761:11)
E/flutter ( 3086): dart-lang/sdk#57178     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3609:5)
E/flutter ( 3086): dart-lang/sdk#57179     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2899:14)
E/flutter ( 3086): dart-lang/sdk#57182     Element.updateChild (package:flutter/src/widgets/framework.dart:2702:12)
E/flutter ( 3086): dart-lang/sdk#57183     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
E/flutter ( 3086): dart-lang/sdk#57184     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
E/flutter ( 3086): dart-lang/sdk#57185     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3614:5)
E/flutter ( 3086): dart-lang/sdk#57186     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3609:5)
E/flutter ( 3086): dart-lang/sdk#57187     Element.inflateWidget (package:flutter/src/widgets/framework.dart:2899:14)
E/flutter ( 3086): dart-lang/sdk#57188     Element.updateChild (package:flutter/src/widgets/framework.dart:2702:12)
E/flutter ( 3086): dart-lang/sdk#57189     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3645:16)
E/flutter ( 3086): dart-lang/sdk#57190     Element.rebuild (package:flutter/src/widgets/framework.dart:3487:5)
E/flutter ( 3086): dart-lang/sdk#57191     ComponentElement._firstBuild (package:flutter/src/widgets/framewor
E/flutter ( 3086): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 3086): FileSystemException: Directory listing failed, path = 'null/keyboards/' (OS Error: No such file or directory, errno = 2)
E/flutter ( 3086): #0      FileTools.directoriesInDirectory (package:keyboard/src/file_tools.dart:21:5)
E/flutter ( 3086): <asynchronous suspension>
E/flutter ( 3086): dart-lang/sdk#57147      _PreferencesDescriptionsState._onRefresh (package:kbml_viewer/preferences/preferences_description_list.dart:47:42)
E/flutter ( 3086): <asynchronous suspension>
E/flutter ( 3086): dart-lang/sdk#57148      RefreshIndicatorState._show.<anonymous closure> (package:flutter/src/material/refresh_indicator.dart:338:53)
E/flutter ( 3086): dart-lang/sdk#57149      _RootZone.runUnary (dart:async/zone.dart:1381:54)
E/flutter ( 3086): dart-lang/sdk#57150      _FutureListener.handleValue (dart:async/future_impl.dart:129:18)
E/flutter ( 3086): dart-lang/sdk#57151      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:637:45)
E/flutter ( 3086): dart-lang/sdk#57152      Future._propagateToListeners (dart:async/future_impl.dart:666:32)
E/flutter ( 3086): dart-lang/sdk#57153      Future._completeWithValue (dart:async/future_impl.dart:481:5)
E/flutter ( 3086): dart-lang/sdk#57154      Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:511:7)
E/flutter ( 3086): dart-lang/sdk#57155      _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
E/flutter ( 3086): dart-lang/sdk#57156     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)

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.

pq commented 6 years ago

This has come up a bunch. Thanks for the report!

cc @bwilkerson

bwilkerson commented 6 years ago

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.

zoechi commented 6 years ago

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.

bwilkerson commented 6 years ago

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).

pq commented 6 years ago

In light of that, how about a rule that flags all relative imports to files in lib?

zoechi commented 6 years ago

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.

zoechi commented 6 years ago

related dart-lang/sdk#33076

MisterJimson commented 4 years ago

Any progress or more thoughts on this? I would really like a lint to only allow package imports in /lib.

bwilkerson commented 4 years ago

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.