vscode-csharp
vscode-csharp copied to clipboard
Wrong message "ambiguity between X and X" (which are the same)
Type: Bug
I'm working on a C# project for a while now and starting today, I repeatedly get the error that something would not be okay with my code. The message on the red underlined code tranlates as "Ambiguity between "MyClass.MyProperty" and "MyClass.MyProperty"". That class and property exists exactly once and shows no issue itself. The message is invalid. Rebuilding the code (Ctrl+Shift+B) does not resolve the error. Reloading the window does resolve it.
I can't show you the code. It's too much in its entirety and even just loading it would not reproduce, you have to actually edit it a lot to see the problem. You never know when it's going to happen. But I've had this 2 or 3 times within the last hour in different places. I assume you have your own projects to try this. My project type is .NET 7 for the Web SDK, but it's only a Web API, so no dangerous* file types like Razor, JavaScript or HTML. (* causing loads of problems on their own)
Somehow the source code analyser gets confused with an increasing size of code. It's already slightly overwhelmed with small code, when it shows errors that are not really there. Making any change to the document, or even just saving it to disk (Ctrl+S) immediately resolve such errors or warnings. It looks like the analyser is making a decision what messages to show, while I'm editing my code again (pressing another key) and then ignores my latest edit while it was busy. But only the last input resolved the warnings (previous text was incomplete, leading to the warnings/errors). It cannot keep up with normal typing speed.
It gets more and more annoying to see this code editor making so many errors. I think I'm switching back to Visual Studio as it turned out I'll have it available in this project.
Extension version: 1.25.2 VS Code version: Code 1.74.3 (97dec172d3256f8ca4bfb2143f3f76b503ca0534, 2023-01-09T16:59:02.252Z) OS version: Windows_NT x64 10.0.19045 Modes: Sandboxed: No
System Info
| Item | Value |
|---|---|
| CPUs | Intel(R) Core(TM) i7-3770 CPU @ 3.40GHz (8 x 3400) |
| GPU Status | 2d_canvas: enabled canvas_oop_rasterization: disabled_off direct_rendering_display_compositor: disabled_off_ok gpu_compositing: enabled multiple_raster_threads: enabled_on opengl: enabled_on rasterization: enabled raw_draw: disabled_off_ok skia_renderer: enabled_on video_decode: enabled video_encode: enabled vulkan: disabled_off webgl: enabled webgl2: enabled webgpu: disabled_off |
| Load (avg) | undefined |
| Memory (System) | 15.71GB (5.92GB free) |
| Process Argv | --crash-reporter-id 1b00a758-578c-4b77-a258-fe145c18e004 |
| Screen Reader | no |
| VM | 29% |
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
pythonvspyl392:30443607
vserr242cf:30382550
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vshan820:30294714
vstes263cf:30335440
vscorecescf:30445987
pythondataviewer:30285071
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
cmake_vspar411:30581797
vsaa593:30376534
pythonvs932:30410667
cppdebug:30492333
vsclangdf:30486550
c4g48928:30535728
dsvsc012cf:30540253
azure-dev_surveyone:30548225
pyindex848:30577860
nodejswelcome1cf:30587006
2e4cg342:30602488
pyind779cf:30647092
89544117:30613380
fim-prod:30623723
Possible steps to reproduce: (not tried, just describing what I did immediately before the error appeared again)
- Create a class with a field and a constructor, assigning the field in the constructor (e.g. a service with an injected
ILogger) - Create a new empty .cs file
- In the new file, type the empty definition of a new class
- Copy the field and constructor from the first class into the second file (now the constructor name is wrong)
- Manually correct the constructor method name to the second class name
The field cannot be used in the second class because the code analyser is confused and still thinks that we're in the old class. The field name would be defined twice which is nonsense. After reloading the window, the problem disappears.
I am also encountering this issue, with the NCalc Library - specifically the EvaluateFunction event in the Expression class. Diff for some code to peek at: https://github.com/kd8lvt/RimWorld-Math/commit/89070c59d8c6a156df8bf22f631692289bf14b1e#diff-249900a5f6507cd777e5382e9f42e7f28d62c512261bb4cedc1abaee8ed0d8f6 And NCalc's Expression class: https://github.com/ncalc/ncalc/blob/master/src/NCalc/Expression.cs
In my case this issue occurs when I open a razor pages project, the .cshtml gives me this errors "Ambiguity between X and X filea...
I also have this error for .razor files in a Blazor Server project. I can't use the C# DevKit (since I am working on a commercial project) so I have "Prefer C# Extension" checked. When I check "Use Omnisharp" and reload VS Code the error is gone, but Omnisharp has its problems with razor files (like missing autocomplete for properties and methods).
Is there anything I could provide to narrow down the cause of the problem?
This appears repeatedly in the C# Extension logs when I have a razor page open
[Error - 10:56:17] [LanguageServerHost] System.UriFormatException: Failed create URI from 'Microsoft.NET.Sdk.Razor.SourceGenerators/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Pages_MyPage_razor.g.cs'; original string: 'Microsoft.NET.Sdk.Razor.SourceGenerators/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Pages_MyPage_razor.g.cs'
---> System.UriFormatException: Invalid URI: The format of the URI could not be determined.
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.CreateAbsoluteUri(String absolutePath) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 186
--- End of inner exception stack trace ---
at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.CreateAbsoluteUri(String absolutePath) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 186
at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.<TextSpanToLocationAsync>g__ConvertTextSpanToLocation|30_0(Document document, TextSpan span, Boolean isStale, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 477
at Microsoft.CodeAnalysis.LanguageServer.ProtocolConversions.TextSpanToLocationAsync(Document document, TextSpan textSpan, Boolean isStale, Nullable`1 context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Extensions/ProtocolConversions.cs:line 442
at Microsoft.CodeAnalysis.LanguageServer.Handler.FindUsagesLSPContext.ComputeLocationAsync(Nullable`1 documentSpan, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/References/FindUsagesLSPContext.cs:line 209
at Microsoft.CodeAnalysis.LanguageServer.Handler.FindUsagesLSPContext.GenerateVSReferenceItemAsync(Int32 definitionId, Int32 id, Nullable`1 documentSpan, ImmutableDictionary`2 properties, ClassifiedTextElement definitionText, Glyph definitionGlyph, Nullable`1 symbolUsageInfo, Boolean isWrittenTo, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/References/FindUsagesLSPContext.cs:line 194
at Microsoft.CodeAnalysis.LanguageServer.Handler.FindUsagesLSPContext.OnReferenceFoundAsync(SourceReferenceItem reference, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/References/FindUsagesLSPContext.cs:line 167
at Microsoft.CodeAnalysis.FindUsages.AbstractFindUsagesService.FindReferencesProgressAdapter.OnReferenceFoundAsync(SymbolGroup group, ISymbol definition, ReferenceLocation location, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/FindUsages/AbstractFindUsagesService.ProgressAdapter.cs:line 123
at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.<>c__DisplayClass20_0.<<ProcessDocumentAsync>g__ProcessDocumentAsync|0>d.MoveNext() in /_/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs:line 298
--- End of stack trace from previous location ---
at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.ProcessDocumentAsync(Document document, MetadataUnifyingSymbolHashSet symbols, Dictionary`2 symbolToGlobalAliases, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs:line 274
at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.ProcessDocumentAsync(Document document, MetadataUnifyingSymbolHashSet symbols, Dictionary`2 symbolToGlobalAliases, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs:line 279
at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.ProcessProjectAsync(Project project, ImmutableArray`1 allSymbols, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs:line 219
at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.ProcessProjectAsync(Project project, ImmutableArray`1 allSymbols, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs:line 231
at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.FindReferencesAsync(ImmutableArray`1 symbols, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs:line 126
at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.FindReferencesAsync(ImmutableArray`1 symbols, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs:line 126
at Microsoft.CodeAnalysis.FindSymbols.FindReferencesSearchEngine.FindReferencesAsync(ImmutableArray`1 symbols, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/FindSymbols/FindReferences/FindReferencesSearchEngine.cs:line 130
at Microsoft.CodeAnalysis.FindSymbols.SymbolFinder.FindReferencesAsync(ISymbol symbol, Solution solution, IStreamingFindReferencesProgress progress, IImmutableSet`1 documents, FindReferencesSearchOptions options, CancellationToken cancellationToken) in /_/src/Workspaces/Core/Portable/FindSymbols/SymbolFinder_FindReferences_Current.cs:line 64
at Microsoft.CodeAnalysis.FindUsages.AbstractFindUsagesService.FindReferencesAsync(IFindUsagesContext context, ISymbol symbol, Project project, FindReferencesSearchOptions options, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/FindUsages/AbstractFindUsagesService_FindReferences.cs:line 154
at Microsoft.CodeAnalysis.FindUsages.AbstractFindUsagesService.FindSymbolReferencesAsync(IFindUsagesContext context, ISymbol symbol, Project project, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/FindUsages/AbstractFindUsagesService_FindReferences.cs:line 125
at Microsoft.CodeAnalysis.FindUsages.AbstractFindUsagesService.FindSymbolReferencesAsync(IFindUsagesContext context, Document document, Int32 position, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/FindUsages/AbstractFindUsagesService_FindReferences.cs:line 103
at Microsoft.CodeAnalysis.FindUsages.AbstractFindUsagesService.FindLiteralOrSymbolReferencesAsync(IFindUsagesContext context, Document document, Int32 position, CancellationToken cancellationToken) in /_/src/Features/Core/Portable/FindUsages/AbstractFindUsagesService_FindReferences.cs:line 65
at Microsoft.CodeAnalysis.LanguageServer.Handler.FindAllReferencesHandler.HandleRequestAsync(ReferenceParams referenceParams, RequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Protocol/Handler/References/FindAllReferencesHandler.cs:line 68
at Microsoft.CommonLanguageServerProtocol.Framework.QueueItem`3.StartRequestAsync(TRequestContext context, CancellationToken cancellationToken) in /_/src/Features/LanguageServer/Microsoft.CommonLanguageServerProtocol.Framework/QueueItem.cs:line 176
[Error - 10:56:17] Request textDocument/references failed.
Message: Failed create URI from 'Microsoft.NET.Sdk.Razor.SourceGenerators/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Pages_MyPage_razor.g.cs'; original string: 'Microsoft.NET.Sdk.Razor.SourceGenerators/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Pages_MyPage_razor.g.cs'
Code: -32000
[object Object]
I am also receiving a notification multiple times
Edit: I am using C# Extension v2.10.28 with VS Code 1.84.2 on Fedora 39
@Kampfmoehre , @enzonun I'm having the same issue. I think they are working on this. Someone in this thread suggested installing an older version (2.6.24) of the C# extension. Just did this myself and it seems to have corrected the problem for now.
interesting, I also have the problem where Go to definition does not work sometimes but I don't use any of the other framreworks mentioned there or in #6426 , instead I am using ASP.NET Core default DI framework.
@Kampfmoehre @tannmatter Indeed that issue would be a duplicate of #6426. It'll impact any use of an API that's using source generators, and Razor can be one of those in some scenarios. As best I can tell it's unrelated to the original conversation though.
I encountered the same issue with the latest C# version installed (ms-dotnettools.csharp v2.15.30).
Downgrading it to 2.6.24 resolved it, but I didn't want to stay on an old version. Considering that others had mentioned code generated files, I cleaned out any build artefacts (via git clean -xfd), re-installed the latest C# extension and reloaded vscode. That also resolved the issue.
Upvoting and also hoping this get resolved (can't believe this issue has been open for one year considering its impact on editing any Razor pages). And I also wonder why installing the closed-sourced C# Dev Kit resolved this issue. Can C# Dev Kit impact how Rosyln works?