Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nuget conditioning on $(Configuration) is not working #5895

Closed
hawston opened this issue Sep 15, 2017 · 20 comments
Closed

Nuget conditioning on $(Configuration) is not working #5895

hawston opened this issue Sep 15, 2017 · 20 comments

Comments

@hawston
Copy link

hawston commented Sep 15, 2017

< 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.

@emgarten emgarten added this to the 4.5 milestone Sep 15, 2017
@emgarten
Copy link
Member

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.

@kulov
Copy link

kulov commented Nov 14, 2017

Closed as WontFix?
This is very much needed.
How can you control project references based on configurations either way.
Please provide workaround at least!

@emgarten
Copy link
Member

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.

@kulov
Copy link

kulov commented Nov 14, 2017

How can this be applied with the Visual Studio build and debug experience?

@emgarten
Copy link
Member

Try using a Choose/When instead of a Condition to select the PackageReference.

@kulov
Copy link

kulov commented Nov 14, 2017

Works great with Choose/When! Thank you!

@rrelyea
Copy link
Contributor

rrelyea commented Nov 29, 2017

@kulov - what kind of project were you needing to choose/when to make it work inside of VS?

  • what version of VS are you seeing the problem in?
  • project type: normal csproj or netcore SDK based csproj?
  • other characteristics?

@kulov
Copy link

kulov commented Nov 29, 2017

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.

@myermian
Copy link

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.

1 similar comment
@iiq374
Copy link

iiq374 commented Mar 5, 2018

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.

@egarim
Copy link

egarim commented Oct 6, 2018

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

@kulov
Copy link

kulov commented Oct 7, 2018

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>

@Hviid
Copy link

Hviid commented Oct 19, 2018

@kulov I can't seem to get the condition to work. What version of VS are you using?
I'm on 15.9 preview 3 with a framework based csproj.
Could something have regressed?

@nmyhre
Copy link

nmyhre commented Oct 22, 2018

@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.

@kulov
Copy link

kulov commented Oct 24, 2018

@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.

@cezarypiatekGC
Copy link

I'm passing additional parameters via /p: and it's working.

 dotnet restore --verbosity minimal /p:Configuration=Release

@Gabriel-Lacatus
Copy link

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.

@cezarypiatekGC
Copy link

@Gabriel-Lacatus If you are building on Linux then case-sensitivity matters. Please try /p:Configuration=Debug

@3ms
Copy link

3ms commented Sep 1, 2021

To close a loop here: There's an environment variable you can set that will apply extra msbuild args to the nuget restore command in azure.

NUGET_RESTORE_MSBUILD_ARGS

You can add this as a pipeline variable in azure. Shove in /p:Platform=$(BuildPlatform) /p:Configuration=$(BuildConfiguration), and it'll apply your build configuration when parsing your project file for nuget projectreferences. You probably still need to do the whole <choose> thing.

See
#7575 (comment)

@omarbekov
Copy link

Here's another way of conditionally using packages

<ItemGroup Condition=" '$(Configuration)' == 'Debug' ">
  <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

ChaosEngine added a commit to ChaosEngine/Dotnet-Playground that referenced this issue Jan 27, 2024
…er "Condition" handling for PackageReference entriers in csprojs gh-issue: NuGet/Home#5895 removing Microsoft.Data.SqlClient package
ChaosEngine added a commit to ChaosEngine/Dotnet-Playground that referenced this issue Jan 27, 2024
…er "Condition" handling for PackageReference entriers in csprojs gh-issue: NuGet/Home#5895 removing Microsoft.Data.SqlClient package
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests