How do I integrate StyleCop 4.7 with Visual Studio 2013? - visual-studio-2013

I really want to use StyleCop with my Visual Studio 2013, but unfortunately it won't work. I've installed the 4.7 version from the official site, checking all options, both VS Studio integration and MSBuild integration, following the precise instructions (download, install while VS applications are closed, then start VS) but it just won't show up in my Visual Studio 2013.
The StyleCop website says that it should be compatible with VS2013. It won't show up anything related to StyleCop under tools and it won't show up the 'Run StyleCop' action when I right-click my C# project. Already tried the repair option from the installation menu and even re-installed it. Haven't made any progress.
Can someone please help me with this? Maybe one has had experience with this before? Thanks in advance!

Open Visual studio
Open Package Manager Console from TOOLS > LIBRARY PACKAGE MANAGER menu
Run the following command
install-package stylecop.msbuild
The above command will download the latest stable required dlls and files and integrate style cop with your project. Build your project and any stylecop errors will be shown in the warnings section.
If you don't find the package manager for some reason in the above menu, please refer this link for instructions on how to install http://surajdeshpande.wordpress.com/2013/10/18/how-to-install-a-nuget-package-in-visual-studio/
If you want your build to succeed only if all stylecop errors are fixed, you will need to make some changes to the project file to set a boolean to not treat stylecop errors as warnings.
Open the .csproj file for your project in notepad, and find the first PropertyGroup section within the file. Add a new tag to set the StyleCopTreatErrorsAsWarnings flag to false. For example:
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{4B4DB6AA-A021-4F95-92B7-B88B5B360228}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestProject</RootNamespace>
<AssemblyName>TestProject</AssemblyName>
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
</PropertyGroup>
A sample proj file content with <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings> is shown above.
The build will be successful only after all sylecop errors are fixed.

Previous answers seems to be deprecated in 2015..
For VS Community 2013 Update 5:
1) Install "JetBrains ReSharper Ultimate 2015.2 [En]"
2) Go to Resharper-> Extensions:
3) Install Resharper.Stylecop from there
Update: Ok, for ReSharper Ultimate 10.0.2 + StyleCop by JetBrains 4.8
I get this error: https://resharper-support.jetbrains.com/hc/en-us/community/posts/206009179-StyleCop-integration-with-ReSharper-10-x-VS2015-
But there is recommendation to ignore this error. Seems to work after restart VS
(I also have reSP installed, for Sharepoint code analysis)

I have integrated the stylecops 4.7 with Visual Studio 2013 (Web) using following steps.
In order to integrate StyleCop with a project, an <Import> needs to be added to the .csproj file (versions and paths might differ):
If you used the official installer and installed the 'MSBuild integration' component, your <Import> will look like this:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- Your StyleCop <Import> will go here -->
<Import Project="$(ProgramFiles)\MSBuild\StyleCop\v4.7\StyleCop.targets" />
Note: You need to add the above line in each and every project in your solution.
Copy Settings.StyleCop in the project solution folder from C:\Program Files\StyleCop 4.7 on solution root folder or add as a solution item.
Double click the Settings.StyleCop file as configure the rules. You should be able to use the stylecops in your project.

Related

msbuild: Check if Import was successful

I installed the MSBuild Community tasks to have the "Zip" task available. Everything works fine with the following code:
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets"/>
On machines where the Community Tasks aren't installed yet, Visual Studio (Enterprise 2017) entirely refuses to load the .csproj file containting the above Import statement.
Since the zip-related part is not an important step in the build process, I tried to make the .csproj also load on machines where the Community tasks are not yet installed, have msbuild issue a warning and build the zip-related targets using a condition. So I tried:
<PropertyGroup>
<MSBuildCommunityTargets>$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets</MSBuildCommunityTargets>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTargets)" Condition="Exists($(MSBuildCommunityTargets))" />
Here the Community Tasks targets file cannot be found. The expanded $(MSBuildExtensionsPath) path points to an MSBuild folder below the VS installation folder while the Community Tasks install into an MSBuild folder directly below "Program Files (x86)". I suspect the Import statement does some compatibility search magic when expanding the $(MSBuildExtensionsPath) variable while this magic is not applied in simple property variable expansions.
Is there any proper way to check the presence of some installed third-party MSBuild tasks/targets?
Thank you.
Is there any proper way to check the presence of some installed third-party MSBuild tasks/targets?
Your Visual Studio version must be 2017. That because the expanded $(MSBuildExtensionsPath) path points to an MSBuild installation folder. The default value for Visual Studio 2015 and before is: C:\Program Files (x86)\MSBuild,
However, it was changed to C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild for Visual Studio 2017. And the installation path of MSBuild.Community.Tasks.msi is still C:\Program Files (x86)\MSBuild.
So when you use $(MSBuildExtensionsPath) to specify the path of Community Tasks in Visual Studio 2017, you will get the error "the Community Tasks targets file cannot be found".
To resolve this issue, we could not use the variable $(MSBuildExtensionsPath) in Visual Studio 2017 before the author updates this MSI file. As a workaround, you could use the absolute path:
<PropertyGroup>
<MSBuildCommunityTargets>C:\Program Files (x86)\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets</MSBuildCommunityTargets>
</PropertyGroup>
<Import Project="$(MSBuildCommunityTargets)" Condition="Exists($(MSBuildCommunityTargets))" />
Or use $(MSBuildProgramFiles32) instead of $(MSBuildExtensionsPath):
<PropertyGroup>
<MSBuildCommunityTargets>$(MSBuildProgramFiles32)\MSBuild\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets</MSBuildCommunityTargets>
</PropertyGroup>
Alternatively, you can install the Community Tasks by NuGet package. Because The MSBuild Community Tasks library is also available on nuget.org via package name MSBuildTasks. To install MSBuildTasks, run the following command in the Package Manager Console:
Install-Package MSBuildTasks
After install this package, you can find below import in the project file:
<Import Project="..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets" Condition="Exists('..\packages\MSBuildTasks.1.5.0.235\build\MSBuildTasks.targets')" />
In this case, you do not need to modify the project file any more, and it will not be bound by the variable $(MSBuildExtensionsPath). This is what we recommend.
Is there any proper way to check the presence of some installed third-party MSBuild tasks/targets?
Can't you use the condition? Has worked for me over the years
<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" Condition="Exists('$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets')" />

Can not configure which Typescript compiler to use in VS 2015

I am installing Microsoft.TypeScript.MsBuild 2.0.3 NuGet package. This comes with the appropriate tsc.exe in its tools subfolder.
After installing the nuget package my .csproj file contains the line:
<Import Project="..\..\lib\Microsoft.TypeScript.MSBuild.2.0.3\build\Microsoft.TypeScript.MSBuild.props" Condition="Exists('..\..\lib\Microsoft.TypeScript.MSBuild.2.0.3\build\Microsoft.TypeScript.MSBuild.props')" />
Note: the ..\..\lib folder is correct, that is my package folder.
It seems all correct (except the minor trap, that Microsoft.TypeScript.targets file shipped with the package contains invalid vstsc parameter (output folder).
This gives build error, which proves that this msbuild task is in effect. After correcting this annoying bug (which will arise always when refreshing packages) build is successful.
However...
Using SysInternals processexplorer I see that not the installed (NuGet) tsc.exe is called, instead this one:
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.8\tsc.exe
Wby... ?. How to configure my project (preferably with NuGet) to use the tsc.exe I want?
All I did to create above mess is...
Installing visual studio 2017 while working on visual studio 2015.
And I stopped the visual studio 2017 installation as I thought I would do it in another day.
So the setup application has removed my installed typescript plugin. All you have to do is
install type script for Visual Studio 2015. You can download the setup from bellow link.
https://www.microsoft.com/en-us/download/details.aspx?id=48593
Based on your description, I create a demo and reproduce your issue on our side. If you want to use C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.8\tsc.exe, please refer to the following steps.
1.Right-Click -> Unload Project
2.Right-Click -> Edit
replace references to
Microsoft.TypeScript.Default.props
The import should look something like:
<Import
Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props"
Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props')" />
Microsoft.TypeScript.targets
The import should look something like:
<Import
Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets"
Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets')" />
For more information, please refer to:
https://github.com/Microsoft/TypeScript/wiki/Configuring-MSBuild-projects-to-use-NuGet

Visual Studio .Net Core project uses the Typescript v1.8 compiler instead of v2.0

I'm using Visual Studio 2015 Pro, Update 3.
My question is how to instruct VS to use the version of the typescript compiler that I want it to use.
VS is refusing to use the Typescript 2.0 compiler, rather is using 1.8. I have installed, via Nuget in VS, Microsoft.Typescript.Compiler v2.0.3 and Microsoft.Typescript.MsBuild v2.0.3, to no avail.
Looking at the detailed output from MSBuild, I see the following:
Target "PreComputeCompileTypeScriptWithTSConfig" in file "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TypeScript\Microsoft.TypeScript.targets" from project "C:\Users[...].xproj" (target "CompileTypeScriptWithTSConfig" depends on it):
Using "VsTsc" task from assembly "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\TypeScript\TypeScript.tasks.dll".
Task "VsTsc"
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.8\tsc.exe --project "C:\Users[...]\tsconfig.json"
Done executing task "VsTsc".
Done building target "PreComputeCompileTypeScriptWithTSConfig" in project "[...].xproj".
So it appears that VS is using the tsc.exe from the Microsoft SDK.
Anyone know how to instruct VS to use the version of tsc that I want it to use?
#starain I really appreciate your efforts.
Unfortunately your suggestion still doesn't work for me. MSBuild/VS does not use the environment path to find tsc. If it were, it would already be finding the correct version. The correct path (to 2.0.3) is already in the environment, and the incorrect path (to 1.8) is not.
I hadn't seen the link you provided to the issue with nuget. This problem does at least appear to be an issue with the nuget installation.
Aided by setting the MSBuild output to "diagnostic" I have managed to figure out a work-around on my own.
In the .xproj file inside <PropertyGroup Label="Globals"> I inserted the following:
<TypeScriptToolsVersion>2.0.3</TypeScriptToolsVersion>
<TscToolPath>C:\Users\[me]\.nuget\packages\Microsoft.TypeScript.MSBuild\2.0.3\tools\tsc</TscToolPath>
So far, it works beautifully except for one thing: Intellisense appears to still be using 1.8. I will leave that for a separate issue.
This solution could easily, of course, present a problem each time I want to update tsc. But for now it's a lot better than nothing.
You need to remove corresponding imported project from your project file.
Right click your project in VS=>Unload project
Right click your project again in VS=>Edit XXX.csproj
Remove this code:
Microsoft.TypeScript.Default.props:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props')" />
Microsoft.TypeScript.targets:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets')" />
(Optional) If this code isn’t included in your project file, please add it (related to the code of Step 3)
:
<Import Project="..\packages\Microsoft.TypeScript.MSBuild.2.0.3\build\Microsoft.TypeScript.MSBuild.props" Condition="Exists('..\packages\Microsoft.TypeScript.MSBuild.2.0.3\build\Microsoft.TypeScript.MSBuild.props')" />
<Import Project="..\packages\Microsoft.TypeScript.MSBuild.2.0.3\build\Microsoft.TypeScript.MSBuild.targets" Condition="Exists('..\packages\Microsoft.TypeScript.MSBuild.2.0.3\build\Microsoft.TypeScript.MSBuild.targets')" />
For .net core application, it has issue with that nuget package, the typescript reference in ASP.NET Core projects is through the Microsoft.DotNet.Web.targets. So, it won't work. You can check this link (paulvanbrenk's reply).
To use higher version of typescript compiler, you can install Typescript 2.0 and change environment variable path value to corresponding folder (C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.0)

Visual Studio 2015 - Typescript "Build:Cannot find module..."

I'm trying to add Typescript to an existing .NET MVC project. I however get weird error-messages when I try to build. The Intellisense works and I can see that the typeings works. I can also see the .js-files being generated on manual save, meaning that the compileOnSave works. It's only when I try to build the whole project the errors show up.
I use the following settings in Visual Studio 2015:
Have installed typescript 2.0.3
Added the following devDependencies in my package.json-file
My tsconfig-file looks like this
This is how i import the dependencies specified in the package.json-file.
I had an app that showed the 'cannot find module' message in the editor when viewed in Visual Studio 2015 [TypeScript 2.0.6] - but worked perfectly in VSCode!
I tried all the recommended changes found on StackOverflow and on other sites but none of them worked for me. The change that fixed my problem was to remove the 'outDir' property from the tsconfig file! I know that the code in the original question does not use the outDir property but this answer may help somebody else.
This change was also necessary for VS2017.
My suggestion: use VSCode if possible! :)
Are your npm dependencies installed? If not, then you should run npm install command on your project folder manually, or to avoid this in the future, you can automatically install npm packages to your project on build by editing your .njsproj file, after line
<Import Project="$(VSToolsPath)\Node.js Tools\Microsoft.NodejsTools.targets" />
add the following section:
<PropertyGroup>
<PreBuildEvent>
npm install --msvs_version=2015
</PreBuildEvent>
</PropertyGroup>
Or if you only want it on rebuild, then:
<PropertyGroup>
<BeforeRebuildEvent>
npm install --msvs_version=2015
</BeforeRebuildEvent>
</PropertyGroup>
The --msvs_version=2015command line option is not mandatory, but if you use multiple versions of VS or your npm is not set up correctly, then it might be useful.
I had the same issue and fixed by installing npm.
npm install
I am using RequireJS and needed to change the module system from CommonJS to AMD. In Visual Studio:
Right Click Project in Solution Explorer, "Properties"
TypeScript Build (tab on left)
Change selection under Module system
I had same problem with Visual Studio 2017. Solved problem installing angular-cli.
Hope it can help :)
The solution posted here works for me: Cannot find module when compiling under Visual Studio
(Update to the latest version of TypeScript for Visual Studio 2015 here: https://www.microsoft.com/en-us/download/details.aspx?id=48593
)

TypeScript / Visual Studio 2012 / Compilation parameters

I need to generate Source Maps when the TypeScript Compiler is executed from Visual Studio 2012. In Sublime Text 2, I just had to add an extra parameter to the build script.
I'm lost in VS 2012.
The Build section doesn't seem to have a section to add build parameters to the build process.
The command switch is "-sourcemap".
As such: tsc -sourcemap file.ts
Mads Kristensen's Web Essentials extension for Visual Studio added support for producing source maps automatically in version 1.3. You need to enable it in in Tools -> Options for Web Essentials. http://visualstudiogallery.msdn.microsoft.com/07d54d12-7133-4e15-becb-6f451ea3bea6
The free Express editions of Visual Studio does not support extensions.
There isn't a VS 2012 UI for editing the TypeScript build flags (yet) but you can get what you want by editing the project file and adding the -sourcemap flag to that build step.
<Target Name="BeforeBuild">
<Exec Command=""$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.0.0\tsc" -sourcemap #(TypeScriptCompile ->'"%(fullpath)"', ' ')" />
</Target>
Follow the guide on this StackOverflow answer. replace --module amd to --sourcemap.
Visual Studio TypeScript Options
In 0.9.5 you can use the following:
Upload the project and in the property group and the following tag:
<TypeScriptSourceMap>true</TypeScriptSourceMap>
That make the job.

Resources