Search code examples
c#azure-devopsyamlazure-pipelines-yaml

Azure DevOps pipeline build issue with floating NuGet package version number


I am doing YAML pipelines in Azure DevOps with C# Visual Studio projects. My test and build pipeline worked fine until I was going to tryout floating NuGet package version number in my C# projects looking like this:

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="*" />
<PackageReference Include="My.Internal.AzureDevOps.Package" Version="*" />

And my pipeline YAML code in Azure DevOps looks like this:

jobs:
  - job: BuildAndTest
    steps:
        
      - task: UseDotNet@2
        inputs:
          packageType: 'sdk'
          version: '6.x'

      - task: DotNetCoreCLI@2
        displayName: 'Restore packages'
        inputs:
          command: 'restore'
          projects: '**/*.csproj'
          feedsToUse: 'select'
          vstsFeed: 'ba262f00-12b3-4448-8346-5795269eb94b/7040e1b7-4758-4ab0-b3bb-ed9d6427d570'

      
      - task: DotNetCoreCLI@2
        displayName: 'Build'
        inputs:
          command: 'build'
          projects: '**/*.csproj'
          arguments: '--configuration release'
      
      - task: DotNetCoreCLI@2
        displayName: 'Test'
        inputs:
          command: 'test'
          projects: '**/*Tests.csproj'
          arguments: '--configuration release'

So when I ran the same pipeline but with floating version numbers in my C# project the build step fails because it cannot find my local Azure DevOps nuget packages. Restore task works fine as I point out my local Azure DevOps Nugets with vstsFeed but the build task does not. Why can't my build task find the nugets?

What I understand there is no support for vstsFeed in the build step (I have tried, throws an error) and any authorization should be done and set in the restore task (I've tried that also in all different ways but failed). Any how does the restore task work without any authorization but the build step does not. I don't exactly how DotNetCoreCLI does the build step but it feels like it does like Visual Studio does when building, get and check for the latest nuget version available and use that but DotNetCoreCLI doesn't get access to get the nuget versions or finds/get the paths, like in my case the local vstsFeed. I have spent more then 1 day on this trying all different things with no success.


Solution

  • I can reproduce the issue when I build a .NET 6 project on self-hosted agent using DotNetCoreCLI task.

    Error NU1101: Unable to find package Fortest. No packages exist with this id in xxxx.

    In dotnet build, DotNet restore runs implicitly. As the restore step is successful, you can add the argument '--no-restore' to disable restore in build step.

    - task: DotNetCoreCLI@2
      displayName: 'Build'
      inputs:
        command: 'build'
        projects: '**/*.csproj'
        arguments: '--configuration release --no-restore'
      
    

    Or you can add Nuget.config file in your project folder and set vstsFeed you want to use.

    <packageSources>
        <add key="Source" value="vstsFeed" />
    </packageSources>
    <config>
        <add key="globalPackagesFolder" value="your package folder" />
     </config>