103

I feel guilty asking a question like this around here, but I'm at a loss and would appreciate some help.

A proof-of-concept like web application was built on one PC and put up on a repo to download on another PC at a different location. There was originally an auto-build feature set up where Azure would build and publish automatically on check-in, but that was removed. Things were working on both ends until one side included a bulk of excluded changes. Now I'm seeing the following error:

The "Microsoft.CodeAnalysis.BuildTasks.Csc" task could not be
loaded from the assembly ...\packages\Microsoft.Net.Compilers.1.0.0\build..\tools\Microsoft.Build.Tasks.CodeAnalysis.dll. Could not load file or assembly 'file:///...\packages\Microsoft.Net.Compilers.1.0.0\tools\Microsoft.Build.Tasks.CodeAnalysis.dll' or one of its dependencies. The system cannot find the file specified. Confirm that the declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.

Does anyone have any suggestions on where to begin looking for the issue?

1

21 Answers 21

123

It turns out that NuGet packages were committed to the repository and breaking everything. Deleting the project\project\packages directory from the repo solved all build problems since NuGet fetches the packages automatically on build.

5
  • 1
    Make sure you include the actual packages folder in VSTS, as well as the contents ... I did just the contents of packages, and got the error
    – SteveC
    Commented Feb 8, 2017 at 14:44
  • 2
    I have nuget packages committed on all of my projects. Only problem is when you want to add them to source control, the *.dll files are "excluded items" by default, so you only commit empty directories instead of your nugets. That's why even restoring nuget doesn't help, because the directories are in place, so nuget thinks everything is allright. Commented Aug 15, 2017 at 14:09
  • 1
    Similar thread solution can be found here kx.cloudingenium.com/programming/net/… Commented Jan 10, 2018 at 9:49
  • 1
    I don't know why this worked... What's the reason? Commented Nov 7, 2018 at 16:05
  • This worked for me, but it seems like Microsoft could do a better job at things like this! Commented Apr 24, 2023 at 20:09
73

I got this error when I created a new branch for my project.

It drove me crazy for an hour. I tried most of the suggestions over the internet including the accepted answer to this question.

I then closed the project, opened it again, cleaned it and the error is gone. So this means this could be cache issue.

Anyways, just wanted to share.

1
  • 7
    Thanks! For me, first it was the package folder. Then, I still had the error, but restarting VS worked :)
    – dsnunez
    Commented Oct 25, 2016 at 18:15
39

I too tried the top answer with no luck so deleted the contents of my bin and packages dir, closed and re-opened VS and everything fine now.

1
  • 2
    This one helped me. I tried running an existing ASP.NET project in VS 2015 in Parallels and it was giving me the error above. Removing 'bin' and 'packages' folders, restarting VS and a clean build did the trick!
    – A3mercury
    Commented Jun 3, 2016 at 14:07
20

I have tried all solutions described before, but none worked.

What solved it for me, was to update the Microsoft.Net.Compilers from the NuGet Package Manager

2
  • 3
    I had the opposite problem. I was too optimistic and downloaded the the latest, RC version. When I reverted to the latest stable version it was ok.
    – Cool Blue
    Commented Jan 27, 2017 at 13:59
  • 2
    I upgraded from 2.8 to 3.1.1 and it fixed my problem. Thanks! Commented Jun 18, 2019 at 20:38
19
  • Right-click on your solution.
  • Go to Manage Nuget Packages.
  • Search for Microsoft.Net.Compilers.
  • Install or update on dependent projects as necessary.
1
  • 2
    Might be worth noting that this package is now deprecated. use Microsoft.Net.Compilers.Tools instead Commented Sep 6, 2021 at 11:11
7

Re-build, clean solution and restart Visual Studio worked for me.

2
  • 2
    Yes, same here, however just restarting VS wasn't enough - I had to restart my computer, then it came good.
    – Billious
    Commented Mar 22, 2018 at 5:05
  • 1
    Restarting my computer did the trick. Not sure if I needed to do all the cleans and reinstalls of the nuget packages. Commented Nov 5, 2018 at 22:45
6

Deleting the package and cleaning the solution solved it for me.

6

Use the below step:

1) Delete the package folder.
2) close the visual studio.
3) open the project and rebuild the project.

5

Deleting these three directories solves the problem.

  • /packages
  • /bin
  • /obj

NOTE: delete both /bin and /obj from all projects included in the solution (including Test projects).

0
4

Problem hides on TFS, you need to remove folder TestProject...\packages from TFS, check in, delete it from your local dir and build again. Worked!

4

I delete all from packages folder and rebuild solution. It's worked for me.

3

In my case: this works for me.

It turns out my teammate had already started looking into Windows 10 development and had Microsoft Build Tools 2015 installed on his machine.

I installed the software from https://www.microsoft.com/en-us/download/details.aspx?id=48159 and the problem was solved.

2

In my case, the solution was:

Use Windows Explorer and navigate to the offending path: C:\MyApplication\Code\Main\ABCProject\ABCProject.UI\Bin

Right click on bin folder > select properties > Uncheck ReadOnly.

0
2

In my case, the solution was:

  1. Right Click on Solution.
  2. Go to Manage Nuget Packages for this Solution.
  3. Search for Microsoft.CodeDom.Providers.DotNetCompilerPlatform.
  4. Uninstall the searched Package.
  5. Restart the Visual Studio.
1

I just tried this on a clean install of a Windows 10 machine and the issue for me ended up being that I didn't have the .NET 3.5 framework installed. This stackoverflow question helps explain why.

Cannot build WIX project on windows 10

1

Right Click on Solution. Go to Manage Nuget Packages for this Solution. Search forMicrosoft.Net.Compilers on Browse Section.

1

Deleting the bin folder worked for me

1

In vs2017 community there appeared a new item in the "build" menu. It disappeared after I used it and was called something like "Optimise project build packages". I clicked it and it fixed everything, just restart etc. I did it on two machines.

What it did was removed Microsoft.net.compilers 2.10.0 and replaced with Microsoft.CodeDom.Providers.DotNetCompilerPlatform 2.0.1

So there you go - more automagic...

1

My project was built with .Net Core 2.2 but I had .Net core 3.0 preview installed. I uninstalled .Net Core 3.0 from my system and went through all my class libraries, removing Microsoft.Net.Compilers 3.0, then rebuilt and it worked.

1

I moved my solution from one drive to another, and one of the files could not be copied because "in used", for which I click ignored, producing the error described in this post. Copying the missing file manually fixed it.

file: Microsoft.Build.Tasks.CodeAnalysis.dll Destination directory: packages\Microsoft.Net.Compilers.2.1.0\tools

1

For me I was trying to open a MVC5 project in VS 2013 and I was getting this error, Opened it in VS 2017 and up it worked just fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.