I put the solution here for the simplicity. It's taken from https://github.com/jaredpar/RoundTripVSIX/commits/master - see @ErikEj 's answer with a small addition.
Usually we're importing Microsoft.VsSDK.targets
file into our VS package project's csproj. As my projects doesn't depend on VS SDKs installed globally I'm importing from a local folder with VS2010 SDK:
<Import Project="..\..\SDK\v10.0\MSBuild\VSSDK\Microsoft.VsSDK.targets" />
The trick is to make this import dynamic:
- when we're in VS there should be importing of VSSDK targets of the current VS version
- when we're building the project there should be importing of VSSDK targets of minimal supported VS version (like before).
This is achieved via additional variable ("VsSdkTargets" - name can be any):
<Import Condition="Exists($(VsSdkTargets))" Project="$(VsSdkTargets)" />
And here's definition of VsSdkTargets
(should be before the import):
<PropertyGroup>
<VsSdkTargets Condition=" '$(VisualStudioVersion)' == '' or '$(BuildingInsideVisualStudio)' != 'false' ">..\..\SDK\v10.0\MSBuild\VSSDK\Microsoft.VsSDK.targets</VsSdkTargets>
<VsSdkTargets Condition=" '$(BuildingInsideVisualStudio)' == 'true' ">$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\VSSDK\Microsoft.VsSDK.targets</VsSdkTargets>
</PropertyGroup>
This makes our import dynamic basing on availability of variable VisualStudioVersion
which is available only inside VS and BuildingInsideVisualStudio
. BuildingInsideVisualStudio
will be true
when building inside VS.
If you need to open solution in different VS versions then we need also to add customization of MinimumVisualStudioVersion
variable
<PropertyGroup>
<!-- This is added to prevent forced migrations in Visual Studio 2012 and newer -->
<MinimumVisualStudioVersion Condition="'$(VisualStudioVersion)' != ''">$(VisualStudioVersion)</MinimumVisualStudioVersion>
</PropertyGroup>
In order for the debugging to start in the straightforward way (F5) in all the supported versions of Visual Studio and independently from user settings for the project, you should append the following instructions into the first PropertyGroup
:
<PropertyGroup>
...
<StartAction>Program</StartAction>
<StartProgram>$(DevEnvDir)\devenv.exe</StartProgram>
<StartArguments>/rootsuffix Exp</StartArguments>
</PropertyGroup>