1040
votes

I am trying to run Asp.net MVC project retrieved from TFS source control. I have added all assembly references and I am able to build and compile successfully without any error or warning.

But I get the following error in the browser:

Could not find a part of the path 'C:\B8akWorkspace\B8akProject\B8akSolution\B8AK.Portal\bin\roslyn\csc.exe'.

Here is a full screenshot of the error page.

enter image description here

After few days of research, I understood that Roslyn is .Net compiler platform that offers advance compiling features. However, I do not understand why my build is trying to find \bin\roslyn\csc.exe because I did not configure anything related to Roslyn nor I intend to use Roslyn in my project.

30

30 Answers

545
votes

The problem with the default VS2015 templates is that the compiler isn't actually copied to the tfr\bin\roslyn\ directory, but rather the {outdir}\roslyn\ directory

Add this code in your .csproj file:

<Target Name="CopyRoslynFiles" AfterTargets="AfterBuild" Condition="!$(Disable_CopyWebApplication) And '$(OutDir)' != '$(OutputPath)'">
    <ItemGroup>
      <RoslynFiles Include="$(CscToolPath)\*" />
    </ItemGroup>
    <MakeDir Directories="$(WebProjectOutputDir)\bin\roslyn" />
    <Copy SourceFiles="@(RoslynFiles)" DestinationFolder="$(WebProjectOutputDir)\bin\roslyn" SkipUnchangedFiles="true" Retries="$(CopyRetryCount)" RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)" />
</Target>
1552
votes

TL; DR

run this in the Package Manager Console:

Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r

More information

This problem is not related to Visual Studio itself, so answers suggesting adding build steps to copy files over are rather a workaround. Same with adding compiler binaries manually to the project.

The Roslyn compiler comes from a NuGet package and there is/was a bug in some versions of that package (I don't know exactly which ones). The solution is to reinstall/upgrade that package to a bug-free version. Originally before I wrote the answer back in 2015 I fixed it by installing following packages at specific versions:

  • Microsoft.Net.Compilers 1.1.1
  • Microsoft.CodeDom.Providers.DotNetCompilerPlatform 1.0.1

Then I looked into .csproj and made sure that the paths to packages are correct (in my case ..\..\packages\*.*) inside tags <ImportProject> on top and in <Target> with name "EnsureNuGetPackageBuildImports" on the bottom. This is on MVC 5 and .NET Framework 4.5.2.

188
votes

Your build is trying to find \bin\roslyn\csc.exe because the following packages have been added to your project. Just review your packages.config file, you can have both of them there

Microsoft.CodeDom.Providers.DotNetCompilerPlatform
Microsoft.Net.Compilers

What is Roslyn and Who added them(packages) in the project : If you’re using .net Framework 4.5.2 to create projects using VS2015, you might have noticed that the project templates use Roslyn by default. Actually, Roslyn is one of open-source compilers for .NET languages from Microsoft.

Why should we delete Roslyn : If your project has Roslyn references and you are interested to deploy it on server, you will get unwanted errors on the website as many hosting providers still have not upgraded their servers and hence do not support Roslyn. To resolve this issue, you will need to remove the Roslyn compiler from the project template.

if you are not interested in using Roslyn, follow steps bellow to delete it

1. Remove NuGet packages, use the following commands from Nuget Package Console

PM> Uninstall-package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
PM> Uninstall-package Microsoft.Net.Compilers

2. After you do this, your web.config file should be auto-updated. In case it is not, look for the below code in web.config file and if it is found, delete this piece of code.

<system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"></compiler>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"></compiler>
    </compilers>
</system.codedom>
186
votes

A clean and rebuild worked for me!

61
votes

Here is a more MSBuild way of doing this.

<Target Name="CopyRoslynFiles" AfterTargets="AfterBuild" Condition="!$(Disable_CopyWebApplication) And '$(OutDir)' != '$(OutputPath)'">
    <ItemGroup>
      <RoslynFiles Include="$(CscToolPath)\*" />
    </ItemGroup>
    <MakeDir Directories="$(WebProjectOutputDir)\bin\roslyn" />
    <Copy SourceFiles="@(RoslynFiles)" DestinationFolder="$(WebProjectOutputDir)\bin\roslyn" SkipUnchangedFiles="true" Retries="$(CopyRetryCount)" RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)" />
</Target>

But I notice that the roslyn files are also in my bin directory (not in a folder). The app seems to work, though.

46
votes

Too late for an answer but still posting incase it helps anyone.
Following the below steps fixed the error for me:

  1. delete packages folder
  2. open VS
  3. rebuild
  4. observe that NuGet packages are restored, but bin\roslyn isnt created
  5. unload project
  6. reload project
  7. rebuild
  8. observe that the bin\roslyn has been created now.
44
votes

As noted in an issue in the Roslyn project on GitHub, a solution (that worked for me) is to simply unload and reload the project in Visual Studio.

The "bin\roslyn" folder wasn't created on build or rebuild until I reloaded the project.

32
votes

I followed these steps and it worked perfectly

  • Delete all the bin and obj folders
  • Clean solution and rebuild
  • Run this command in powershell

Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r

26
votes

I was also having same issue while running the project. Here are the steps that I followed.

  1. Right click in solution
  2. select Clean solution
  3. After clean succeeded,Again build your project
  4. Run the project again

This time I didn't see the same error. This works as expected.

23
votes

After trying all of the fixes with no cigar I fixed it by updating this Nuget Package in Visual Studios:

Microsoft.CodeDom.Providers.DotNetCompilerPlatform

Mine was from 1.0.0 to 2.0.0 for reference (The error no longer shows)

22
votes
  1. Clean Solution
  2. Rebuild Solution ,These two steps worked for me.
18
votes

As already noted by the currently highest voted answer, the quick fix is to use the package manager, Tools > Nuget Package Manager > Package Manager Console, to run

Update-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform -r

Packet Manager Console - how to open


Here is code that reproduces the error:
http://henke.atwebpages.com/SrvrErr-reproduce.zipfile 1
(Originally from https://github.com/aspnet/AspNetDocs/tree/master/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client/sample/server/ProductsApp)

Consider trying the example code provided in the zip file above.
If no changes are made, Ctrl+F5 will reproduce the error.

Server Error '/' in Application


A more robust solution

An alternative solution is to remove an attribute from the project's Web.config file.
(Web.config is in the same directory as the .csproj file.)
This will automatically and silently recreate your packages if they are missing.

Open the Web.config file in a text editor or in Visual Studio.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings></appSettings>
  ...
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

In the tag configuration > system.codedom > compilers > compiler language="c#;cs;csharp", completely remove the type attribute. – In short, remove the line that starts with type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, . 2

Visual Studio will take care of the rest. – No more Server Error in '/' Application.


In the example provided above, hitting Ctrl+F5 will now result in an HTTP Error 403.

HTTP Error 403.14 - Forbidden

Try replacing http://localhost:64195 in your web browser with http://localhost:64195/api/products. The web API now displays correctly:

A web API containing products

As a provocation, I tried removing the whole package directory from the Visual Studio project.
It was automatically and silently recreated as soon as the project was rebuilt.


1 Please rename the extension from zipfile to zip.
2 Presumably, the same fix works for Visual Basic as well as for C#, but I haven't tried it.

16
votes

For VS 2019 remove the following node completely:

<system.codedom>
</system.codedom>
15
votes

NuGet Package Manager

You need to install Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix, was especially created for that error

15
votes

In my case, before trying any of the other solutions, I switched to a "Release" configuration, rebuilt (the folder got created) and then switched back to "Debug", while the folder remained intact.

This was a checkout from source control of an older solution and apparently the original (automatic) package restore and building the project didn't create that folder in the bin directory.

Note that at time of writing this, the blamed component has reached v.2.

14
votes
  • Right click on your project and select Manage Nuget Packages
  • Find "Microsoft.CodeDom.Providers.DotNetCompilerPlatform"
  • Simply Update to an older or newer version (doesn't matter which), and then update again back to your original version.

This re-installs all the dependencies and files of the package (like csc.exe)

Nuget - DotNetCompilerPlatform

12
votes

Updating nuget packages worked for me Right click on the solution > Manage NuGet packages for solution and update all the packages and specially: Microsoft.Net.Compilers and Microsoft.CodeDom.Providers.DotNetCompilerPlatform

11
votes

So, Rob Cannon's answer essentially worked for me, but I had to tweak a handful of the options. Specifically, I had to remove the condition on the target, as well as change the Include attribute, as $CscToolPath was empty when the project was being built on our build server. Curiously, $CscToolPath was NOT empty when running locally.

<Target Name="CopyRoslynFiles" AfterTargets="AfterBuild" >
  <ItemGroup>
    <RoslynFiles Include="$(SolutionDir)packages\Microsoft.Net.Compilers.1.1.1\tools\*" />
  </ItemGroup>
  <MakeDir Directories="$(WebProjectOutputDir)\bin\roslyn" />
  <Copy SourceFiles="@(RoslynFiles)" DestinationFolder="$(WebProjectOutputDir)\bin\roslyn" SkipUnchangedFiles="true" Retries="$(CopyRetryCount)" RetryDelayMilliseconds="$(CopyRetryDelayMilliseconds)" />
</Target>
11
votes

This is a known issue with Microsoft.CodeDom.Providers.DotNetCompilerPlatform 1.0.6. Downgrading to 1.0.5 fixed this for me.

9
votes

Per a comment by Daniel Neel above :

version 1.0.3 of the Microsoft.CodeDom.Providers.DotNetCompilerPlatform Nuget package works for me, but version 1.0.6 causes the error in this question

Downgrading to 1.0.3 resolved this issue for me.

9
votes

In my case I have had issue in Jenkins when it tried to deploying it in Octopus with following error:

MSBUILD : OctoPack error OCT-1676060969: Failed to build the path for '\bin\roslyn\csc.exe' relative to 'T:\workspace\machine.engine\Machine.engine.Test': Invalid URI: The format of the URI could not be determined.. See the inner exception for more details. [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969: System.Exception: Failed to build the path for '\bin\roslyn\csc.exe' relative to 'T:\workspace\machine.engine\Machine.engine.Test': Invalid URI: The format of the URI could not be determined.. See the inner exception for more details. ---> System.UriFormatException: Invalid URI: The format of the URI could not be determined. [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind) [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at System.Uri..ctor(String uriString) [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at OctoPack.Tasks.Util.OctopusPhysicalFileSystem.GetPathRelativeTo(String fullPath, String relativeTo) in Z:\buildAgent\workDir\20ba9f2e0d5e4022\source\OctoPack.Tasks\Util\OctopusPhysicalFileSystem.cs:line 211 [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    --- End of inner exception stack trace --- [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at OctoPack.Tasks.Util.OctopusPhysicalFileSystem.GetPathRelativeTo(String fullPath, String relativeTo) in Z:\buildAgent\workDir\20ba9f2e0d5e4022\source\OctoPack.Tasks\Util\OctopusPhysicalFileSystem.cs:line 224 [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at OctoPack.Tasks.CreateOctoPackPackage.AddFiles(XContainer nuSpec, IEnumerable`1 sourceFiles, String sourceBaseDirectory, String targetDirectory, String relativeTo) in Z:\buildAgent\workDir\20ba9f2e0d5e4022\source\OctoPack.Tasks\CreateOctoPackPackage.cs:line 443 [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
MSBUILD : OctoPack error OCT-1676060969:    at OctoPack.Tasks.CreateOctoPackPackage.Execute() in Z:\buildAgent\workDir\20ba9f2e0d5e4022\source\OctoPack.Tasks\CreateOctoPackPackage.cs:line 190 [T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj]
Done Building Project "T:\workspace\machine.engine\Machine.engine.Test\Machine.engine.Test.csproj" (default targets) -- FAILED

Cause

After spending some time, I was using an internal developed component that was using Microsoft.Net.Compilers. The reason the internal component was using Microsoft.Net.Compilers was to overcome this issue (C#: throw invalid expression compilation) and was solved this way (How to use C# 7 with Visual Studio 2015?). This result in, when I installed the component on the main program, the Microsoft.Net.Compilers get added it selves automatically.

Solution

My work around was, uninstall following from our internal component by (following @malikKhalil answer)

PM> Uninstall-package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
PM> Uninstall-package Microsoft.Net.Compilers

And chose C# 7 compiler in Jenkins instead of C# 6 and rebuild, this is to ensure everything is working and building correctly.

Than finally in my main program I tried to update my internal component. And everything than build again. It has built without any problems or issues.

8
votes

In my case I just needed to go to the bin directory in Visual Studio Solution Explorer (web application project) and include the roslyn project directly. By right clicking the folder and selecting Include In Project. And check in the solution again to trigger the build process.

The roslyn folder was not included by default.

8
votes

Upgrading Microsoft.CodeDom.Providers.DotNetCompilerPlatform from 1.0.0 to 1.0.1 fixed this for me.

7
votes

In my case by just Deleting everything inside the bin folder and recompiling did all the work for me.

6
votes

If you were adding ASPNETCOMPILER to compile your Razor views in MVC, like in this StackOverflow question, then change PhysicalPath to place where Roslyn nuget package is located (usually pointed via $CscToolPath variable):

<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
<AspNetCompiler VirtualPath="temp" PhysicalPath="$(CscToolPath)" />

6
votes

Open the project file and remove all references with Import Project="..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.0....

Open web.config and remove all system.codedom compilers attributes

6
votes

The problem with the default VS2015 templates is that the compiler isn't actually copied to the {outdir}_PublishedWebsites\tfr\bin\roslyn\ directory, but rather the {outdir}\roslyn\ directory. This is likely different from your local environment since AppHarbor builds apps using an output directory instead of building the solution "in-place".

To fix it, add the following towards end of .csproj file right after xml block <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">...</Target>

<PropertyGroup>
  <PostBuildEvent>
    if not exist "$(WebProjectOutputDir)\bin\Roslyn" md "$(WebProjectOutputDir)\bin\Roslyn"
    start /MIN xcopy /s /y /R "$(OutDir)roslyn\*.*" "$(WebProjectOutputDir)\bin\Roslyn"
  </PostBuildEvent>
</PropertyGroup>

Reference: https://support.appharbor.com/discussions/problems/78633-cant-build-aspnet-mvc-project-generated-from-vstudio-2015-enterprise

5
votes

In my case, similar to Basim, there was a NuGet package that was telling the compiler we needed C# 6, which we didn't.

We had to remove the NuGet package Microsoft.CodeDom.Providers.DotNetCompilerPlatform which then removed:

  1. <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" /> from the packages.config file
  2. <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" /> </compilers> </system.codedom>

In the system.codedom node, you can see why it was bringing in roslyn: compilerOptions="/langversion:6

5
votes

Delete the Bin folder in your solution explorer and Build the solution again. That would solve the problem

5
votes

I had the same problem when installing my application on the server when everything worked perfectly on localhost.

None of these solutions woorked, I always had the same error:

Could not find a part of the path 'C:\inetpub\wwwroot\myApp\bin\roslyn\csc.exe'

I ended up doing this:

  • on my setup project, right clic, view > file system
  • create a bin/roslyn folder
  • select add > files and add all files from packages\Microsoft.Net.Compilers.1.3.2\tools

This solved my problem.