0
votes

I try to simply run "dotnet test" in azure pipeline (the command runs fine on my machine)

jobs:
- job: Build_and_test 
  steps:
  - task: DotNetCoreCLI@2
    displayName: "Xxx.sln"
    inputs:
      command: 'test'
      workingDirectory: 'Xxx'
      feedsToUse: 'select'
      feedRestore: 'xxx/xxx' #format I use is 'Project_name/Feed_name', it works with nuget push BTW
      includeNuGetOrg: true

The error I have :

d:\a\1\s\xxx\xxx\xxx.csproj : error NU1101: Unable to find package XXX. No packages exist with this id in source(s): Microsoft Visual Studio Offline Packages, nuget.org [d:\a\1\s\xxx\xxx.sln]

As you can see, it seems that the feedRestore (I also tried to use "vstsFeed") is not used because only nuget.org source is searched. I also tried to only use 'Feed name' (without project_name/feed_name) ...

Everything I've tried comes from https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops

The problem is that most problems related to NU1101 are mainly with the old pipelines that are using nuget.config (but I do not want to put that in my source control, I want to use the 'select' option to avoid hardcoding the URL of the feed everywhere) For ex, irrelevant question for my case : Error NU1101: Unable to find package ProjectABC.Core.Services. No packages exist with this id in source(s)

Also, if I use nuget.config with my private nuget feed : C:\Program Files\dotnet\sdk\3.0.101\NuGet.targets(123,5): error : Unable to load the service index for source https://xxx.pkgs.visualstudio.com/xxx/_packaging/Xxx/nuget/v3/index.json. [d:\a\1\s\xxx\xxx.sln]

Does someone have an idea to why my sources is not searched ?

1
I assume it should work if you change the test to restore... Dotnet restore is the better choice to do the restore work, and dotnet test is used to run the test after restore=>build. I tried your script and reproduced same issue, please add a dotnet restore task before the test task to restore the packages. (Not sure if yaml rule allows this, but after my check, dotnet test seems not to recognize the feed well)LoLance

1 Answers

2
votes

Does someone have an idea to why my sources is not searched ?

I think this issue resulted from that dotnet test didn't recognize the feedRestore element. I reproduced same issue using your script and then find the dotnet test won't recognize feedRestore element. So the issue occurs when it can't find packages in artifacts feed.

For this, I think you can resolve this issue by adding a dotnet restore command before dotnet test. Normally, dotnet restore instead of dotnet test is the one designed to restore nuget packages.

And the normal template for one .net core project should look like this:

Classic UI:

enter image description here

Yaml:

  ...
  steps:
  - task: DotNetCoreCLI@2
    inputs:
      command: 'restore'
  ...
  - task: DotNetCoreCLI@2
    inputs:
      command: 'build'
  ...
  - task: DotNetCoreCLI@2
    inputs:
      command: 'test'
...

So you can change your script to something similar to this:

steps:
- task: DotNetCoreCLI@2
  displayName: "My restore task."
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'select'
    feedRestore: 'ProjectName/FeedName' #Using ProjectName/FeedName if it's project scoped feed.
    includeNuGetOrg: true

- task: DotNetCoreCLI@2
  displayName: "My test task."
  inputs:
    command: 'test'
    projects: '**/*.csproj'
    arguments: '--no-restore'

It's recommended that we should have a restore process before build and test. Please avoid inserting the restore step into test step especially when using Artifacts Feed.