Skip to content
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

Error AD0001: Could not load file or assembly 'NetFabric.CodeAnalysis, Version=4.1.0.0' #73

Open
robinvanpoppel opened this issue Aug 2, 2023 · 24 comments · Fixed by #76

Comments

@robinvanpoppel
Copy link
Contributor

When upgrading from 2.0.4 to 2.1.0 my builds start failing with multiple occurences of the following error message:
CSC : error AD0001: Analyzer 'NetFabric.Hyperlinq.Analyzer.AssignmentBoxingAnalyzer' threw an exception of type 'System.IO.FileNotFoundException' with message 'Could not load file or assembly 'NetFabric.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'. . Most of the projects are targeting net48.

@xtqqczze
Copy link
Contributor

xtqqczze commented Aug 2, 2023

@robinvanpoppel Could you try adding a package reference to NetFabric.CodeAnalysis and report whether the error still occurs?

Using .NET CLI, you can achieve this with the following command line:

dotnet add package NetFabric.CodeAnalysis --version 4.1.0

You may then need to run a restore:

dotnet restore

@xtqqczze
Copy link
Contributor

xtqqczze commented Aug 2, 2023

@aalmada Looks like the NuGet package is missing a dependency on NetFabric.CodeAnalysis.

@robinvanpoppel
Copy link
Contributor Author

@xtqqczze Sounds like that is causing this issue. Thanks for investigating!

@robinvanpoppel
Copy link
Contributor Author

Is a fix planned? Or can I help out by supplying a PR?

@xtqqczze
Copy link
Contributor

@robinvanpoppel Did adding a package reference to NetFabric.CodeAnalysis resolve the error?

@robinvanpoppel
Copy link
Contributor Author

robinvanpoppel commented Aug 22, 2023

I did not try that because you mentioned the nuget package was missing a dependency. I can of course try it as a workaround but since NetFabric.CodeAnalysis should be a dependency of the analyzers I am hesitant to have a direct dependency on this package myself as a permanent solution.

@DavidTCarpenters
Copy link

DavidTCarpenters commented Aug 31, 2023

+1 Adding a package reference to NetFabric.CodeAnalysis does not resolve the error, had to roll back to v2.0.4 to use the analyzer.

Looks like this commit is where the change occurred

5fbcf3d#diff-6745205804bf0b3ec6066749eb78fa56e335a98bae48d20cf48e12c6861dfb20

The package was moved to .Package.csproj and no longer has a reference to NetFabric.CodeAnalysis or the ILRepack section

@xtqqczze
Copy link
Contributor

+1 Adding a package reference to NetFabric.CodeAnalysis does not resolve the error, had to roll back to v2.0.4 to use the analyzer.

@DavidTCarpenters Did you make sure to dotnet restore after adding the reference?

@DavidTCarpenters
Copy link

DavidTCarpenters commented Aug 31, 2023

I did yeah @xtqqczze

@xtqqczze
Copy link
Contributor

xtqqczze commented Sep 5, 2023

cc: @aalmada

@aalmada
Copy link
Member

aalmada commented Sep 26, 2023

Hi all,
Sorry but I've been away from all my open-source projects.
NetFabric.CodeAnalysis contains code that can be useful for other developers and, having it as a separate projects, makes it more likely for others to contribute.
Unfortunately, dependencies in analyzers have been a mystery to me. I can't find documentation about it. The analyzer is placed in folder where there is no dependency versioning.
I first tried merging the assemblies to avoid this issue. Unfortunately, but both projects ILMerge and ILRepack are no longer maintained and I've been running into build issues.
Does any of you have a suggestion on how to solve dependencies for analyzers?

@aalmada
Copy link
Member

aalmada commented Oct 11, 2023

Hello,

I want to express my gratitude to @robinvanpoppel for contributing to pull request #76. This should hopefully resolve the dependency issue.

I've released a new version, available both on NuGet and the Visual Studio Marketplace.

During my sabbatical, I plan to allocate some of my time to enhance and expand the rules within this analyzer. Additionally, I've been sharing articles related to this on my LinkedIn profile: LinkedIn Articles.

I'm eager to hear about your experiences with the analyzer. For now, I'll keep this issue open. Please share your feedback.

@aalmada aalmada reopened this Oct 11, 2023
@DavidTCarpenters
Copy link

Hi all,

I've pulled the latest and I'm afraid the issue still appears to be present, with the following warning displayed:

warning AD0001: Analyzer 'NetFabric.Hyperlinq.Analyzer.NonDisposableEnumeratorAnalyzer' threw an exception of type 'System.TypeLoadException' with message 'Could not load type 'NetFabric.CodeAnalysis.Errors' from assembly 'NetFabric.CodeAnalysis, Version=5.1.0.0, Culture=neutral, PublicKeyToken=null'.'.

@aalmada
Copy link
Member

aalmada commented Oct 12, 2023

@DavidTCarpenters Thank you for the feedback

I'm going to give assembly merging another shot. Here's a compilation of assembly merger options I've come across:

  1. ILMerge
  2. ILRepack (formerly used in conjunction with ILRepack.MSBuild.Task, but can also be employed with ILRepack.Lib.MSBuild.Task)
  3. dnMerge
  4. Costura (note: patronage required for access)
  5. ILLink

@aalmada
Copy link
Member

aalmada commented Oct 12, 2023

In attempt #77, I experimented with dnMerge but encountered an issue where the resulting assembly still had dependencies on NetFabric.CodeAnalysis.

Subsequently, in attempt #78, I employed ILRepack.MSBuild.Task, which functioned as expected on my local environment. However, when integrated with GitHub actions, it failed to build successfully. I had previously reported this issue in the following GitHub repository: gluck/il-repack#273.

@aalmada
Copy link
Member

aalmada commented Oct 13, 2023

ILLink (trimming) appears to be the current "official" solution, with other alternatives either abandoned or in maintenance mode. I'm inclined to investigate this new solution further.

@xtqqczze
Copy link
Contributor

@aalmada Unfortunately trimming doesn't appear to work for projects targeting .NET Standard.

Additionally, starting in the .NET 8 SDK, trimming is unsupported for any version of .NET Standard or .NET Framework.

https://learn.microsoft.com/dotnet/core/compatibility/sdk/8.0/trimming-unsupported-targetframework

@aalmada
Copy link
Member

aalmada commented Oct 13, 2023

@xtqqczze Appreciate it! It's quite unfortunate, though, as I'm encountering problems with all the other options.

@xtqqczze
Copy link
Contributor

@aalmada Does anything like this work:

<PackageReference Include="NetFabric.CodeAnalysis" Version="4.1.0" PrivateAssets="all" Pack="true" />

See https://til.cazzulino.com/msbuild/how-to-include-package-reference-files-in-your-nuget-package

@aalmada
Copy link
Member

aalmada commented Oct 13, 2023

@xtqqczze, I'm grateful for your valuable suggestion!

In the context of NuGet packages, it's a common practice for them to extract their dependencies into the same directory as the executable. When it comes to analyzers, the executable in question is Roslyn, and it needs to locate the required assemblies. Inside the .csproj file, there is a target named _AddAnalyzersToOutput responsible for copying analyzer assemblies into the "analyzers/dotnet/cs" folder.

The pull request (PR) submitted by @robinvanpoppel aimed to install the dependency into this same folder. However, a significant concern arises in this scenario - there's no versioning for these assemblies. What if another analyzer relies on the same assembly but with a different version? That's why I created the NetFabric.CodeAnalysis package, designed to be reusable by other analyzers or source generators.

The solution to this versioning challenge comes in two parts. One approach involves copying the source code into the analyzer itself, although I'm currently hesitant to pursue this option. The alternative, which is more ideal, is to merge the assemblies. This would make the dependencies disappear while still maintaining the independence of the projects. The success of the second solution largely depends on the effectiveness of at least one of the assembly mergers.

At present, ILRepack is functioning smoothly on my local setup, and I can release a new package. However, this results in the loss of GitHub CI. I remain hopeful that either ILRepack or dnMerge will ultimately resolve this issue.

@aalmada
Copy link
Member

aalmada commented Oct 13, 2023

I have just released version 2.3.0, and I genuinely hope that this resolves the problem. I deeply apologize for any inconvenience this may have caused.

@DavidTCarpenters
Copy link

Hi Antão,

No need to apologise! Thank you for all of your work on this.

I have managed to get this to build locally and produce a package that I can reference in another project without using ILRepack. I'll fork the repo and submit a PR so that you can review, maybe this helps. I'll bump the version to 2.3.1

@DavidTCarpenters
Copy link

This really does feel like a dark art though the whole nuget distribution of analysers with dependencies.

DavidTCarpenters pushed a commit to DavidTCarpenters/NetFabric.Hyperlinq.Analyzer that referenced this issue Oct 13, 2023
Add a package reference to NetFrabric.CodeAnalysis - Gets the dll into the build
Include PackDependencies target - Adds the reference to the lib and analyzer
Add all output dlls as analyzer references
@DavidTCarpenters
Copy link

PR is here #80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants