msbuild: Check if Import was successful - visual-studio

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')" />

Related

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)

Build VS 2015 extension on build server without VS installed?

Is it possible to build a Visual Studio 2015 extension project on a build server (TeamCity agent) without Visual Studio installed? What kind of SDK do we need to install?
At the moment we receive the following error message:
error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\Portable\v4.6\Microsoft.Portable.CSharp.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.
So there is definitely some kind of SDK missing.
Microsoft.VSSDK.BuildTools
Contains targets and tools to enable the building of managed VSIX
projects without VSSDK MSI installed. Only for VS 2015 and onwards
Additional packages that may be of interest:
https://www.nuget.org/profiles/VisualStudioExtensibility
Using #weir's answer almost worked - the project built successfully, but it failed to produce a VSIX container at the end. For some reason the Nuget package hadn't added the necessary Import to the .csproj to bring in the VsSDK.targets, so the VSIX targets were missing and never got executed.
Here are the steps which worked for me:
Edit the VSIX project .csproj file, and remove Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" />. This will fail on the build server where the VSSDK doesn't exist in the VSToolsPath.
In the VS2015 IDE, open the Nuget Package Manager for the project, and install Microsoft.VSSDK.BuildTools (I used v14.3.25407)
Back in the .csproj file, find the import which the Nuget package added, e.g. <Import Project="..\packages\Microsoft.VSSDK.BuildTools.14.3.25407\build\Microsoft.VSSDK.BuildTools.targets" .../> and add another one below it for the VsSDK.targets file (inside the tools directory), <Import Project="..\packages\Microsoft.VSSDK.BuildTools.14.3.25407\tools\vssdk\Microsoft.VsSDK.targets" .../>
It looks like you have to install the Portable Library Tools on the build agent. You can download them from the VS Gallery and install them without having VS on the build agent using the following parameter /buildmachine.
Download Microsoft Build Tools 2015

How do I integrate StyleCop 4.7 with 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.

MSBuild in TeamCity of Visual Studio 2012 solution

I have a VS 2012 web project /sln that I am trying to build in TeamCity. it uses .NET 4.5 which is installed on TeamCity.
The TeamCity server has VS 2010 installed only.
I get this error when the build runs:
C:\BuildAgent\work\d5bc4e1b8005d077\CUSAAdmin.Web\CUSAAdmin.Web.csproj(799, 3):
error MSB4019:
The imported project
"C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets" was not found.
Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. Project CUSAAdmin.Web\CUSAAdmin.Web.csproj failed.
Project CUSAAdmin.sln failed.
It is trying to use Visual Studio 2012 (v11.0) to build.
I have set the VisualStudioVersion to be 10 in the build.xml though??
<Target Name="BuildPackage">
<MSBuild Projects="CUSAAdmin.sln" ContinueOnError="false"
Targets="Rebuild"
Properties="Configuration=$(Configuration); VisualStudioVersion=10.0" />
As well inside the project it is defaulting to VS2010
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
<VSToolsPath
Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
Actually, you don't need to install Visual Studio on your CI server. You only need to copy a few folders from a development machine to the same location on the CI server.
VS 2015:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\Web
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications
VS 2013:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Web
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\WebApplications
VS 2012:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications
VS 2010:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications
.NET 4.6:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6
.NET 4.5.2:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2
.NET 4.5.1:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1
.NET 4.5:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5
.NET 4.0.1:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0.1
.NET 4.0:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0
Or, as Matt suggested, you could copy them into a subdirectory of your project and alter the <MSBuildExtensionsPath32> location in your MSBuild (typically .csproj or .vbproj) file.
Once you have done this, your project will compile. You should still set the VisualStudioVersion explicitly to the one you are using just to be sure it is set right.
NOTE: This solution works for all project types (including web projects). For a web site (that has no project file), I ended up installing the Windows SDK matching the .NET SDK version I am using, because there were missing registry keys that were causing it not to build.
Turns out it's really simple. To make MSBuild run VS2010 as the builder on a solution made by VS2012 in TeamCity, simply set the environment variable for the build configuration like this:
Name: env.VisualStudioVersion
Value: 10.0
Note TeamCity does not need VS2012 installed.
Alternatively, you can copy the build targets you need from the c:\Program Files (x86)\MSBuild to a subdirectory of your project (for example .\Build) making sure to preserve structure and add the following to your csproj:
<!-- redirect msbuild path so targets can be added to source control -->
<PropertyGroup>
<MSBuildExtensionsPath32>..\Build\</MSBuildExtensionsPath32>
</PropertyGroup>
For example, if my project root is C:\Dev\MyProjSln\MyProj
Create Folder C:\Dev\MyProjSln\Build\Microsoft\VisualStudio\version\WebApplications\
Copy Contents of C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\version>\WebApplications\ to created folder
Add MSBuildExtensionsPath32 element to Property Group under Project node in csproj
Profit!
Personally, I prefer this method of tracking build target dependencies, as it prevents build server from being dependent on having undocumented folder structure requirements, and gets your dependencies into source control
As described here:
Install nuget MSBuild.Microsoft.VisualStudio.Web.targets
Edit the .csproj file
Replace:
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
with:
<Import Project="..\packages\MSBuild.Microsoft.VisualStudio.Web.targets.11.0.2.1\tools\VSToolsPath\WebApplications\Microsoft.WebApplication.targets" Condition="true" />
Obviously you have to make sure versions match your case both on the nuget installed and path in <Import>
I totally disagree with changing the project files because that might affect other developers. This is what worked for me since the v11.0 folder was missing on MS build folder:
1)Create v111.
Create v11.0 folder on C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio
Copy Web and WebApplications folders from my development box "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0"
Booom! work like a charm\
Note: I installed "Microsoft Visual Studio 2012 Shell (Isolated) Redistributable Package"
Firstly, TeamCity does not require presence of Visual Studio of any version to build. When a build step in the project is configured with MSBuild runner TeamCity needs to know which version of MSBuild to use. This is a setting of a build step. Proper MSBuild version must be selected in a build step configuration according to the .NET tools installed on a build machine. TeamCity will use that value to determine the location path and will set an environment variable when invokes MSBuild.
The problem with the Visual Studio 2012/2013 is that it's only supported by TeamCity starting 8.1.0. So if your TeamCity version is before 8.1. you need to upgrade to the latest 8.1.x to see a setting for MSBuild Tools 2013. Once you select a proper MSBuild version in the build step settings the problem will disappear. For more information read here: http://blog.turlov.com/2014/07/upgrade-teamcity-to-enable-support-for.html

Resources