-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Import path's affect class definitions? #31230
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
That is correct. Library equality in Dart is defined in terms of the URI used to reference the library rather than the path to the file containing the library. The code above is loading two different libraries (that happen to be defined by the same file), and hence two different classes (that happen to have the same name). I've been advising people for a long time to always use a |
IMHO, these semantics made sense in the JITted web world, where URL, not the file, gives identity to the downloaded code. In AOT (dart4web, Flutter) and command-line Dart VM, it's just confusing. |
But if you get a class via an export, it's the same as getting it via the source library. ...and it shouldn't matter if the export got the type via package: or relative URI. Feel very much like a bug! |
I don't know what you mean by "source library". When you import library A, and library A exports a class from library B, the class is still defined in library B, and the URI used to reference the library (in the export directive) is still used to identify the library.
The description above is the way I read the current specification. I'd be happy to find out I'm wrong. Personally, I think we should change the specification to use file paths rather than URI's, because I think the days of loading code into a VM running in the browser are gone. That's why I marked this as a language issue. |
Actually, @jxson – I have a question. It looks like you put this file in Try moving it to If it works, I'm with Brian. I'm arguing for semantics that I'm about 98% are valid. If you're in file X and you're resolved as a If you're running
|
dup of #33076 |
Duplicate of #33076. |
I had this issue in Dart 2.8.4. when the 2 URLs were practically the same, but one of them didn't have a canonical path:
See the extra slash in the 2nd URL? I would expect the Dart VM/Compiler to canonise it or issue a warning and/or have the "Optimize imports" command in Android Studio canonise it, but none of these happened/worked. I just got a runtime error. Should I open a new issue on it @matanlurey ? |
@bwilkerson - would it be appropriate to add a hint in the analyzer when a URI should be canonicalized in an import? |
I would say, yes. I doubt that anyone is intentionally using URIs that are not canonical in order to prevent a file from being seen as being the same, so I expect there'd be zero false positives. |
Filed as a new feature request: #42829 |
While working on some Flutter code in Fuchsia I noticed some weird behavior, it took some head scratching to figure out a bug that was caused by it.
If you import the same file using different paths it changes the class/type definition:
The asserts above will fail.
The text was updated successfully, but these errors were encountered: