I am trying to run an ASP.NET MVC (model-view-controller) project retrieved from TFS (Team Foundation Server) 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.
https://i.stack.imgur.com/lkxJP.png
After few days of research, I understood that Roslyn is a .NET compiler platform that offers advanced 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 did I intend to use Roslyn in my project.
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.
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>
A clean and rebuild worked for me!
Copying file from "C:\Users\medmondson\Source\UK\Portal\Branches\v12\Source\packages\Microsoft.Net.Compilers.1.3.2\tools\csi.exe" to "bin\Debug\roslyn\csi.exe".
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=\"Web\" /optionInfer+"></compiler>
</compilers>
</system.codedom>
<system.codedom>
block from production web.config & my application starts running like a charm.
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.
Too late for an answer but still posting incase it helps anyone. Following the below steps fixed the error for me:
delete packages folder open VS rebuild observe that NuGet packages are restored, but bin\roslyn isnt created unload project reload project rebuild observe that the bin\roslyn has been created now.
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.
0. The quick fix
As already noted in 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
https://i.stack.imgur.com/fdWOj.png
1. Code to reproduce the error
Here is code that reproduces the error:
https://user.it.uu.se/%7Ehesc0353/SrvrErr-reproduce.zip
(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.
https://i.stack.imgur.com/u4Qc5.png
2. 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=\"Web\" /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,
. 1
Visual Studio will take care of the rest. – No more Server Error in '/' Application
.
3. HTTP Error 403
In the example provided above, hitting Ctrl+F5 will now result in an HTTP Error 403.
https://i.stack.imgur.com/UfSsT.png
Try replacing http://localhost:64195
in your web browser with http://localhost:64195/api/products
. The web API now displays correctly:
https://i.stack.imgur.com/Fd5oQ.png
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.
References
Project files that reproduce the error
The original project
1 Presumably, the same fix works for Visual Basic as well as for C#, but I haven't tried it.
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
I was also having same issue while running the project. Here are the steps that I followed.
Right click in solution select Clean solution After clean succeeded,Again build your project Run the project again
This time I didn't see the same error. This works as expected.
Clean Solution Rebuild Solution ,These two steps worked for me.
Ctrl C
when I was in the middle of checking out a branch in git
and it screwed my repo up. git reset --hard
didn't work, so I git clean -xdf
and had to rebuild the project. However I ran into this error so I simply cleaned and rebuilt the project again and it worked for me.
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)
roslyn
folder is created in my output path. I also see no "roslyn" reference in my csproj. It could be that Target Name="CopyRoslyn...
is a VS2015 thing and not necessary in (the version of) 2017 I have. Worth to note: Since I updated DotnetCompilerPlatform before I played around with adding a copying target (the one I mentioned) I have a cleaner csproj.
For VS 2019 remove the following node completely:
<system.codedom>
</system.codedom>
https://i.stack.imgur.com/gOv27.png
You need to install Microsoft.CodeDom.Providers.DotNetCompilerPlatform.BinFix, was especially created for that error
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)
https://i.stack.imgur.com/sDltU.png
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.
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
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>
This is a known issue with Microsoft.CodeDom.Providers.DotNetCompilerPlatform 1.0.6. Downgrading to 1.0.5 fixed this for me.
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.
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.
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.
Upgrading Microsoft.CodeDom.Providers.DotNetCompilerPlatform
from 1.0.0 to 1.0.1 fixed this for me.
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)" />
In my case by just Deleting everything inside the bin folder and recompiling did all the work for me.
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
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>
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:
In the system.codedom
node, you can see why it was bringing in roslyn: compilerOptions="/langversion:6
Delete the Bin folder in your solution explorer and Build the solution again. That would solve the problem
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.
Success story sharing