88

There are a bunch of related questions on this, though most of the answers define Roslyn and/or provide a "fix" to some issue (exe, with hosting providers, etc.)

What I can't seem to track down is the "why" and "what for" (perhaps only in the context of ASP.Net MVC/Web API) in /bin/roslyn.

I ran in to similar issues (hosting - .exe restrictions, support for 4.6, etc.) and my "fix" was to "just deploy to Azure" (of course everything works without a hitch). But really, this doesn't answer:

  • why are they needed?
  • does this mean that the they are used for runtime compilation (my brain points to this, but that is a complete guess/my perhaps wrong grok), as this SO post shows - unless corrected, this is "it" (more below).
  • it seems "removing the package" is a "fix" (based on some past answers), but if so, it (re)begs the question

I think understanding this will help - e.g. I can't be the only one who will have an eyebrow raised seeing an .exe "needed"....


Update

Goes to show that "hidden gems" exist :) I've read this over and over...after all it's been there for some time now - but not the comments thread - the original referenced link, circa 2014, has been redesigned by Microsoft and the comments are no longer displayed...luckily the relevant parts are below.

BIG mistake - it was staring at me all this time (or at least since this exchange):

Dmitry Dzygin 2 Jun 2015 12:53 AM

I have tried the latest version of the NuGet package, but there's seem to be a difference in the way the compiler is loaded/executed.

In the v0.2.0.0 the Roslyn compiler would be loaded into memory, improving greatly performance for not pre-compiled websites with multiple *.as*x/*.cshtml files. The new version, however, features a new /bin/roslyn/csc.exe file, which is executed once per file, completely removing the mentioned above optimization feature.....

Gold:

XMao 2 Jun 2015 1:22 PM

@Dmitry The job of the csc.exe in /bin/Roslyn is to invoke the VBCSCompiler.exe, which sits in the same folder. VBCSCompiler.exe is the process that does the actual compilation work. If the VBCSCompiler is already running csc.exe will reuse it and thus we will still gain the mentioned performance improvement.

Hth...


Update: 10/2017

Seems this is relevant after all this time so a further update.

The answer below by @Donny V is an option. By fully compiling your application, including all Views (.cshtml/.vbhtml), you wouldn't need that exe in your application.

This is true even if Visual Studio (to this day, VS 2017, confusingly) will still create the /bin/roslyn and it's contents in the Publish process, even if "full compile" is set.

You can test this by excluding the /bin/roslyn folder and it's contents when pushing your application to your hosting provider.

Caveat:

As mentioned, fully compiling your application means you'll have to recompile it, even for View level changes.

EdSF
  • 11,753
  • 6
  • 42
  • 83
  • Are you using Entity Framework? – Jeroen Vannevel Nov 16 '15 at 17:28
  • @JeroenVannevel - not _specifically_ at the moment, but the bits are there and ready... – EdSF Nov 16 '15 at 18:49
  • So EF is added as a reference to the project? Up until a very recent PR EF has a dependency on Roslyn. – Jeroen Vannevel Nov 16 '15 at 18:52
  • 1
    @JeroenVannevel yes, though if memory serves, it was from default scaffolding (unless I did a Nuget update to "latest" EF). Still I don't understand its relationship to "needing" an executable (`.exe`) in _deployment_ - perhaps just the libraries/dll but what needs to be _executed by an `exe` in deployment environment_ ? – EdSF Nov 17 '15 at 16:18
  • Did you ever find anything more out on this? I agree, it's extremely odd to require an executable in a deployment folder, and I'm trying to eliminate that – Peder Rice Apr 18 '16 at 14:27
  • 1
    @PederRice **This is subject to correction by any MS folk in SO**. If you have a hosting issue (restricts `exe`) you [could remove these packages in your project](http://stackoverflow.com/a/32295103/304683), **but** if you do, you need to debug your code - if you're using some new features or those with Roslyn dependencies, things will break. Unfortunately, a "safe" way is to revert to `4.5.x` - this is my _personal_ "fix" if a project **isn't** slated for Azure deployment. – EdSF Apr 18 '16 at 14:46
  • @EdSF, you have done a nice investigation, thanks. And you are pasted some comments that are saying that "it is required to do the compilation work for not pre-compiled websites". Did I correctly understand that? If so, then I can safely delete "roslyn" folder if my web site is fully compiled and not uses any functionality that executes code from string on the fly. – LaoR May 19 '16 at 08:20
  • @LaoR If memory serves, even if you _precompile_ the entire application (including `vb/cshtml` `Views` for example), the `exe` files in `/bin/Roslyn/` are still deployed/published. That was actually why I was confused - if there's nothing "left" to compile (the app is fully compiled), then why? I haven't tried just deleting, my "recipe" has been to [remove the associated packages](http://stackoverflow.com/a/32295103/304683). I rarely fully compile entire web app if it has a front end (it was an experiment), so that wouldn't be viable for me. Hth – EdSF May 19 '16 at 12:44
  • I'm trying to understand IF I need this Roslyn compiler or not. I have a .NET WebAPI2 project and it seems to include it by default. However, my CI process (using BitBucket Pipelines) uses MONO and it throws this error: " Error building target IncludeRoslynCompilerFilesToItemGroup: Item has already been added. Key in dictionary: 'Link' Key being added: 'Link'" . I remove them and my API project works fine (I am using Angular2, NOT MVC)). I don't fully understand WHY I would need it or if this is a good move... – Rodney Feb 12 '17 at 17:25

5 Answers5

36

This is taken from MSDN forum.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/442b100a-2b88-4ac4-b655-0c1345791f15/roslyn-cscexe-web-api-2-on-hosting-server?forum=msbuild

I have noticed a minor drawback to uninstalling this package:

https://www.nuget.org/packages/Microsoft.CodeDom.Providers.DotNetCompilerPlatform

Some of the new C# 6.0 language features if used in Views (MVC project) will not compile. Many of my views use the ?. null checking operator for accessing Model properties. All of these views now return errors on my Godaddy hosted MVC 5 application.

This error occurs because Views (by default) are compiled at runtime using the .NET pipeline (not pre-compiled).

To resolve this issue, simply uncheck the "Allow precompiled site to be updatable" option in your publish profile settings. This should pre-compile your views and allow your C# 6.0 (Latest version of Roslyn Compiler) to run like a champ.

Just wanted anyone looking at this question to know the ramification of uninstalling it and why its there in the first place

Donny V.
  • 22,248
  • 13
  • 65
  • 79
  • Thanks. That pretty much means the entire site is compiled (including front end `vb/cs/html` view files, so it may not be a workable situation for all (have to recompile entire site for "simple" front end changes that touch views). Also, last I "published", even with "full compile" switch on, it will still output the `exe` files in `/bin/roslyn` ..... – EdSF Jul 11 '16 at 15:04
  • 3
    Just wanted anyone looking at this question to know the ramification of uninstalling it and why its there in the first place. – Donny V. Jul 12 '16 at 15:21
  • if you do form postbacks with dynamic data, this will break your site. – John Lord Nov 01 '19 at 13:35
9

Was running into this issue all the time in Visual Studio 2017 Community Edition where the project could not be rebuilt because the older files in bin/roslyn could not be deleted. Based on the OP's Gold comment, I now keep the Task Manager open (Ctrl+Shift+Esc) and kill the VBCS.exe process. The offending files in bin/roslyn can now be deleted.

Manish
  • 1,726
  • 3
  • 23
  • 29
1

Another feature of it is that it does not require build servers to actually have compiler dependencies. You send the compiler you want to use WITH the code to the build server and it just uses exactly what you told it to.

Paul Swetz
  • 2,234
  • 1
  • 11
  • 28
  • 1
    Thanks though confused about this. The `exe` in `bin/roslyn` is created as part of "publishing" an Asp.Net app (_already_ compiled/built). – EdSF Jun 22 '16 at 05:45
  • 1
    The Roslyn components come into the source as well via reference and nugget packages so they can be used for the building, the stuff in bin I believe is used for anything JIT'd (Razor) and also to go from MSIL to native on the system running the application. – Paul Swetz Jun 22 '16 at 12:12
1

This release of Visual Studio contains a new version of C# & VB.net compilers code named “Roslyn”.

Roslyn is a complete rewrite of the C# and VB.net compilers with each written in their respective language for example the C# compiler is written in C# rather than C++. Roslyn is open source (Roslyn on GitHub) so you could even theoretically create your own version of C# or VB.net!

What would become Roslyn, was first mentioned way back in 2008 by Anders Hejlsberg at the PDC conference however it wasn’t until 2011 that the first preview was released.

You could refer to the following links to get more detailed information about your problem.

https://gooroo.io/GoorooTHINK/Article/16253/Visual-Studio-2015-and-Roslyn-Compiler/17944#.VmkfwjaheM8

https://visualstudiomagazine.com/articles/2012/03/20/10-questions-10-answers-on-roslyn.aspx

From: https://forums.asp.net/t/2079727.aspx?What+is+the+roslyn+folder+

Prime
  • 69
  • 2
  • 12
  • Thank you. I think the "what" was clear, it was the "why", and more specifically, _why would an executable (`exe`) be needed in a web application context, **even** when the web application was **fully compiled**_ - in other words, there is "nothing" left at runtime to compile (everything is already _pre-compiled_) - where discarding/excluding that entire roslyn folder is just "fine". – EdSF Feb 19 '19 at 17:23
0

Two things to note: 1) Removing it will "fix" the problem, but it "fix"es it by falling back to a built-in, old, legacy compiler that is not compatible with later language features.
2) Pre-compiling is not possible in many cases. How would you show information in a model if it's pre-compiled, and then show changes to that data? Any data that you rely on in partial views to refresh would never update.

Another point of interest is to make sure you are tracking the "build" directory in the roslyn's package. If you don't, it won't thrown an error but it will not compile your site when you try to load it. Fully tracked, it just works. We deployed it to other development systems without installing it on those systems first, and this is possible because it's a NuGet package.

John Lord
  • 1,941
  • 12
  • 27
  • Unless those features are critical, fallback isn't much of a problem really. – EdSF Nov 02 '19 at 07:06
  • As for #2, unsure what you mean - aside from the stated need to recompile for all source changes, there aren't any other issues. – EdSF Nov 02 '19 at 07:09
  • any language feature can be worked around. Examples that made us want to update ours would be the null operators ? and ??. It's cleaner to say object?.value ?? 0 vs if (object == null || object.value == null) 0 else object.value. – John Lord Nov 04 '19 at 18:10
  • as for #2, what i'm saying is you can't pre-compile anything that has dynamic data. Let's say i'm showing "driver information". How would i precompile that? I don't know in advance which driver it is. I suppose you could get the data afterwards with jquery but that is usually unnecessary. If your page has a model on it, you can't precompile. – John Lord Nov 04 '19 at 18:12
  • #2 Nope. There is no "functional" difference with (pre) compiling your entire application than not. All the functions that dynamically build your model (user input, etc) all run as is. – EdSF Nov 05 '19 at 01:08
  • End of day, only **static** changes in `View` (e.g. html) will need to be re-compiled as mentioned. Changes in `VB`/`C#` are a given (need to recompile). Lastly, client (browsers) have no concept of "pre-compiled" they'll get the same resources (html, css, js) in the `response`. – EdSF Nov 05 '19 at 01:11