< PackageReference Include="Newtonsoft.json" Version="9.0.1" Condition="'$(Configuration)' == 'Debug'" / >
I am trying to keep one nuget package in my project for debug configuration, but it is still there even for release.
TargetFramework
is the only support condition allowed for PackageReference
items.
To work with other conditions you will need to handle them yourself by passing the extra property values to restore, and ensuring that restore is done appropriately before each build.
Closed as WontFix?
This is very much needed.
How can you control project references based on configurations either way.
Please provide workaround at least!
The workaround is to run msbuild /t:restore /p:Configuration=release
Any custom parameters you pass to build must also be passed to restore. If you do that conditioning on configuration works fine, it just isn't possible to run 1 restore and then N builds on N different configurations without restoring again.
How can this be applied with the Visual Studio build and debug experience?
Try using a Choose/When instead of a Condition to select the PackageReference.
Works great with Choose/When! Thank you!
@kulov - what kind of project were you needing to choose/when to make it work inside of VS?
It is Visual Studio 2017, v15.4.4
normal csproj
no other specifics other that I want to load one specific dll on one configuration and load 5 other dlls if the configuration differs from the first.
The Choose/When workaround works, though I wonder if this is going to be the official way to do it, or will there be an update to simply utilize the Condition attribute on the element itself?
It's strange that Choose/When w/ Condition works but just a Condition on ItemGroup or PackageReference does not work.
The Choose/When workaround works, though I wonder if this is going to be the official way to do it, or will there be an update to simply utilize the Condition attribute on the element itself?
It's strange that Choose/When w/ Condition works but just a Condition on ItemGroup or PackageReference does not work.
Choose/When
Hi Martin @kulov , can you share your csproject and packages.config I want to learn how to use choose/when to restore include different versions of the NuGet packages my project need
Hi @egarim,
Edit your csproj file and put the ItemGroup section that has your reference into Choose/When/Otherwise condition.
The example below shows how to load assembly reference compiled locally on your disk vs assembly reference as a nuget package. You will need to create build configuration DebugLocal also for your project/solution in order to get it working.
<Choose>
<When Condition="'$(Configuration)' == 'DebugLocal'">
<ItemGroup>
<Reference Include="MyRef">
<HintPath>..\MyRef\bin\Debug\MyRef.dll</HintPath>
</Reference>
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="MyRef">
<Version>2.*</Version>
</PackageReference>
</ItemGroup>
</Otherwise>
</Choose>
@kulov I can't seem to get the
I'm on 15.9 preview 3 with a framework based csproj.
Could something have regressed?
@kulov, your choose/when/otherwise solution worked perfectly for me. I was attempting to use a separate debug and ship version of a nuget and running into the usual set of issues. @Hviid , FWIW, I'm on VS 15.8.7 release.
@Hviid, the only problem so far I found was that $(Configuration) is not populated for the Choose/When clause when running Visual Studio Build step in Azure DevOps.
I am upgrading my VS to newest updates all the time so it should be working across all versions of VS15.
I'm passing additional parameters via /p:
and it's working.
dotnet restore --verbosity minimal /p:Configuration=Release
Conditioning does not work in Azure DevOps when generating NuGets from projects with conditional references.
E.g: in csproj we have package references such as
<Choose>
<When Condition="'$(Configuration)' == 'Debug'">
<ItemGroup>
<ProjectReference Include="..\ProjectA\ProjectA.csproj" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<ProjectReference Include="..\ProjectB\ProjectB.csproj" />
</ItemGroup>
</Otherwise>
</Choose>
When the nupkg is generated, in the .nuspec file we can always see a dependency to ProjectB, even when building with 'Debug' passed to msbuild: /p:configuration="Debug"
The frustrating thing is that running the same msbuild command locally, the .nuspec is generated correctly, meaning the condition is evaluated correctly and we see a dependency to ProjectA.
@Gabriel-Lacatus If you are building on Linux then case-sensitivity matters. Please try /p:Configuration=Debug
Most helpful comment
Hi @egarim,
Edit your csproj file and put the ItemGroup section that has your reference into Choose/When/Otherwise condition.
The example below shows how to load assembly reference compiled locally on your disk vs assembly reference as a nuget package. You will need to create build configuration DebugLocal also for your project/solution in order to get it working.
<Choose> <When Condition="'$(Configuration)' == 'DebugLocal'"> <ItemGroup> <Reference Include="MyRef"> <HintPath>..\MyRef\bin\Debug\MyRef.dll</HintPath> </Reference> </ItemGroup> </When> <Otherwise> <ItemGroup> <PackageReference Include="MyRef"> <Version>2.*</Version> </PackageReference> </ItemGroup> </Otherwise> </Choose>