-
Notifications
You must be signed in to change notification settings - Fork 1.7k
The argument type 'x' can't be assigned to the parameter type 'x' #32042
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
(@bwilkerson FWIW this doesn't seem to crash at runtime, so maybe just an analysis issue?) |
Actually, this is the same issue - I used the code-fix to add the import and it added it was a Changing I found the other issue - it's #28895. |
Yep. In Dart, two libraries are the same if, and only if, they are imported using the same URI. If two different URIs are used, even if they resolve to the same file, they will be considered to be two different libraries and the types within the file will occur twice, leading to exactly this problem. As a result, the recommendation used to be that you always use a That said, the opposite approach also works. If all URIs within a package are always relative (and two packages are not mutually recursive), then you still avoid this problem. (If you have mutually dependent packages, then using The key is to be consistent. Unfortunately, the tooling assumes the former scheme, and doesn't maintain consistency for the latter scheme. |
@bwilkerson If I understood correctly from the #28895, that issue only occurred on Windows and on Mac it was fine. I've been meaning to dig into it to see if I can debug, but haven't had chance yet (but I still have a reminder in my inbox). |
I'll be interested to hear what you find. |
Me too! Is there a convenient place I can breakpoint to inspect an error like this to see what it's comparing (so I can see how it differs between Mac/Win)? |
The way I usually tackle an issue like this is to figure out which error code defines the error you're seeing (should be defined in https://github.com/dart-lang/sdk/blob/master/pkg/analyzer/lib/src/error/codes.dart). Then find all references to the error code and put a breakpoint at the places where it's being used to create a diagnostic (there's often only one place). That usually gets me to the problematic code fairly quickly. |
@bwilkerson Great, thanks! |
Thanks! |
@maryx I never encountered this except in Flutter where If mixing import styles would cause issues in general, the only sane way of handling that would be to entirely remove relative imports from the language. |
I think the rule I use is "never have It's not how I think about it, but it really boils down to that. Always use relative imports inside a package, always use If you seem to need to say It also holds for entry-points. If you want to run That is where the actual error is in this case—there is a reference into a package using a non-package URI—and all the changing of URIs in the package are just trying to avoid the symptoms, not the root-cause of the error. |
The analyzer currently returns a fix to use the If the guidance is to not do this then I think the analyzer should be updated to change the order (or even drop the |
The error is not due to the imports. It doesn't (well, shouldn't) matter whether a package library uses a relative or absolute path to refer to another library in the same package, it should always refer to the same library. So, the error is to have |
If I'm understanding correctly, this seems different to what @bwilkerson said?
That said, based on #28895 I think the behaviour for this may be different between Windows/Mac right now. I wanted to investigate #28895 but got stuck because of #32095 (I think they might be related, but one is user-fixable and the other is not). Personally I'd prefer that the files are treated the same (even if imported with different uris) but whatever the decision I think it must be documented so we know what's a bug and what's not. |
What @bwilkerson said is correct. A relative URI reference is not a URI. It's converted into a URI by resolving it relative to the library's own URI. If you somehow import Windows has special issues because its file system is case indifferent and URIs are not, so importing |
Ok, I think I understand you now. However, the original code that caused me to raise this is: import 'package:flutter_tools/src/vscode_validator.dart';
import 'vscode_validator.dart' The top one causes the error, the button one works. Are you saying these should be considered the same? (If so, I think this is a bug to re-open?) |
The only way that those imports can do something different if in a file that is in |
This is the import 'android/android_studio_validator.dart'; to import 'package:flutter_tools/src/android/android_studio_validator.dart'; This causes the same error. I can't see an obvious issue with any of the imports there. |
How do you start the program? I'm guessing Normally a Dart app would put its entry point in In this case, you might want to consider |
You shouldn't need to run it, since it's the analysis that's reporting the error. But if it's useful, here's my launch config (the entry is in {
"name": "Flutter Doctor",
"program": "${workspaceRoot}/bin/flutter_tools.dart",
"args": [
"doctor",
"-v"
],
"request": "launch",
"type": "dart-cli"
}, |
Ah, makes sense. I think the analyzer needs to recognize The The |
Seems like I've opened a can of worms! I'm re-opening this since it seems like something is wrong somewhere, but I'm not sure where or who is best placed to look. I did try changing all the imports in Seems like those imports in the Fuchsia files have been touched be a lot of different people so I'm not sure who is best to ping about fixing them if they're deemed wrong? |
This is still the recommendation that I give, but as you can see there are others that recommend using relative paths from libraries inside
That is, in fact, exactly what analyzer does (and has been doing for years). My only guess, at this point, is that somehow on Windows we're ending up with two URIs that have different case somewhere, despite the fact that once they've been resolved to a file path the paths are identical. @DanTup Is this a hypothesis you could investigate? One way to do that is to find the location(s) at which that error is being produced and either add a breakpoint or modify the code to use the URI rather than the file path when constructing the error message. |
Yep; this is one of the things I wanted to investigate (as mentioned above, I suspected it might be Windows-only) but hit the other Windows-analyzer issue. I think we might have a reasonable fix for that now though, so I should be able to debug further. |
(I don't currently have any access in this repo, feel free to assign it to me) |
I had this issue too, and it took me a while to figure out why. Turns out one file still had the import with the all-lowercase directory name "models", and another used the capitalized "Models". I find it strange that android-studio still accepted the import as valid, even though the directory name wasn't all-lowercase anymore. Especially since whether it was capitalized or not, it still pointed to the same file if I ctrl+clicked on it. |
@xorinzor Most likely, you are running on Windows. So, from everybody's perspective, you are referencing perfectly valid files using two different URIs, so you get two different libraries. We should probably do something about this, it's too much of a foot-gun, and it's easily detectable that it really is the same Windows file. |
Yeah I'm using windows. I can understand the issue with windows not caring about capitalization, where linux does. But as you said yourself, this is really a kind of issue where it's just an absolute pain to debug the cause. So a fix would be really appreciated :) Not sure how hard it'd be to implement linux-like behaviour, where it will just error on the import if the path (case-sensitive) doesn't match. |
A Windows user generally wouldn't expect an error because they wrote Normalising paths to their file-system equiv internally when resolving the path may be an option; but it could also cause weird issues if an editor sent a request to the analysis server cased one way but the response had a different casing - so I don't think it's as simple as converting everything one way on the way in. My preferred option would be keep the case provided by the user/editor and fix all comparisons to do the right thing. There are a LOT of places this affects though - and I guess it's not just path comparisons, because it really affects the identity of types - you need to know that If it makes you feel better, this issue isn't isolated to Dart - there are so many npm packages that make assumptions about paths being case-sensitive - check how many issues reference this VS Code issue which as far as I can tell, nobody has any solid ideas for fixing. Uh, maybe that's not making anyone feel better. (probably we should continue discussion in #28895 since that's the open issue for this problem, even though the title is a little misleading since it mentions only drive letters) |
For me this was the issue, different import for the same file, I was like mad from last 3-4 days and nothing seems to be working, then I read this. thanks now it's working. |
this too long conversation makes me dizzy. please anyone tell me which comment provide the solution? thank you. much appreciated |
@jerinho |
@jerinho Most common causes of this are mixing |
I'm running into this right now
I am trying to create a new class by extending Subject from rxdart within the example folder of a Flutter package. |
@lukepighetti Your error looks different:
This looks like you may have a class named |
Hey @DanTup , I was able to get around this, but I honestly can't remember how. Forgot to report back here. |
Glad you sorted it, but do shout if you see it again and can't figure it out :-) |
I have landed here after spending an hour or so fighting this:
type casting didn't help as suggested but after digging deeper it's when I saw where the error was:
Turned out to be an uppercase/lowercase typo in one of the imports. |
For me the issue was an extra '/' in my import path import './models//image_model.dart'; rathar than- import './models/image_model.dart'; |
I have this error with no imports at all:
|
The effect of In other words, what you wrote is equivalent to
in which case the error message would read:
which hopefully makes a little more sense. |
That makes sense. Might be nice to have a lint to warn you not to name type parameters the same as existing types. |
Agreed. @pq |
Great idea. We have a similar one for function params: http://dart-lang.github.io/linter/lints/avoid_types_as_parameter_names.html |
A way to trigger this mess also is by making a generic type parameter name:
|
In my case the problem has occurred by importing the class with a slash '/' at the end (import '... / [class_name].dart/') |
@julisanchez I think we have fixed that issue since then. We currently do not allow importing a URI ending in |
I think I'm having the same issue - X can't be assigned to X. Changing import type from package to relative path resolves the problem.
|
I am getting the same error :
Anyone got solution yet? |
How are you importing |
Yes, on windows, issue solved after "flutter clean" and "flutter run" |
I think we've seen this before on Windows and believed it to be imports mixing 'package:' and relative paths, however I've just hit this today on a framework type:
Note that the types are the same:
The source code is this revision:
DanTup/flutter@601655b
Originally the
VsCodeValidator.installedValidators
returned a List (not Iterable) but I got an equally confusing message (can't assign the list to an argument of iterable) so I change it to iterable while debugging and then it still complained.I can try this on my Mac later, but I wouldn't be surprised if this was Windows-only given the previous similar error we had was.
The text was updated successfully, but these errors were encountered: