-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Two internal types created from one type declaration when a test uses relative imports #41868
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
cc @johnniwinther does this sound like a potential CFE issue? |
This is working as intended. There are few important parts to understand here:
Combining this all together explains the behaviour - when test is importing other libraries using relative URIs those libraries and all other libraries they import using relative imports are imported through Effective Dart gives very clear guidance here, see this section:
|
It might be as it's currently implemented in the Language, but does it make sense or does it more lead to confusion? Also I and others don't understand wha Effective Dart make the recommendation
at all. It too often leads to problems. Why not always use the package import as the golden rule? |
I do agree that this is somewhat confusing. /cc @lrhn to provide language design perspective on this issue. |
@escamoteur can you clarify why you prefer using a combination of import scheme for the content under import 'package:spacex_flights/src/models/second_stage.dart'; which could have been: import 'second_stage.dart'; |
I believe there are three options here. Are you sure you really prefer option 2. with the very verbose imports? 1: allow mixed importsWe'd change the language and implementation to allow both style imports. I'm not an expert here, but I'd assume this is very hard, if even possible. 2: Recommend package importsWe'd change the guidance to use package imports only. That would lead to much more verbose imports, e.g. from your sample repo, main.dart would change from: import 'package:flutter/material.dart';
import 'service_locator.dart';
import 'src/app.dart'; to: import 'package:flutter/material.dart';
import 'package:spacex_flights/service_locator.dart';
import 'package:spacex_flights/src/app.dart'; 3: Ensure projects use lints to avoid mixed mode importsWe keep the current guidance, but ensure that all project templates configure the lint to prevent this. We'd even change the severity of it to a warning, or error:
|
See also dart-lang/language#939 for a suggestion on how to reduce the problem. My usual recommendation is that you must never have any path with The usually exception comes from Flutter encouraging people to put a "main" library into |
FYI: @lrhn already created this issue, proposing that a certain amount of implementation specific normalization of URIs should be applied. This would help us avoid the situation where the same library (in some sense, e.g., same absolute path on a traditional UN*X style file system) is imported multiple times using different URIs. It would still be possible to create a similar problem (say, using several hard links), and it would be possible to go further and decide that textually identical libraries are "the same" library, but we can't hope for a complete and perfect solution. So a modest amount of normalization as proposed by @lrhn is probably the best approach. |
@mit-mit the verbosity of the package notation isn't a problem with the autoimport and path completion of the IDEs |
@lrhn what is the benefit to use relative urls inside lib? Why should we prefer them there? |
I (subjectively) disagree; I find it a lot more verbose to read. |
@mit-mit that's another point, where I agree. if we can somehow assure that either mixed imports still only create one library instance or give a warning if this is detected I'm also fine with. |
Is that not suggestion #3 in #41868 (comment) ? |
Brevity. You also avoid hard-coding your own package's name. If you want to change the name of the package, which does happen during development, you only need to change the name entry in pubspec.yaml. (Well, and any imports in |
Yep that would be an option although currently not every project you create with flutter create has an analysis_option included But having both ways work smoothly together would definitely the best solution |
Yes, we would fix that as part of option 3 (we're already working on a new minimal set of "must do" lints we're planning to add per default to all Flutter & Dart templates). |
Dart SDK Version (
dart --version
)Dart VM version: 2.9.0-7.0.dev.flutter-617bc54b71 (be) (Thu May 7 23:23:09 2020 +0000) on "windows_x64"
Whether you are using Windows, MacOSX, or Linux (if applicable)
Windows
A user of my package get_it raised an issue that a type that he registered in GetIt was not available when he tried to access it later.
GetIt uses internally a `map<Type,Object> to store registered Objects. When debugging the test I observed that the type in question was indeed stored in the map and that the type that was passed to access it was the same type but still the map returned always null.
When comparing the hashcodes of both type instances they were different.
Following a hunch I changed all relative imports to package imports because there were problems before and suddenly it worked.
I tried to reproduce it in a new project but didn't succeed. So I stripped down the project that had the problem.
Please watch this screencast:
https://www.screencast.com/t/dqufsChuC
you find the project here: https://github.com/escamoteur/spacex_flights_get_it_assertion
The text was updated successfully, but these errors were encountered: