Title: Azure Pipelines: dotnet test fails after dotnet build with -o - "It was not possible to find any compatible framework version"
Tags: .net;.net-core;msbuild;azure-pipelines
Question: Here's what I'm trying to do, running a Pipeline on a self-hosted Agent:

Install .NET SDK 6.0.202
Build my Solution to a specific Output Directory:

```- task: DotNetCoreCLI@2
        inputs:
          command: build
          projects: MySolution.sln
          arguments: &quot;--configuration MyConfiguration -o $(Build.BinariesDirectory)&quot;
```

Run (NUnit) Unit tests contained in some of the built DLLs

```      - task: DotNetCoreCLI@2
        inputs:
          command: test
          projects: |
            $(Build.BinariesDirectory)\**\*Tests.dll
```
However, I get the TestHost exiting with the following error:
```
It was not possible to find any compatible framework version
The framework 'Microsoft.NETCore.App', version '6.0.0' (x64) was not found.

No frameworks were found.

```

The same thing works, if I leave out the -o parameter and just let each project build in its bin/MyConfiguration folder.
Locally, everything works fine, with or without the -o, just doing &quot;dotnet test Outfolder/someTests.dll&quot;.
Listing the installed runtimes with dotnet --list-runtimes, shows various runtimes including the wanted 6.0.0.
Using &quot;where.exe dotnet&quot; shows two installation locations, with the correct one (_work_tool\dotnet\dotnet.exe) showing first. Neither indicates a x86 installation, following advice for some similar problems that pointed to the x86 runtime being falsely used/discovered
Both the succeeding step without -o and the failing step with -o use the same dotnet.exe
Getting the agent's OS architecture with &quot;wmic OS get OSArchitecture&quot; returns 64-bit
I tried explicitly installing the .NET runtime 6.0.0 to no avail.
Earlier, all projects were configured via .targets file to build to a common output directory. All test projects were configured to build to a different common directory. Build and test were both successful at that point.
All projects define x64 as Platform, all test projects additionally define PlatformTarget x64
All projects have net6.0-windows as TargetFramework. I am unsure why it is trying to explicitly use the 6.0.0 runtime instead of a later 6.0.x one.

How do I get my build agent to successfully run my tests when explicitly building to an output directory?
Update
Some new info:
Extending the UseDotNet Task with &quot;performMultiLevelLookup: true&quot; replaces &quot;No frameworks were found&quot; with a list of some runtimes found at C:\Program Files (those which are installed globally on the agent I guess).
The issue seems to be that dotnet test does not look for the runtime in the agent working directory (_work_tool\dotnet). PATH does include that path (and ahead of C:\Program Files...), but with a forward slash (C:\agent_work_tool/dotnet) which I am not sure if it is an issue.
Update 2
These issues might be related:
https://github.com/microsoft/vstest/issues/2228
https://github.com/dotnet/runtime/issues/68180
I suspect that the testing task is looking for the runtime in the wrong place, which is why it finds none and some in the default installation location if using performMultiLevelLookup. Why this would happen only if I previously build to a specific output directory though and how to fix it, is puzzling to me.
I tried outputting the DOTNET_ROOT env var at various points throughout the pipeline run. It is set by the UseDotNetTask (to ```C:\agent\_work\_tool/dotnet```) and not changed before or after the testing task. So I guess, either it is set temporarily during the test task, ignored or doesn’t work due to the forward slash. But then: why does it work for dotnet build?



Here is another answer: in our Pipelines running on servers with multiple runtimes, before we use .NET core we always have to include a task like this at the top, give it a try if it makes any difference:
```steps:
- task: UseDotNet@2
  inputs:
    version: '6.0.x'
```

Comment for this answer: We do have that. It's what I meant with my first bullet point "Install .NET SDK". Additionally, we tried explicitly installing the 6.0.0 runtime with the UseDotNet task and packageType runtime.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:05/24/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<.net><.net-core><msbuild><azure-pipelines>', 'Title': 'Azure Pipelines: dotnet test fails after dotnet build with -o - "It was not possible to find any compatible framework version"', 'ViewCount': 266, 'id': 72360080, 'user_AboutMe': None, 'user_AccountId': 25373870.0, 'user_DisplayName': 'TheNickOfTime', 'user_Reputation': 1.0, 'user_Views': 0.0, 'user_id': 19187272.0}}