Gerino
Gerino

Reputation: 1983

Make NuGet look for newer packages that satisfy wildcard

Using SDK-style csproj.

Scenario:

csproj:

<PackageReference Include="MyNamespace.Package" Version="3.5.4-*" />
<PackageReference Include="MyNamespace.Other" Version="3.5.3" />

Installed:

MyNamespace.Package 3.5.4-pre.1
MyNamespace.Other 3.5.3

In NuGet repo:

MyNamespace.Package 3.5.4-pre.1
MyNamespace.Package 3.5.4-pre.2
MyNamespace.Other 3.5.3
MyNamespace.Other 3.5.4
MyNamespace.Other 3.6.0

I do not want NuGet to modify any PackageReference entries. I do want NuGet to install newest MyNamespace.Package that satisfies the wildcard, here it will be 3.5.4-pre.2.

Is there any built-in way to achieve that?

Upvotes: 9

Views: 5689

Answers (4)

JasonS
JasonS

Reputation: 7733

2025 answer:

Add <RestoreNoCache> to your .csproj, as shown in this example:

<ItemGroup>
    <PackageReference Include="NotNot.Bcl" Version="*" />
</ItemGroup>
<PropertyGroup>
    <!--force nuget to restore without using cached packages, to ensure above wildcard always gets latest non-preview version in nuget-->
    <RestoreNoCache>true</RestoreNoCache>
</PropertyGroup>

I can't find this property documented anywhere, though a similar property, RestoreNoHttpCache is documented here: https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets

Upvotes: 0

Paul Schroeder
Paul Schroeder

Reputation: 1601

See floating versions here: NuGet PackageReference format - Floating Versions

<ItemGroup>
<!-- ... -->
<PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.*" />
<PackageReference Include="Contoso.Utility.UsefulStuff" Version="3.6.0-beta*" />
<!-- ... -->

Upvotes: 4

kkost
kkost

Reputation: 394

I don't know of a way to do this with pre-release packages, but I believe that PackageReference and wildcards

<PackageReference Include="I.want.the.latest.version.of" Version="*" />
<PackageReference Include="I.want.the.latest.version.3.of" Version="3.*" />

along with msbuild /restore can achieve this. I'm doing the exact same thing in my project, where I want to consume the latest version of certain packages with each build, without modifying any PackageReference entries.

The msbuild /restore target supports a property called RestoreForce (https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#restore-target) that has been very useful here.

NuGet.exe restore, as of this writing (version 5.4.0.6315) appears to have some bugs around supporting wildcard versions in PackageReference (https://github.com/NuGet/Home/issues/8432). msbuild /restore does not suffer from those bugs.

I'm using Visual Studio 2019.

Upvotes: 0

Gerino
Gerino

Reputation: 1983

After writing the question I came up with another thing I have not tried.

To get it to look again in all the repos (or selected ones) and install according to the wildcard package resolution use command:

nuget restore \Path\To\Solution.sln -NoCache [-Source \path\to\repo]

-NoCache will make it ignore the "known" packages, but not update any of your references. It will find the newer package that satisfies the wildcard and install it.

Upvotes: 3

Related Questions