AzureDevOps pipeline: DotNetCoreCLI 'restore' ignores Directory.Build.props file

1.8k views Asked by At

We are switching from NetFramework projects to NetCore projects with multiple TargetFrameworks. This requires a Directory.Build.Props file if the TargetFramework includes code written for net40 or net35. The NuGet packages referred to in the Directory.Build.Props are from NuGet.org.

As the NuGetCommand was built for NetFramework, we are attempting to switch from using the NuGetCommand to DotNetCoreCLI in the yaml pipeline. However, DotNetCoreCLI 'restore' doesn't seem to include the NuGet packages in Directory.Build.props. The restore task works, but the build task fails with the following error:

  Determining projects to restore...
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net20.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net20/1.0.0/microsoft.netframework.referenceassemblies.net20.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net40.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net40/1.0.0/microsoft.netframework.referenceassemblies.net40.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net40.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net40/1.0.0/microsoft.netframework.referenceassemblies.net40.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net20.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net20/1.0.0/microsoft.netframework.referenceassemblies.net20.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
##[error]C:\Program Files\dotnet\sdk\5.0.202\NuGet.targets(131,5): Error : Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net40.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net40/1.0.0/microsoft.netframework.referenceassemblies.net40.1.0.0.nupkg'.
Response status code does not indicate success: 401 (Unauthorized).

NuGetCommand:

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'config'
    nugetConfigPath: 'NuGet.config'

DotNetCoreCLI

- task: DotNetCoreCLI@2
  displayName: DotNetCoreCLI NuGet restore
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'config'
    nugetConfigPath: 'NuGet.config'

NuGet.Config includes the following

  <packageSources>
    ...
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json"/>
  </packageSources>

Directory.Build.props

<Project>
  <PropertyGroup>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
    <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
    <PackageReference Include="jnm2.ReferenceAssemblies.net35" Version="1.0.1" PrivateAssets="all" />
  </ItemGroup>
</Project>
1

There are 1 answers

1
Walter On BEST ANSWER

Please use --no-restore option in your dotnet build task to disable implicit restore.

Here is the document: The dotnet restore command is still useful in certain scenarios where explicitly restoring makes sense, such as continuous integration builds in Azure DevOps Services or in build systems that need to explicitly control when the restore occurs.

In addition, dotnet restore internally uses a version of NuGet.exe that is packaged with the .NET Core SDK. dotnet restore can only restore packages specified in the .NET Core project .csproj files. If you also have a Microsoft .NET Framework project in your solution or use package.json to specify your dependencies, you must also use the NuGet task to restore those dependencies. Please refer to this document.