-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Library-private final fields get different instances depending on how the library was imported #32601
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
Comments
Dart libraries are identified by their import URI. If you import the same file using two different URIs, say This is expected behavior. To act differently, Dart would have to somehow recognize that it's the same file (either by content or by resolving the My personal rule of thumb is "never have
|
Thanks @anders-sandholm , this is really helpful. I wasn't sure which actual part caused this issue. I must note though that I used So in this case replacing relative imports with I submitted relevant issue in flutter/flutter#15748 for completeness. |
Can we get this re-opened to discuss fixing these semantics in say, Dart 3? |
With @zoechi responding "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/" to the Flutter issue, we need to do something. Not clear to me if this is primarily an analyzer/front-end issue (giving better error messages) or a language/library issue. |
The current model comes from a time when we expected Dart to run in the browser and import all libraries remotely by URL. It makes much less sense when imports are always files, and all import are Changing the language specification is probably doable. We just say that the identity of We could go even further and allow In almost most cases, everything will be fine and simple, but we should at least be aware of edge cases. |
@lrhn @anders-sandholm – which area should this be? |
It's a language issue. Tools might be able to alleviate the problem by implicitly converting command line file references to |
Looks like a dup of #33076 (or the other way around) |
Duplicate of #33076. |
I tried to find relevant issues via search but it didn't yield anything similar.
This may be me learning, but I find following behavior really confusing.
For instance, this Dart file:
Up until now I assumed that
_a
would always be the same instance, but it turns out not entirely true and there can actually be multiple instances of_a
depending on how import / export statements are organized in a project.It is a bit verbose to describe in this issue so I created example project here: https://github.com/pulyaevskiy/library-private-issue
Executing
dart lib/main.dart
there prints something like this for me:Combination of export and import statements in that project is probably far from perfect and normally should be cleaned up. However it seems dangerous to even allow such scenarios.
The text was updated successfully, but these errors were encountered: