dotnet / dotnet/msbuild

Microsoft.CodeAnalysis fails to load on `dotnet build` in a custom MSBuild task

Open
#3,290 2 comments 4 reactions 0 assignees View on GitHub
triaged
Dominant language
C#
Stars
5.5k
Forks
1.5k
Avg merge
1d 13h
Merged PRs (30d)
133

Description

moved from https://github.com/dotnet/roslyn/issues/26569 on behalf of @pbalaga

**Version Used**:
[Microsoft.CodeAnalysis 2.8.0](https://www.nuget.org/packages/Microsoft.CodeAnalysis/)

`dotnet --info`:
```
.NET Command Line Tools (2.1.105)

Product Information:
Version: 2.1.105
Commit SHA-1 hash: 141cc8d976

Runtime Environment:
OS Name: Windows
OS Version: 10.0.16299
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.1.105\

Microsoft .NET Core Shared Framework Host

Version : 2.0.7
Build : 2d61d0b043915bc948ebf98836fefe9ba942be11
```

**Steps to Reproduce**:

1. `git clone https://github.com/pbalaga/TaskWithCodeAnalysisFailure.git`
2. `cd TaskWithCodeAnalysisFailure\TaskConsumerProject`
3. `dotnet build`

Important points about how the above repro repository was built:
- TaskLibrary.csproj
* define a custom MsBuild task: `SampleTask`
* `true` in order to copy NuGet references to bin folder
- TaskConsumerProject.csproj
* invoke SampleTask from TaskLibrary's bin folder:

```

```

**Expected Behavior**:
Project should build without errors.

**Actual Behavior**:
Build fails with the below error:
>TaskConsumerProject\TaskConsumerProject.csproj(11,5): error MSB4018: System.IO.FileLoadException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.8.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621)

**Remarks**:
- the same project builds without errors in Visual Studio 2017 and via MsBuild CLI
- commenting out [the line](https://github.com/pbalaga/TaskWithCodeAnalysisFailure/blob/1f440e36f2ca0648d3b7624fefb03a173ea947d0/TaskLibrary/Task.cs#L11) containing Microsoft.CodeAnalysis usage makes the problem disappear
- all projects are targeting netstandard2.0, no multi-targeting (so not the same issue as https://github.com/dotnet/cli/issues/7510)
- I managed to attach a debugger to `SampleTask` before it throws an exception, but there is no more detail presented than in console output
- two additional dependencies were added to `SampleTask`: a project dependency and a NuGet dependency on a package different than Microsoft.CodeAnalysis to rule out possibility that this is a more general `dotnet`'s problem with dependency resolution. Only using Microsoft.CodeAnalysis causes the build to fail
- I tried other versions of Microsoft.CodeAnalysis, specifically: `2.8.0-beta4` and `2.7.0`, both of which produce exactly the same results

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.