AL0999 error "An item with the same key has already been added"
Describe the bug It seems that the CodeLens or CodeAnalyzer crashes with duplicate key somehow when multiple projects have been opened from single workspace. The following errors are thrown:
Processing of message 'textDocument/hover' failed with error: 'An item with the same key has already been added. Key: Microsoft.Dynamics.Nav.CodeAnalysis.SymbolReferenceSpecification'
Details:
System.ArgumentException: An item with the same key has already been added. Key: Microsoft.Dynamics.Nav.CodeAnalysis.SymbolReferenceSpecification
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at Microsoft.Dynamics.Nav.CodeAnalysis.ProjectReferencesReferenceManager.AddModuleSymbols(PooledDictionary`2 moduleSymbols, IEnumerable`1 symbols) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\ReferenceLoader\ProjectReferencesReferenceManager.cs:line 185
at Microsoft.Dynamics.Nav.CodeAnalysis.ProjectReferencesReferenceManager.GetLoadedModules() in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\ReferenceLoader\ProjectReferencesReferenceManager.cs:line 74
at Microsoft.Dynamics.Nav.CodeAnalysis.Compilation.get_CompiledModule() in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\Compilation.cs:line 145
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.BinderFactoryVisitor.VisitCompilationUnitInternal(CompilationUnitSyntax compilationUnit) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 476
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.BinderFactoryVisitor.VisitSyntaxNode[T](SyntaxNode node, String name, Func`4 binderCreator, SymbolKind kind) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 314
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.BinderFactoryVisitor.VisitApplicationObject(SymbolKind kind, ApplicationObjectSyntax node) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 329
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.BinderFactoryVisitor.VisitMethodOrTriggerDeclaration(MethodOrTriggerDeclarationSyntax methodDecl, String methodName) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 291
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.GetBinder(SyntaxNode node, Int32 position) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 87
at Microsoft.Dynamics.Nav.CodeAnalysis.MemberSemanticModelFactory.CreateMemberModel(SyntaxNode node) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\MemberSemanticModelFactory.cs:line 77
at System.Collections.Immutable.ImmutableInterlocked.GetOrAdd[TKey,TValue](ImmutableDictionary`2& location, TKey key, Func`2 valueFactory)
at Microsoft.Dynamics.Nav.CodeAnalysis.SyntaxTreeSemanticModel.GetOrAddModelIfContains(SyntaxNode node, TextSpan span) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SyntaxTreeSemanticModel.cs:line 545
at Microsoft.Dynamics.Nav.CodeAnalysis.SyntaxTreeSemanticModel.GetMemberModel(SyntaxNode node) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SyntaxTreeSemanticModel.cs:line 502
at Microsoft.Dynamics.Nav.CodeAnalysis.SyntaxTreeSemanticModel.GetSymbolInfoWorker(SyntaxNode node, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SyntaxTreeSemanticModel.cs:line 85
at Microsoft.Dynamics.Nav.CodeAnalysis.SemanticModel.GetSymbolInfo(ExpressionSyntax expression, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SemanticModel.cs:line 565
at Microsoft.Dynamics.Nav.CodeAnalysis.SemanticModel.GetSymbolInfoFromNode(SyntaxNode node, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SemanticModel.cs:line 400
at Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.DocumentExtensions.GetSymbolFromNode(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\Shared\Extensions\DocumentExtensions.cs:line 360
at Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.DocumentExtensions.GetSymbolAtPositionAsync(Document document, Int32 position, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\Shared\Extensions\DocumentExtensions.cs:line 202
at Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.DocumentExtensions.GetSymbolAtPositionAsync(Document document, LinePosition symbolPosition, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\Shared\Extensions\DocumentExtensions.cs:line 183
at Microsoft.Dynamics.Nav.EditorServices.Protocol.LanguageServer.HoverRequestHandler.HandleAsync(HoverRequest request, Int32 requestId, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\LanguageServer\HoverRequestHandler.cs:line 99
at Microsoft.Dynamics.Nav.EditorServices.Protocol.MessageProtocol.RequestHandlerBase`1.HandleAsync(JToken requestContents, Int32 requestId, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\MessageProtocol\RequestHandlerBase.cs:line 65
at Microsoft.Dynamics.Nav.EditorServices.Protocol.RequestRegistry.Process(Message message) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\Endpoints\RequestRegistry.cs:line 80
[Error - 17:38:31] Please report this issue to https://github.com/microsoft/al/issues including information on how to reproduce it, if possible.
Processing of message 'al/gotodefinition' failed with error: 'An item with the same key has already been added. Key: Microsoft.Dynamics.Nav.CodeAnalysis.SymbolReferenceSpecification'
Details:
System.ArgumentException: An item with the same key has already been added. Key: Microsoft.Dynamics.Nav.CodeAnalysis.SymbolReferenceSpecification
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at Microsoft.Dynamics.Nav.CodeAnalysis.ProjectReferencesReferenceManager.AddModuleSymbols(PooledDictionary`2 moduleSymbols, IEnumerable`1 symbols) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\ReferenceLoader\ProjectReferencesReferenceManager.cs:line 185
at Microsoft.Dynamics.Nav.CodeAnalysis.ProjectReferencesReferenceManager.GetLoadedModules() in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\ReferenceLoader\ProjectReferencesReferenceManager.cs:line 74
at Microsoft.Dynamics.Nav.CodeAnalysis.Compilation.get_CompiledModule() in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\Compilation.cs:line 145
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.BinderFactoryVisitor.VisitCompilationUnitInternal(CompilationUnitSyntax compilationUnit) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 476
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.BinderFactoryVisitor.VisitSyntaxNode[T](SyntaxNode node, String name, Func`4 binderCreator, SymbolKind kind) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 314
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.BinderFactoryVisitor.VisitApplicationObject(SymbolKind kind, ApplicationObjectSyntax node) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 329
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.BinderFactoryVisitor.VisitMethodOrTriggerDeclaration(MethodOrTriggerDeclarationSyntax methodDecl, String methodName) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 291
at Microsoft.Dynamics.Nav.CodeAnalysis.BinderFactory.GetBinder(SyntaxNode node, Int32 position) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Binder\BinderFactory.cs:line 87
at Microsoft.Dynamics.Nav.CodeAnalysis.MemberSemanticModelFactory.CreateMemberModel(SyntaxNode node) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\MemberSemanticModelFactory.cs:line 77
at System.Collections.Immutable.ImmutableInterlocked.GetOrAdd[TKey,TValue](ImmutableDictionary`2& location, TKey key, Func`2 valueFactory)
at Microsoft.Dynamics.Nav.CodeAnalysis.SyntaxTreeSemanticModel.GetOrAddModelIfContains(SyntaxNode node, TextSpan span) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SyntaxTreeSemanticModel.cs:line 545
at Microsoft.Dynamics.Nav.CodeAnalysis.SyntaxTreeSemanticModel.GetMemberModel(SyntaxNode node) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SyntaxTreeSemanticModel.cs:line 502
at Microsoft.Dynamics.Nav.CodeAnalysis.SyntaxTreeSemanticModel.GetSymbolInfoWorker(SyntaxNode node, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SyntaxTreeSemanticModel.cs:line 85
at Microsoft.Dynamics.Nav.CodeAnalysis.SemanticModel.GetSymbolInfo(ExpressionSyntax expression, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SemanticModel.cs:line 565
at Microsoft.Dynamics.Nav.CodeAnalysis.SemanticModel.GetSymbolInfoFromNode(SyntaxNode node, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\SemanticModel.cs:line 400
at Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.DocumentExtensions.GetSymbolFromNode(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\Shared\Extensions\DocumentExtensions.cs:line 360
at Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.DocumentExtensions.GetSymbolAtPositionAsync(Document document, Int32 position, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\Shared\Extensions\DocumentExtensions.cs:line 202
at Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces.DocumentExtensions.GetSymbolAtPositionAsync(Document document, LinePosition symbolPosition, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\Shared\Extensions\DocumentExtensions.cs:line 183
at Microsoft.Dynamics.Nav.EditorServices.Protocol.LanguageServer.Extensions.AlDefinitionRequestHandler.ProcessRequestAsync(AlDefinitionRequest request, Int32 requestId, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\LanguageServer\Extensions\AlDefinitionRequestHandler.cs:line 71
at Microsoft.Dynamics.Nav.EditorServices.Protocol.LanguageServer.Extensions.NavServerRequestHandler`2.HandleAsync(T request, Int32 requestId, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\LanguageServer\Extensions\NavServerRequestHandler.cs:line 77
at Microsoft.Dynamics.Nav.EditorServices.Protocol.MessageProtocol.RequestHandlerBase`1.HandleAsync(JToken requestContents, Int32 requestId, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\MessageProtocol\RequestHandlerBase.cs:line 65
at Microsoft.Dynamics.Nav.EditorServices.Protocol.RequestRegistry.Process(Message message) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\Endpoints\RequestRegistry.cs:line 80
To Reproduce It is reproduceable on our project, but I would not know the way how to reproduce it manually by creating a sample.
Expected behavior I would like to know exactly what duplicate key has produced that error so I could fix it. This generic error does not give any clues to what is wrong. The project compiles OK but the CodeLens and Goto References is not working (usage of procedurs... etc)
Screenshots
5. Versions:
- AL Language: v6.0.344420
- Business Central: 16.3
We would need a repro though.
We would need a repro though.
As I have said, the only repro I could do is to send you our 3 apps and 3 test projects and vscode workspace for them to get the error. Which is something I cannot do.
However, I assume that the issue could be that there are somewhere two objects with the same name (as code or in symbols) and the same function, field or whatever else that key/value pair is being filled with. The only issue that I have that that code does not have a proper error handling of an exception with a meaningful error not just a generic exception. That would help a lot to determining the "collision" of the key(s). That is because we have multiple projects, some dependable on other project and test projects all in one workspace. All projects work OK when being opened one by one and not in a workspace. Also, they compile OK even when they are all in that single workspace. But the VScode does not display references then and goto definition functions does not work correctly.
What would be a way for me to manually track down problematic duplicate key issue - if there is any? Since there are multiple symbol files (Base App, Test Libraries, our 3 apps and 3 test apps...) - a lot of symbols to be loaded I presume.
Best, Zoran M.
Have you tried having one packageCachePath for the whole workspace, so that everything is resolved from that folder, rather then having individual packageCachePath paths per project
Hi Kálmán,
Yes, we do have one workspace with 6 projects (3 apps and 3 test apps) with single .alpackage folder (and al.codeAnalyzers and al.ruleSetPath are configured the same).
We have narrowed down that the issue has something to do with the "propagateDependencies" property and "application" property in app.json.
So the constellation is something like this: AppA - generic app for general use used by other apps AppB - real App that has dependancy on AppA TestAppB - test App for AppB that has dependancy on AppB and TestLibraries
So the issue is happening when: AppA - has propagateDependencies = true and "application" set to 16.0.0.0 AppB - has propagateDependencies = true and "application" set to 16.0.0.0 TestAppB - has Dependancy only to AppB and "application" set to 16.0.0.0
If we remove "application" property from AppB and TestAppB then we cannot compile these apps (although we would expect that the propagation of the Base Application would occur from AppA and propagateDependencies. This does not happen.
So, we return "application" property to 16.0.0.0 in AppB and TestAppB. Afterwards we remove propagateDependencies from apps AppA and AppB and set the "dependencies" in AppB - to AppA TestAppB - to AppA, AppB and TestLibraries
Then the issue stops occuring with CodeAnalyzer and the references pop-up again.
After every change of the app.json property you have to rebuild and republish the chain of apps and redownload symbols, reloading window etc..., so it is a painstaking work.
There is an AppC and TestAppC but that is not relevant to reproduce the issue.
Best, Zoran M.
Thanks for a a possible repro. The republishing, redownloading symbols, and mostly reloading windows. Is this because of the bug, i would assume, since normally P2P publishing should take care of that. Symbols in P2P scenarios are not resolved from the symbol cache, but from the referenced project. Looks like there is bug here in the propagateDependencies scenario.
Hi @kalberes, Hi @plagijat,
we got nearly the same error, BUT we could reproduce it in a simple project. Just follow the steps. Attached you will find the project in a 7zip file.
- Unzip the project.
- Open the workspace
- Download symbols for ALProject1.
- Build ALProject1.
- Note: We would have expect that on ALProject1.app file is in the .alpackage folder of ALProject2. It's not due to version conflict (see below...)
- Publish ALProject1.
- Download symbols for ALProject2.
- Try to build ALProject2: error.
Microsoft (R) AL Compiler version 6.1.5.31404
Copyright (C) Microsoft Corporation. All rights reserved
Compilation started for project '<TestApp/> Test' containing '1' files at '14:35:44.615'.
error AL0999: Internal error: System.ArgumentException: An item with the same key has already been added. Key: Microsoft.Dynamics.Nav.CodeAnalysis.SymbolReferenceSpecification
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at Microsoft.Dynamics.Nav.CodeAnalysis.ProjectReferencesReferenceManager.AddModuleSymbols(PooledDictionary`2 moduleSymbols, IEnumerable`1 symbols) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\ReferenceLoader\ProjectReferencesReferenceManager.cs:line 185
at Microsoft.Dynamics.Nav.CodeAnalysis.ProjectReferencesReferenceManager.GetLoadedModules() in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\ReferenceLoader\ProjectReferencesReferenceManager.cs:line 66
at Microsoft.Dynamics.Nav.CodeAnalysis.Compilation.get_CompiledModule() in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\Compilation.cs:line 139
at Microsoft.Dynamics.Nav.CodeAnalysis.Compilation.GetSourceDeclarationDiagnostics(SyntaxTree syntaxTree, Nullable`1 filterSpanWithinTree, Func`4 locationFilterOpt, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\Compilation.cs:line 1032
at Microsoft.Dynamics.Nav.CodeAnalysis.Compilation.GetDiagnostics(CompilationStage stage, Boolean includeEarlierStages, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Compilation\Compilation.cs:line 683
Compilation ended at '14:35:44.618'.
Error: The package could not be created.
A colleague showed me that sometimes the error is shown directly in the problems tab of vscode

Probably important information. If you set your user settings as follows, the error is thrown during the compilation and no .app file is generated.
"al.enableCodeAnalysis": false,
"al.backgroundCodeAnalysis": false,
If you delete this settings, you will receive the error in the problem tab, but the project creates an .app file.
As a workaround, we could simply set version of our dependency in ALProject2 to 1.0.0.1. This makes sure that ALProject1.app file is generated in the .alpackage folder. Then the error will not occur during the ALProject2 build process. We would have expected that the version in the dependency is just a minimum version for multiroot workspaces as it is for "Download symbols".
I hope it helps to speed up the troubleshooting.
AL Language Extensions: v6.1.359074 Version: DE Business Central 16.5 (Plattform 16.0.15884.18153 + Anwendung 16.5.15897.18165)
Hello, I totally dont know if it makes sense, but I had exactly same error with my workspace, mutiroot with deoendencies between projects. After enabling background code analysis, code actions, code analysis, incremental build, poof, it started to build correclty. Before I was not able to build any extension which has a dependency to any other extension I've created.
I have been fighting with this issue as well, and found a solution.
In my case, I had a multiroot workspace with several extensions. What solved it for me was the dependencies in app.json. 3 extensions (not actual names): Basic, Advanced and Very Advanced Advanced build on Basic and 'Very Advanced' had a dependency on BOTH Basic and Advanced (using functionality from both). Removing the Basic dependency from 'Very Advanced' solved my issue. This was for Business Central 17CU0. Maybe related to PropagateDependencies
Edit: Sadly this only worked for a couple of days. Have not been able to isolate the issue yet.
I suspect this has to do more with project structure than the actual code in the projects. Could you reproduce it as it is, remove your IP, and then try to reproduce it again?
I suspect this has to do more with project structure than the actual code in the projects. Could you reproduce it as it is, remove your IP, and then try to reproduce it again?
Hi Alex,
I could reproduced the error in my post. A project is attached in https://github.com/microsoft/AL/issues/6206#issuecomment-721127359 .
Any news on this issue? We're facing the same issue and need a solution!
A similar, if not the same issue has been accepted as Request For Hotfix. That means that it will have higher priority and a fix will start for it in the days to come.
Hi kalberes. Nice to know! Could you provide a link to that request?
This is internal information that were projected by Hot Fix Requests asked by customers. I cannot provide a link here.
This fix has been released to the marketplace. You can find the fix in version 7.1.441584 or later of the AL language extension.