NuGet / Home

Repo for NuGet Client issues
Other
1.49k stars 250 forks source link

NuGet.exe restore: allow specifying Solution Configuration to use for .sln, for example -Properties similar to pack. #7575

Open KirillOsenkov opened 5 years ago

KirillOsenkov commented 5 years ago

Details about Problem

NuGet product used: NuGet.exe

NuGet version (x.x.x.xxx): 4.9.1.5694

dotnet.exe --version (if appropriate): 2.2.0-preview1-007622

VS version (if appropriate): 15.9.2

OS version (i.e. win10 v1607 (14393.321)): Win10

Worked before? If so, with which NuGet version: not a regression

Detailed repro steps so we can see the same problem

Have an .sln file with two configurations: Debug and Release. Have a project that only fails to build in Debug.

You should be able to restore nuget packages by specifying: nuget restore My.sln /Configuration Release

Currently restore fails because nuget chooses the default (Debug) configuration which doesn't build.

Unfortunately the workaround of using msbuild /r /p:Configuration=Release or dotnet restore doesn't work because many solutions still include projects that have no understanding of NuGet, and so the Restore target isn't there.

nkolev92 commented 5 years ago

You can already achieve that with an environment variable. https://docs.microsoft.com/en-us/nuget/tools/cli-ref-environment-variables

NUGET_RESTORE_MSBUILD_ARGS

Furthermore in PackageReference NuGet itself does not use the Configuration when evaluating Package/ProjectReferences, so not sure relying on NuGet to play well with these Configuration conditions imports is a good idea.

StingyJack commented 5 years ago

@nkolev92 - instead of specifically for a restore, perhaps this is for installing a development or debug tool inclusion? Some additional logging or tracing that only needs to be present for debug configuration, or even something like non-minified JS for a web project. Or maybe the opposite; something that needs to only be included on a release or production build.

mletterle commented 5 years ago

There are cases where MSBuild properties need to be set in order to have a valid parsable project file, while the environment variable works having a generic way to specify multiple msbuild properties on the command line would be handy. Doing nuget restore -Property Configuration='Release' -Property CustomImportPath='C:\SomeDir\Imports' would be handy.

kulov commented 4 years ago

This issue is reported in 2018. https://github.com/NuGet/Home/issues/5895#issuecomment-432767345 You can have workaround with Choose/When/Otherwise msbuild constructs. But it fails to work in Azure DevOps as NuGet defaults to Otherwise condition and there is no way to build When construct on specific project configuration.

kulov commented 4 years ago

I managed to workaround the issue by setting NUGET_RESTORE_MSBUILD_ARGS to /p:Platform=$(BuildPlatform) /p:Configuration=$(BuildConfiguration) in my Azure DevOps pipeline. Without this NuGet parses all csproj files with no Platform value set and it defaults to Debug because of this msbuild line: <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>

So with Choose/When/Otherwise in combination of NUGET_RESTORE_MSBUILD_ARGS I was able to run csproj files with conditional MSBuild ItemGroup elements in Visual Studio and in Azure DevOps.

It would be nice to be able to set properties via command line and also in AzDo NuGet step as @mletterle proposes.

nkolev92 commented 4 years ago

I think a Properties option like @mletterle suggests is the direction here instead of a configuration specific switch.

KirillOsenkov commented 4 years ago

Yes, I'd love to have that. That would be a better and more general solution.

nkolev92 commented 4 years ago

Note that I'm not sure we'll be able to get this to this soon, but we'd be open to taking a contribution.

kulov commented 4 years ago

~~One thing worth noting is that selecting configuration using NUGET_RESTORE_MSBUILD_ARGS seems to work on individual project level and does not respect the solution configuration setup. For example: Lets say MyApp.sln has configuration called ReleaseSetup. It also has project MyLib1.csproj with configuration ReleaseSetup and second project MyLib2.csproj with configuration Release. Solution level configuration ReleaseSetup as specified in the .sln file is set to build MyLib1.csproj in ReleaseSetup configuration and MyLib2.csproj in Release configuration. It seems right now that setting configuration ReleaseSetup with NUGET_RESTORE_MSBUILD_ARGS will basically crawl all individual projects and will try to restore packages using ReleaseSetup configuration. However the specified desired configuration is set to Release on solution level therefore nuget will incorrectly try to restore ReleaseSetup in this case.~~

nkolev92 commented 4 years ago

@kulov

All you do when when you specify that env variable is that NuGet passes that to msbuild when evaluating projects. Configuration is not a supported pivot for PackageReference based projects because MSBuildProjectExtensionsPath is not configuration based.

kulov commented 4 years ago

Please ignore - it was related to something else.