After installing my analyzer (published nuget package) in a VS2015 RTM default Console Application, it works, but displays these two warnings after a solution rebuild:
An instance of analyzer CodeContractNullability.CodeContractItemNullabilityAnalyzer cannot be created from d:\Bart\Documents\Visual Studio 2015\Projects\ConsoleVersionTester\packages\ResharperCodeContractNullability.1.0.1\analyzers\dotnet\cs\CodeContractNullability.dll : Could not load file or assembly 'System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified..
An instance of analyzer CodeContractNullability.CodeContractNullabilityAnalyzer cannot be created from d:\Bart\Documents\Visual Studio 2015\Projects\ConsoleVersionTester\packages\ResharperCodeContractNullability.1.0.1\analyzers\dotnet\cs\CodeContractNullability.dll : Could not load file or assembly 'System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified..
The published vsix version does not produce these warnings.
Any idea what is causing this?
To repro, install the nuget package at https://www.nuget.org/packages/ResharperCodeContractNullability/1.0.1 in a new console application and rebuild.

What version is the analyzer referencing? Since this error is coming from build, can you check what version of Microsoft.CodeAnalysis.dll is present in your MSBuild directory and what version of System.Collections.Immutable it's referencing?
The analyzer references System.Collections.Immutable v1.1.37.
The version of Microsoft.CodeAnalysis.dll in "c:\Program Files (x86)\MSBuild\14.0\Bin\" is v1.0.0.50618. It references v1.1.36.0 of System.Collections.Immutable, which exists beside it.
Does that help to determine how I can fix this? I cannot remember if I updated some references, maybe should not have done that.
Could this be related to files exclusion in .nuspec? I only added the JetBrains.Annotations.dll, the rest was already there when creating the analyzer project.
@bkoelman The compilers use System.Collections.Immutable version 1.1.36, so that is what your analyzer will need to target. The packages.config for CodeContractNullability is wrong.
I suspect the analyzer loads in Visual Studio due to a binding redirect that tells the runtime to always use version 1.1.36.0 of System.Collections.Immutable _regardless of the version built against_. That will work so long as no-one depends on APIs in 1.1.37.0 that aren't in 1.1.36.0. We should probably be less aggressive and only redirect versions <1.1.36.0.
The NuSpec file exclusions are there to prevent an analyzer NuGet package from including binaries that will be provided by the host; again, this could lead to version mismatch problems.
@tmeschter Thanks, I downgraded the packages in my analyzer project and the warning is gone.
I was unaware that package upgrades could break analyzers.
@bkoelman - in general, all dependencies of an analyzer should be carried by the analyzer package. Some dependencies like S.C.Immutable which are loaded by VS are excluded from nuget packages and changing the version of those from the ones targeted by VS\csc will cause problems like this.
Sorry to add to a closed case but I think that I'm encountering the same issue and so didn't want to create a new one.
I've created a NuGet package with an analyser in it and it works when I add it to a fresh project on my home computer but not my work computer. Where it fails, it fails with the same error as reported at the start of this issue:
An instance of analyzer ProductiveRage.Immutable.Analyser.CtorSetCallAnalyzer cannot be created..
Could not load file or assembly 'System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified..
Looking in c:\Program Files (x86)\MSBuild\14.0\Bin\ on my home computer (where it works) I have System.Collections.Immutable.dll version 4.6.23124.0 and on my work computer (where it doesn't work) I have version 1.1.36.0.
Is this likely to be an artifact of an old installation on my work computer? Ideally, the NuGet package would work on any computer and pull in any dependency that it needs, but it's been stated here that
Some dependencies like S.C.Immutable which are loaded by VS are excluded from nuget packages
And so I'm not quite sure what to do. I don't really want other people to try downloading the package and finding that it doesn't work!
It's this package if that helps: https://www.nuget.org/packages/ProductiveRage.Immutable
I realised that my home PC had VS2015 Update 1 installed and my work one didn't. Installing this fixed it the issue.. but it leaves me a bit uneasy that people without Update 1 won't be able to use my NuGet package's analyser.
@ProductiveRage Right, VS2015 RTM ships with System.Collections.Immutable 1.1.36.0, but Update1 has 1.1.37.0. So if your analyzer is built against 1.1.37.0, you'll run into issues when VS2015 RTM tries to find it and fails. On the other hand, the reverse _will_ work: building against 1.1.36.0 and then loading in Update1, where only 1.1.37.0 is available. This is because Update1 has binding redirects in place.
To ensure that your analyzer works properly in VS2015 RTM and later (and csc.exe/vbc.exe v1.0 and later, for that matter) your analyzer should target the 1.0.0 versions of the Microsoft.CodeAnalysis.* NuGet packages. It is also possible that NuGet will try to be "helpful" anyway by automatically giving you a newer version of System.Collections.Immutable, in which case you'll need to downgrade.
Does that help?
It certainly might explain a few things!
Is this really going to be as easy as changing the packages.config in my Analyser project from
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.CodeAnalysis.Common" version="1.0.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.CodeAnalysis.CSharp" version="1.0.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="1.0.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.Composition" version="1.0.27" targetFramework="portable45-net45+win8" />
<package id="NuGet.CommandLine" version="2.8.5" targetFramework="portable45-net45+win8" />
<package id="System.Collections.Immutable" version="1.1.37" targetFramework="portable45-net45+win8" />
<package id="System.Reflection.Metadata" version="1.1.0" targetFramework="portable45-net45+win8" />
</packages>
to
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.CodeAnalysis.Analyzers" version="1.0.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.CodeAnalysis.Common" version="1.0.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.CodeAnalysis.CSharp" version="1.0.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="1.0.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.0" targetFramework="portable45-net45+win8" />
<package id="Microsoft.Composition" version="1.0.27" targetFramework="portable45-net45+win8" />
<package id="NuGet.CommandLine" version="2.8.5" targetFramework="portable45-net45+win8" />
<package id="System.Collections.Immutable" version="1.0.37" targetFramework="portable45-net45+win8" />
<package id="System.Reflection.Metadata" version="1.1.0" targetFramework="portable45-net45+win8" />
</packages>
?
Sorry, instead of asking questions I thought I'd try it out! I changed the packages.config files, I deleted the packages folders then rebuilt the solution to pull them all in fresh. I had to remove the CodeAnalysis 1.1.0 references from the affected projects manually and then reference the 1.0.0 binaries from the freshly-populated packages folder. But it seems to work.
It's introduced a warning
Analyzer 'Microsoft.CodeAnalysis.CSharp.Analyzers.FixAnalyzers.CSharpFixerWithFixAllAnalyzer' threw an exception of type 'System.TypeInitializationException' with message 'The type initializer for 'Microsoft.CodeAnalysis.Analyzers.FixAnalyzers.FixerWithFixAllAnalyzer`1' threw an exception.'.
But it sounds like this was an issue with the older binaries that was fixed in 1.1.0 (https://github.com/dotnet/roslyn/issues/7276). It's a pity that 1.1.0 seems to require VS 2015 Update 1!
My only concern now.. is there any way to test this? It seems to work fine on my Update 1 PC but I no longer have a VS2015RTM to try against. How confident should I be?
The changes that I made for this made be seen here: https://github.com/ProductiveRage/Bridge.Immutable/commit/239864a3ff262e0a089e7d8809ac3ea7de420cfe
In case they're of any use to anyone in the future.. or in case anyone fancies having a very quick look at them and giving it a LGTM?? :)
Thanks for your help!
@ProductiveRage If you're going to be developing using Update1, you can safely use Microsoft.CodeAnalysis.Analyzers version 1.1.0 even if all the other Microsoft.CodeAnalysis.* packages are at 1.0.0.
Despite the name, Microsoft.CodeAnalysis.Analyzers is really separate from the other packages. Your code doesn't build against the assemblies in Microsoft.CodeAnalysis.Analyzers, it is instead _analyzed by_ the analyzers in that package. That is, the other Microsoft.CodeAnalysis.* packages add Reference items to your project, but Microsoft.CodeAnalysis.Analyzers just adds Analyzer items.
Confused yet? ;-)
So, the only thing you should need to change is to bump System.Collections.Immutable down from 1.1.37 to 1.1.36.
As for that warning about the TypeInitializationException from CSharpFixerWithFixAllAnalyzer: yes, that was a bug in Microsoft.CodeAnalysis.Analyzers v1.0.0 that was fixed for v1.1.0. However, VS2015 RTM basically swallowed that exception, whereas Update1 now surfaces it as a warning. Again, if you're going to be using Update1 for development, just stick with v1.1.0 of Microsoft.CodeAnalysis.Analyzers.
I _think_ I follow, but I'm just going to make absolutely sure.. :)
I need to drop the Immutable collections library down a version so that people who are developing _using_ my analyser can do so if they only have the older version of that library on their computer. However, I don't need to drop the Microsoft.CodeAnalysis.Analyzers library's version for while I'm developing the analyser itself because that library is not referenced by the analyser - so someone on VS2015RTM could use my analyser just fine (but they might have problems cloning my analyser repo and working on _that_).
While I don't want to limit who can use the analyser that I will distribute, I don't mind if I limit who can actually _develop_ that analyser.
Correct me if I've got anything wrong there but I think I've got you. And I think I'm going to bump the ersion of Microsoft.CodeAnalysis.Analyzers back up to 1.1.0 to get rid of that annoying warning.
Thanks again!
@ProductiveRage You've got it exactly. :+1:
It all sounded so easy..
I bumped Microsoft.CodeAnalysis.Analyzers back to 1.1.0 but found a System.Collections.Immutable reference in a test project with 1.1.37, not that is 1.1.36 as well. There are no explicit references to Immutable 1.1.37 in my project. However, when I build I get two warnings still (one for each project with the Immutable Collections library) -
Found conflicts between different versions of the same dependent assembly that could not be resolved
Enabling more detailed logging, I can see the following:
Unified Dependency "System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". (TaskId:17)
Using this version instead of original version "1.1.36.0" in "C:\Users\Dan\Documents\GitHub\Bridge.Immutable\packages\Microsoft.CodeAnalysis.Common.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.dll" because AutoUnify is 'true'. (TaskId:17)
Using this version instead of original version "1.1.36.0" in "C:\Users\Dan\Documents\GitHub\Bridge.Immutable\packages\Microsoft.CodeAnalysis.CSharp.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.dll" because AutoUnify is 'true'. (TaskId:17)
Using this version instead of original version "1.1.36.0" in "C:\Users\Dan\Documents\GitHub\Bridge.Immutable\packages\Microsoft.CodeAnalysis.CSharp.Workspaces.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.CSharp.Workspaces.dll" because AutoUnify is 'true'. (TaskId:17)
Using this version instead of original version "1.1.36.0" in "C:\Users\Dan\Documents\GitHub\Bridge.Immutable\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0\lib\portable-net45+win8\Microsoft.CodeAnalysis.Workspaces.dll" because AutoUnify is 'true'. (TaskId:17)
Could not resolve this reference. Could not locate the assembly "System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. (TaskId:17)
.. and also this:
There was a conflict between "System.Collections.Immutable, Version=1.1.36.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and "System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". (TaskId:17)
"System.Collections.Immutable, Version=1.1.36.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and "System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not. (TaskId:17)
References which depend on "System.Collections.Immutable, Version=1.1.36.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [C:\Users\Dan\Documents\GitHub\Bridge.Immutable\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll]. (TaskId:17)
C:\Users\Dan\Documents\GitHub\Bridge.Immutable\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll (TaskId:17)
Project file item includes which caused reference "C:\Users\Dan\Documents\GitHub\Bridge.Immutable\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll". (TaskId:17)
System.Collections.Immutable (TaskId:17)
References which depend on "System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" []. (TaskId:17)
C:\Users\Dan\Documents\GitHub\Bridge.Immutable\packages\System.Reflection.Metadata.1.1.0\lib\portable-net45+win8\System.Reflection.Metadata.dll (TaskId:17)
So it looks like the System.Reflection.Metadata library at version 1.1.0 wants 1.1.37 of the immutable collections library. Looking at history on https://www.nuget.org/packages/System.Reflection.Metadata/ it looks like 1.0.21 was the last stable version that would use Immutable 1.1.36, so I've switched to using that and the warnings have now all gone.
Hopefully this was the most sensible thing to do.
Most helpful comment
@ProductiveRage Right, VS2015 RTM ships with System.Collections.Immutable 1.1.36.0, but Update1 has 1.1.37.0. So if your analyzer is built against 1.1.37.0, you'll run into issues when VS2015 RTM tries to find it and fails. On the other hand, the reverse _will_ work: building against 1.1.36.0 and then loading in Update1, where only 1.1.37.0 is available. This is because Update1 has binding redirects in place.
To ensure that your analyzer works properly in VS2015 RTM and later (and csc.exe/vbc.exe v1.0 and later, for that matter) your analyzer should target the 1.0.0 versions of the Microsoft.CodeAnalysis.* NuGet packages. It is also possible that NuGet will try to be "helpful" anyway by automatically giving you a newer version of System.Collections.Immutable, in which case you'll need to downgrade.
Does that help?