Skip to content

Consider changing default convention of putting main.dart inside lib/ or provide guidelines #15748

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

Closed
pulyaevskiy opened this issue Mar 20, 2018 · 9 comments

Comments

@pulyaevskiy
Copy link
Contributor

This is to prevent users from potential bugs caused by dart-lang/sdk#32601.

In my project I have several top-level library files, e.g.:

lib/
- widgets.dart
- models.dart
- main.dart
- ...

In the lib/main.dart I was using relative imports to these:

import 'package:flutter/widgets.dart';
import 'widgets.dart';

void main() {
  runApp(new MyApp());
}

Which caused a really weird and hard to debug issue.

As mentioned in the linked SDK issue a simple fix would be to replace relative file imports with package: imports. However it would be really hard for a new user to debug such issues or even notice that something is wrong as there is nothing that tells "do not use relative imports in lib/main.dart".

@Herohtar
Copy link

Herohtar commented Mar 20, 2018

That is probably a good idea; having main.dart in lib goes against the Dart guidelines for entrypoints mentioned in the package layout conventions.

@Hixie
Copy link
Contributor

Hixie commented Mar 20, 2018

I don't understand the problem you are running into. Can you elaborate? What are the two imports that are colliding?

@Hixie Hixie added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Mar 20, 2018
@pulyaevskiy
Copy link
Contributor Author

I have an example project demonstrating the issue here: https://github.com/pulyaevskiy/library-private-issue

In case the same Dart file is being imported using both relative and package: styles it is treated as two different libraries with their own declarations and state.

Say, I have a Dart file with a library-level field:

// file:lib/a.dart
final a = new A();
class A {}

In my project I can import it either with import 'a.dart'; or import 'package:my_project/a.dart';.
If I mix these imports in my project I essentially get two separate libraries with their own state, which means:

// file:lib/main.dart
import 'a.dart' as relativeA;
import 'package:my_project/a.dart' as packageA;

void main() {
  assert(identical(relativeA.a, packageA.a));
  /// identical returns `false`, there are two different instances of `a` field.
}

I normally use package: style imports in my project inside lib/src/. However since my main.dart is in lib/ together with some other library files I thought I could just use shorter style and import those as relative (as I've shown in the issue description).

Moving main.dart outside of lib/ would naturally force developers to use package: imports in it as well.

This is just a proposal. This can also be solved with a lint rule detecting relative imports in lib/main.dart for instance.

@zoechi
Copy link
Contributor

zoechi commented Mar 27, 2018

I see such issues very frequently. Either the analyzer and VM need to be changed to recognize such mistakes or as suggested follow pub package conventions by moving entry point files to bin/

@DanTup
Copy link
Contributor

DanTup commented Mar 28, 2018

It's worth noting that there may be additional issues with mixing relative and package: imports on Windows (see dart-lang/sdk#28895 for ex).

@pulyaevskiy
Copy link
Contributor Author

FYI: dart-lang/sdk#32601 has been re-opened and it seems like will be addressed on the language level (which would be really great).

@zoechi
Copy link
Contributor

zoechi commented May 8, 2018

should be covered by dart-lang/sdk#33076

@Hixie Hixie removed the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label May 24, 2018
@zoechi
Copy link
Contributor

zoechi commented Jun 15, 2018

I'm closing this as dup of dart-lang/sdk#33076

@github-actions
Copy link

github-actions bot commented Sep 3, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants