Know Which compiler is used in visual studio 2015? [duplicate] - c#

I've a bit of confusion about roslyn.
What I have done:
I've installed vs 2015 community edition and download in extensibilty > download compiler platform sdk.
So I created a simple console application: hello world example.
Well now I'm expect to choise the c# compiler between the vs2015 default one and roslyn..., but I've not found such option.
So my first question is: how to select version of c# compiler?
Second I've downloaded master-roslyn and I build, then I found csc.exe, well the odd things is that if I lauch the exe
I get c# compiler version 42.42.42.42. ???? Right?
Then I've follow some tutorials, but all purpose me:
to load a source from text file or string vars and analyze or change syntax tree, then compile to var.
Well at this point I'm confused... So:
What is roslyn exactly? A meta compiler? This mean that I can change my code at runtime just like Reflection?
Second: how can compile with vs2015 with default csc or choose roslyn?
third: If I build a custom version of roslyn How can I compile my source using Vs2015 ?
Which know if csc.exe is roslyn? No help or command line print the codename.
Thanks

So it looks like you've got a few questions:
What is Roslyn?
Roslyn is the new default compiler inside of Visual Studio 2015. If you're building and running applications within Visual Studio 2015, they're being compiled with the Roslyn compiler. You'll get to take advantage of all the new C# 6 features that are available only within the new compiler.
If you're using VS2015, Roslyn has replaced the old compiler entirely and as far as I know you can't use the old compiler within VS 2015.
Roslyn is also a platform that allows you to build programs that can modify, interpret and understand other programs. It's not really meant to let you write code that modifies itself (although that's probably possible to a degree).
The common use cases for Roslyn are:
Building Code Analyzers that provide errors and warnings within Visual Studio.
Building extensions for Visual Studio that understand source code.
Building other tools that understand or run source code. Example: ScriptCS - Scripting with C# code.
In order to use Roslyn for these purposes, you pull down the Microsoft.CodeAnalysis packages from NuGet. You can use these packages to parse code, analyze syntax trees, analyze symbols or compile code and emit IL.
If you're interested in learning more about Roslyn, I've started a series called Learn Roslyn Now that you might be interested in.
Can I replace the compiler?
Yes you can, but I'm not convinced this is a great idea outside of testing changes you want to contribute back to Roslyn. You can pull down Roslyn from GitHub and follow these instructions to build and run Roslyn from within Visual Studio.
If you follow those instructions, you'll be able to run the Roslyn project with F5. It will start a new instance of Visual Studio that's using your customized compiler. This is how people outside of Microsoft will contribute features to the compiler from now on. (Previously you couldn't deploy your custom compiler to Visual Studio but they fixed that in Visual Studio Update 1).

Roslyn is two things:
An API that lets you see "compiler things" like syntax trees and symbols.
A new csc.exe that is implemented atop #1.
If you want to make changes to the compiler and use that to build, take a look at these instructions if you haven't already. There's a few different ways you can make your own version of csc.exe and then use that to build something. But there's no "choice" dialog like you're looking for.

Roslyn is the default compiler of Visual Studio 2015. So, if you install VS2015 you´re already using Roslyn.
Roslyn is a codename for .NET Compiler Platform, and it provides open-source C# and Visual Basic compilers. The project is available on github.

Related

C# source code compilation from command line (any cases, without Visual Studio)

Dear Community. I have faced on the case when is needed to compile C# source code without Visual Studio usage in order to build it in DLL library and then include this package into other platforms (not only Windows-based). After a little bit of investigation CMake as a build tool has been chosen and appropriate configuration file (CMakeLists.txt) created. During it's running using native command prompt there is a mistake of compilation - "C# is currently only supported for Microsoft Visual Studio 2010 and later". That's why I would like to ask you share your knowledge and provide any additional cases to build it (some additional tools or whole scenarios) such as I'm a brand new in this topic and can't proceed before that wall (; . Thank you in advance.

Now that VS2015 is out, what's a supported way to modify Roslyn, with debugging support?

The Context
We'd like to modify Roslyn and be able to debug it while compiling with it. Pre-VS2015 release, doing this was a painful process that didn't flow very well.
Our goal is to develop a C# variant compiler.
The Dream
Pre-VS2015, executing and debugging your modded Roslyn required the opening of a second VS IDE (experimental) set to use your modded Roslyn. This process wasn't straight forward to setup properly, and oftentimes would break your VS2015 installation.
Post-VS2015, is there a better setup and process possible to modify and debug Roslyn?
I have installed Visual Studio 2015 but it looks like I need more required bits. After that I'm unsure how to run the tests and try the changes in VS2015.
We have our current documented process of testing your own versions of Roslyn here. As long as you're on Visual Studio 2015 Update 1 or later (where we did all the work to support this), everything should work.
The executive summary of those instructions is if you now enlist into Roslyn, you can choose the "VisualStudioSetup" project and just hit F5 to run. That builds to .vsix files in your build directory you can also install. If you want to, there's a CompilerExtension project that produces a compiler you can build with.

Is there a simple way to make Visual Studio 2015 use a specific ToolsVersion?

When building a project or solution using a specific version of msbuild I can select an earlier .net toolchain by using the /toolsversion or /tv switch:
"C:\Program Files (x86)\MSBuild\14.0\bin\msbuild" /tv:12.0 amazing.sln
This Just Works for all versions of msbuild, and the version of csc.exe etc. is correctly chosen based on the above:
> "C:\Program Files (x86)\MSBuild\14.0\bin\msbuild" /tv:4.0 amazing.sln
...
CoreCompile:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Csc.exe ...
...
> "C:\Program Files (x86)\MSBuild\14.0\bin\msbuild" /tv:12.0 amazing.sln
...
CoreCompile:
C:\Program Files (x86)\MSBuild\12.0\bin\Csc.exe ...
...
If I don't specify /tv, then depending on which version of msbuild I'm using and a number of environment variables, I may get any of:
The ToolsVersion specified in the top-level element in the project file
The ToolsVersion corresponding to the version of msbuild.exe I'm using
A value from msbuild.exe.config
A value from the registry
(See the different versions of the Overriding ToolsVersion Settings page on MSDN).
So, in order to have builds that have consistent results on the build server and on my local machine, I use /tv when running msbuild.exe (in fact, this is enforced in a psake script, which also ensures it uses the corresponding version of msbuild.exe).
However I cannot use the /tv switch when building with Visual Studio. Instead, Visual Studio 2013 and up will use the .net toolchain that shipped with that version of Visual Studio unless:
The environment variable MSBUILDLEGACYDEFAULTTOOLSVERSION is set and...
...all the project files have the ToolsVersion attribute set to the version I want to use.
This is so baroque that I cannot believe anyone is actually doing it. My questions are thus:
Is anyone doing the MSBUILDLEGACYDEFAULTTOOLSVERSION thing?
If not, is there another way to make Visual Studio use a specific ToolsVersion short of using the version of Visual Studio that shipped with that ToolsVersion? Something that could be stored in version control (so in a project or some other settings file) would be ideal.
And lastly:
Should I even care? Given that each successive version of the C# compiler should be able to handle previous versions' input, and I can set the target .net framework and C# language level in the project file, is this enough to ensure repeatable builds?
(My prejudice is that I should care, since:
I want builds in the IDE and on the build server to be the same (of course)
I want to be able to use VS2015 (and future versions) because it's a better IDE than previous versions, but I don't want to be obliged to use the new toolchain until I decide to.
Perhaps I want too much...)
For a concrete example of the problem, please see my msbuild-vs-vs2015-toolsversion repository on github.
Some background: I'm asking this because we recently had a CI build error when one of my colleagues submitted C# 6.0 code that compiled fine with Roslyn on their copy of Visual Studio 2015, but failed in CI because that uses the previous release of the .net toolchain (they'd used an automatic property with no setter, which is fine in Roslyn but not in earlier versions). We will be updating the CI build to Roslyn, but I wanted to see if we could prevent this sort of thing happening in the future.
I solved this by writing a Visual Studio extension that temporarily sets the environment variable MSBUILDDEFAULTTOOLSVERSION for the duration of a build; the value to be used is read from a file .toolsversion in the same directory as the .sln file. The psake script reads the same .toolsversion file and passes the value to the /tv switch.
The code for the extension can be found here: https://github.com/guyboltonking/set-toolsversion-extension. Sadly, I'm not working with C++, or indeed with Visual Studio, at the moment, so I can't provide any support for it (but I can tell you I used it with no issues at all for several months).
Kudos to #efaruk for reminding me about the existence of MSBUILDDEFAULTTOOLSVERSION.
Edit: Thanks to #mbadawi23, it's now possible to use the extension with both VS2015 and VS2017.
To force a specific C# version in Visual Studio 2015, you can go into the project properties -> Build -> Advanced -> Language Version.
If you set this to 5, the compiler will complain about C# 6 features with: Feature '...' is not available in C# 5. Please use language version 6 or greater.
Alternativly ReSharper also has some tools for this.
Note: You can always create an msbuild file to build your project from using it or changing your project it self and you can decide your tool version conditionally (https://msdn.microsoft.com/en-us/library/7z253716.aspx) (.csproj is also a structured msbuild script with different extension and it will be also compatible with VS).
Regards...
Edit:
https://msdn.microsoft.com/en-us/library/bb383985.aspx
by setting the $(ProjectToolsVersion) property on a project within a solution. This lets you build a project in a solution with a Toolset version that differs from that of the other projects.
So, I think you have got your answer ;)
What you see within Visual Studio (the tools etc) and the code behind them are not what is included in the compiled data they are merely a visual/readable representation, when compiling them as an earlier version of VS you are making the executable of that version.
Please keep in mind that if compiling as a previous .NET version you will potentially lose functionality such as async functionality.

Compile-time extensions in C#

One of the possibilities demoed when the C# dev team was talking about porting the C# compiler to C# was the option to extend the compiler phase with custom plugins, written in C#.
Was that option included in the Visual Studio 2015 release, and if so, where can I find the MSDN page for it?

How to create a new language for use in Visual Studio

I want to write a new templating language, and I want Visual Studio to "support" it. What I need to know is:
How do I parse my new language?
Given some code in my new template language, how do I translate it into HTML? Right now I'm using regular expressions to parse it token by token, but I don't think this is going to scale very well as the language gets more complicated, and there's no error checking. I've heard of ANTLR but never used it. Would that be the right tool for this job, or is there perhaps something simpler? Ideally I'd like to send any syntax errors to the error window with as much information as possible (line #, type of error) like other languages do.
How do I create a new file type for Visual Studio?
How do I get syntax highlighting?
Can I use the same parser I created in step 1, or is this something entirely different?
How do I get Intellisense?
I'd prefer to write my parser in C#.
I would take a look at another language that has already done the legwork of integrating with Visual Studio. A great example is Boo. The language and Visual Studio integration are open source. So you can take a look at exactly what they had to do.
Boo Language: https://github.com/boo/boo-lang
Boo Syntax Highlighting for VS2010 (VSX add-in): http://vs2010boo.codeplex.com/
Boo Language Studio (syntax highlighting for VS2008): http://boolangstudio.codeplex.com/
The Boo Syntax Highlighting for VS2010 includes some recommended links on its homepage, which I'll copy for easy reference:
Nice article about "classification" (syntax highligting) in VS 2010: http://dotneteers.net/blogs/divedeeper/archive/2008/11/04/LearnVSXNowPart38.aspx
Examples for VSX add-ins: http://blogs.msdn.com/vsxteam/archive/2009/06/17/new-editor-samples-for-visual-studio-2010-beta-1.aspx
Regarding the Visual Studio aspects, what you need is a "language service", which is the entity that handles colorizing, intellisense, etc. for a given file extension/type.
For an intro, see this article
And for a code sample see here
Regarding parsing, there are lots of technologies, and I won't offer an opinion/advice.
Beware, there is a fair amount of work involved, although in my opinion it is much more straightforward in VS2010 than in previous versions of Visual Studio to provide this kind of extension.
See also
Visual Studio 2010 Extensibility, MPF and language services
I wrote a VS Language Service using this article as my basis:
http://www.codeproject.com/KB/recipes/VSLanguageService.aspx
It wasn't too bad if you have a basic handle on Grammars.
There is a sample in the VS SDK that shows most of the features you are looking for.
I was using VS with own language and desperately needed a syntax highlight. I built mine based on this tutorial: https://mattduffield.wordpress.com/2012/07/31/writing-a-brightscript-syntax-highlight-extension-for-visual-studio-2010/
I know the tutorial is in VS2010. I made mine in VS2012 with no or very small hiccups. (also worked in VS2013) Recently I changed to VS2015 and the solution can be edited, built with no problem.
I found this very useful collection of recent samples for Visual Studio 2013 SDK:
http://blogs.msdn.com/b/vsx/archive/2014/05/30/vs-2013-sdk-samples-released.aspx
It also contains the recent version of the OokLanguage which sounds promising.
We used ANTLR 4 to parse our language which works like a charm and allows direct interaction with C# code. Can totally recommend it.
As mentioned in other answers, the most interesting code sample is the Ook language extension for the latest version of Visual Studio (2017 at the time of writing).
For VS 2015 see the sample in the VS2015 branch.
In order to install the SDK for 2015 or later, you need to rerun the VS setup. In 2015 it's called "Visual Studio Extensibility Tools Update 3".

Categories