azure-devopsazure-pipelinesnugetdevopsnuget-package-restore

Azure Devops Problem with Nuget packages with Platform inside the name


I've got 2 nugetpackages which follow the naming convention: SomePackage.x86 and SomePackage.x64

Locally these work by doing package reference in the following method:

<PackageReference Include="SomePackage.$(Platform)" Version="xx.xx.xx.xx" />

With the configuration set for x86 or x64 these both work correctly. The problem comes when trying to do a nuget restore task:

  - task: NuGetCommand@2
    displayName: Restore Nuget Packages
    inputs:
      restoreSolution: '$(Build.SourcesDirectory)\MySolution\MySolution-$(buildPlatform).sln'
      feedsToUse: config
      nugetConfigPath: '$(Build.SourcesDirectory)\MySolution\.nuget\NuGet.Config'

I do also have this task inside of a matrix for the platforms. So Ideally one would build x86 and one would build x64

- job: Build
  dependsOn: SetVersion
  strategy:
    maxParallel: 2
    matrix:
      x86:
        buildPlatform: 'x86'
      x64:
        buildPlatform: 'x64'

When the pipeline tries to restore the package, it's clearly using the incorrect platform as the following is the error

 error NU1301: Failed to retrieve information about 'SomePackage.Any CPU' from remote source 'https://somenugeturl/SomePackage.any cpu/index.json'

To further complicate this, I have this compiling in .NET Framework 4.8 and .NET 6.0.

In .net framework, I can set the default platform and this will get the initial build through however, I'm unsure if the other platform would work correctly.

I've tried altering the nuget restore to this:

arguments: "-Property Platform='x86'"

I have also tried

dotnet restore "MySolution.sln" --arch x86 

Which gives me the any cpu error still.

I've got my platforms and platform target set in the project and it doesn't change anything:

<Platforms>x86;x64</Platforms>
<PlatformTarget>x64</PlatformTarget>
<TargetFramework>net6.0-windows</TargetFramework>

I'm wondering if there's a standard way to deal with these kinds of packages?


Solution

  • When build a solution or project, if you do not specify the build configuration (buildConfiguration|buildPlatform), it will use the default configuration "Debug|Any CPU" to run restore and build.

    To overwrite the default configuration when running restore, build and other steps (such as publish, pack, etc..), you can do like as below:

    variables:
      buildConfiguration: Release
    
    jobs:
    - job: build
      strategy:
        matrix:
          x86:
            buildPlatform: 'x86'
          x64:
            buildPlatform: 'x64'
      steps:
      - task: DotNetCoreCLI@2
        displayName: 'Restore Dependences'
        inputs:
          command: 'restore'
          projects: 'path/to/solution.sln'
          restoreArguments: '-p:Configuration=$(buildConfiguration);Platform=$(buildPlatform)'
          feedsToUse: 'config'
          nugetConfigPath: 'path/to/nuget.config'
      
      - task: DotNetCoreCLI@2
        displayName: 'Build Solution'
        inputs:
          command: 'build'
          projects: 'path/to/solution.sln'
          arguments: '--no-restore -c $(buildConfiguration) -p:Platform=$(buildPlatform)'
    

    enter image description here

    enter image description here