Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"The library 'hostpolicy.dll' required to execute the application was not found" - Azure vstest #4743

Closed
jvu-parsec opened this issue Nov 8, 2023 · 16 comments

Comments

@jvu-parsec
Copy link

jvu-parsec commented Nov 8, 2023

Description

Hello, I have an automated E2E test project on Azure and have a build pipeline set up to run our test suite

I am trying to set up a release pipeline in order to run selected automated tests directly from Azure Test Plans, following this guide:
https://learn.microsoft.com/en-us/azure/devops/test/run-automated-tests-from-test-hub?view=azure-devops

My build pipeline publishes an artifact containing the test binaries and the release pipeline downloads the binaries artifact ok, also the automated test cases are associated to the case in the Test Plan

But when I go to run the test I get a 'hostpolicy.dll' library missing error (see error message / logs below)

I tried following the steps outlined in this response but have not been able to get it working but yet:
#2427 (comment)

Steps to reproduce

Expected behavior

Selected test(s) from Test Plans should run

Actual behavior

Test exits with error: 'hostpolicy.dll' required to execute the application was not found

Diagnostic logs

2023-11-08T18:38:35.3500242Z ##[error]DiscoveryMessage : Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Testhost process for source(s) 'C:\agent01_work\r1\a\binaries\Ts.TestAutomation.Web.Tests.dll' exited with error: Cannot use file stream for [C:\agent01_work\r1\a\binaries\testhost.deps.json]: No such file or directory
2023-11-08T18:38:35.3507332Z A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\agent01_work\r1\a\binaries'.
2023-11-08T18:38:35.3509600Z Failed to run as a self-contained app.
2023-11-08T18:38:35.3511962Z - The application was run as a self-contained app because 'C:\agent01_work\r1\a\binaries\testhost.runtimeconfig.json' was not found.
2023-11-08T18:38:35.3517076Z - If this should be a framework-dependent app, add the 'C:\agent01_work\r1\a\binaries\testhost.runtimeconfig.json' file and specify the appropriate framework.
2023-11-08T18:38:35.3519328Z . Please check the diagnostic logs for more information.
2023-11-08T18:38:35.3522035Z at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.ThrowOnTestHostExited(IEnumerable1 sources, Boolean testHostExited) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs:line 520 2023-11-08T18:38:35.3525746Z at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable1 sources, String runSettings) in //src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs:line 294
2023-11-08T18:38:35.3529864Z at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.InitializeDiscovery(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler, Boolean skipDefaultAdapters) in /
/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyDiscoveryManager.cs:line 149
2023-11-08T18:38:35.4866569Z ##[error]DiscoveryMessage : Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Testhost process for source(s) 'C:\agent01_work\r1\a\binaries\Ts.TestAutomation.Web.Tests.dll' exited with error: . Please check the diagnostic logs for more information.
2023-11-08T18:38:35.4962672Z at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.ThrowOnTestHostExited(IEnumerable1 sources, Boolean testHostExited) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs:line 520 2023-11-08T18:38:35.4979792Z at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyOperationManager.SetupChannel(IEnumerable1 sources, String runSettings) in //src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyOperationManager.cs:line 294
2023-11-08T18:38:35.4987864Z at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.Client.ProxyDiscoveryManager.InitializeDiscovery(DiscoveryCriteria discoveryCriteria, ITestDiscoveryEventsHandler2 eventHandler, Boolean skipDefaultAdapters) in /
/src/Microsoft.TestPlatform.CrossPlatEngine/Client/ProxyDiscoveryManager.cs:line 149
2023-11-08T18:38:35.5697915Z ##[error]DiscoveryMessage : Microsoft.VisualStudio.TestPlatform.ObjectModel.TestPlatformException: Testhost process for source(s) 'C:\agent01_work\r1\a\binaries\binaries\Ts.TestAutomation.Web.Tests.dll' exited with error: Cannot use file stream for [C:\agent01_work\r1\a\binaries\binaries\testhost.deps.json]: No such file or directory
2023-11-08T18:38:35.5704704Z A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\agent01_work\r1\a\binaries\binaries'.
2023-11-08T18:38:35.5707532Z Failed to run as a self-contained app.
2023-11-08T18:38:35.5709067Z - The application was run as a self-contained app because 'C:\agent01_work\r1\a\binaries\binaries\testhost.runtimeconfig.json' was not found.
2023-11-08T18:38:35.5711018Z - If this should be a framework-dependent app, add the 'C:\agent01_work\r1\a\binaries\binaries\testhost.runtimeconfig.json' file and specify the appropriate framework.

Environment

Test project on .NET 7.0
vstest task version 2

@nohwnd
Copy link
Member

nohwnd commented Nov 9, 2023

Could you check that there are both runtimeconfig.json and deps.json files next to your test dll? Their name should start with the name of your dll.

@jvu-parsec
Copy link
Author

Could you check that there are both runtimeconfig.json and deps.json files next to your test dll? Their name should start with the name of your dll.

Yes, runtimeconfig.json and deps.json are both in the same directory my Tests.dll is in

deps and runtimeconfig

@ksidirop-laerdal
Copy link

ksidirop-laerdal commented Jan 31, 2024

Facing the same issue with a dotnet8 / xunit project in Azure. In my localdev I have the test-dlls published properly under 'bin/Release/net8.0/publish/'. I then invoke:

dotnet  test  Foo.Tests/bin/Release/net8.0/publish/Foo.Tests.dll  --verbosity normal  --logger trx;LogFileName=Laerdal.Assets.Installers.Tests.trx  --results-directory TestResults

This works on localdev but fails when running it in Azure with the following error message:

  Starting test execution, please wait...
  A total of 1 test files matched the specified pattern.
##[error]Testhost process for source(s) 'D(0,0): Error : A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'D:\a\1\s\Foo\bin\Release\net8.0\publish\'.
Testhost process for source(s) 'D : \a\1\s\Foo\bin/Release/net8.0/publish/Foo.Tests.dll' exited with error : A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'D:\a\1\s\Foo\bin\Release\net8.0\publish\'. [D:\a\1\s\Laerdal.Scripts\Laerdal.Builder.targets]
  Failed to run as a self-contained app.
    - The application was run as a self-contained app because 'D:\a\1\s\Foo\bin\Release\net8.0\publish\testhost.runtimeconfig.json' was not found.
    - If this should be a framework-dependent app, add the 'D:\a\1\s\Foo\bin\Release\net8.0\publish\testhost.runtimeconfig.json' file and specify the appropriate framework.
  . Please check the diagnostic logs for more information.
  
  Test Run Aborted.
  Results File: D:\a\1\s\TestResults\Laerdal.Assets.Installers.Tests.trx
##[error]Laerdal.Scripts\Laerdal.Builder.targets(151,9): Error MSB3073: The command "..." exited with code 1.
D:\a\1\s\Laerdal.Scripts\Laerdal.Builder.targets(151,9): error MSB3073: The command "..." exited with code 1.

Using the following nugets:

        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0-preview-24080-01" />
        <PackageReference Include="Microsoft.TestPlatform" Version="17.10.0-preview-24080-01" />
        <PackageReference Include="Microsoft.TestPlatform.TestHost" Version="17.10.0-preview-24080-01" />

        <PackageReference Include="xunit" Version="2.4.2"/>
        <PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="coverlet.collector" Version="6.0.0">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>

@nohwnd
Copy link
Member

nohwnd commented Jan 31, 2024

This seems strange to reference <PackageReference Include="Microsoft.TestPlatform" Version="17.10.0-preview-24080-01" /> but it should not hurt anything, maybe you are using it to get the whole TP into your .nuget cache.

There is most likely no Foo.Tests.runtimeconfig.json in the output folder. Which means your test project is not built as exe for some reason. Can you try adding <OutputType>exe</OutputType> property to your .csproj?

@ksidirop-laerdal
Copy link

ksidirop-laerdal commented Jan 31, 2024

This seems strange to reference <PackageReference Include="Microsoft.TestPlatform" Version="17.10.0-preview-24080-01" /> but it should not hurt anything, maybe you are using it to get the whole TP into your .nuget cache.

It was just me experimenting with adding a few more nugets just to be sure I'm not missing something.

There is most likely no Foo.Tests.runtimeconfig.json in the output folder. Which means your test project is not built as exe for some reason.

Yes you're right - in Azure I create the file manually:

bin\Release\net8.0\publish\Foo.Tests.runtimeconfig.json

This still doesn't resolve the issue completely however (see the output I get at the bottom)

Can you try adding <OutputType>exe</OutputType> property to your .csproj?

I get the following error:

CSC : error CS5001: Program does not contain a static 'Main' method suitable for an entry point [

I had to use the following in the .csproj:

        <OutputType>exe</OutputType>
        <GenerateProgramFile>false</GenerateProgramFile>

And I added a 'Main' inside Program.cs on the test project. When the tests are run on Azure however I get this:

  No test is available in D:\a\1\s\Foo.Tests/bin/Release/net8.0/publish/Foo.Tests.dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again.

While on localdev tests are discovered and run just fine. Hmmm ...

PS: The Foo.Tests.csproj looks like so:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        
        <Nullable>enable</Nullable>
        <LangVersion>12.0</LangVersion>
        <ImplicitUsings>enable</ImplicitUsings>

        <IsPackable>false</IsPackable>
        <IsTestProject>true</IsTestProject>

        <!-- https://stackoverflow.com/questions/38085430/the-library-hostpolicy-dll-was-not-found -->
        <!--        <GenerateRuntimeConfigDevFile>true</GenerateRuntimeConfigDevFile>-->

        <OutputType>exe</OutputType>
        <GenerateProgramFile>false</GenerateProgramFile>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Moq" Version="4.20.70" />
        <PackageReference Include="FluentAssertions" Version="7.0.0-alpha.3" />
        <PackageReference Include="System.Linq.Async" Version="6.0.1" />
        
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0-preview-24080-01" />
        <PackageReference Include="Microsoft.TestPlatform" Version="17.10.0-preview-24080-01" />
        <PackageReference Include="Microsoft.TestPlatform.TestHost" Version="17.10.0-preview-24080-01" />

        <PackageReference Include="xunit" Version="2.4.2"/>
        <PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
        <PackageReference Include="coverlet.collector" Version="6.0.0">
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
            <PrivateAssets>all</PrivateAssets>
        </PackageReference>
    </ItemGroup>

    <ItemGroup>
      <ProjectReference Include="..\Foo.csproj" />
    </ItemGroup>

</Project>

Additional factoids:

  • The Foo.csproj is a netstandard2.1 project
  • I'm running the pipeline on Windows-2022 on Azure if that's of any consequence.
  • I also gave 'dotnet vstest' a shot but no dice (no tests get discovered)
  • I upgraded xunit to version 2.6.6 still no joy (no tests get discovered)

It turns out that the warning I get about the tests not being discoverable is because the following dlls are not outputted in the output directory when running the pipeline in Azure:

xunit.runner.reporters.netcoreapp10.dll
xunit.runner.utility.netcoreapp10.dll
xunit.runner.visualstudio.testadapter.dll

I have no idea why this happens.

@nohwnd
Copy link
Member

nohwnd commented Jan 31, 2024 via email

@ksidirop-laerdal
Copy link

How do you build this? Do you do just dotnet publish on the project / solution, or something else?

I have a .targets msbuild-script that builds both Foo.csproj and Foo.csproj like so:

        <!-- RESTORE NUGETS -->
        <MSBuild Projects="Foo.csproj" Targets="Restore"/>

        <!-- COMPILE AND PACK -->
        <MSBuild Projects="Foo.csproj"  Targets="Build"/>

        <MSBuild Projects="Foo.Tests.csproj" Targets="Restore"/>

        <MSBuild Projects="Foo.Tests.csproj"  Targets="Publish"/>

        <Exec Command="  dotnet  test  &quot;Foo.Tests/bin/Release/net8.0/publish/Foo.Tests.dll&quot;  --verbosity normal  --logger &quot;trx;LogFileName=Foo.Tests.trx&quot;  --results-directory &quot;TestResults&quot;  "
              EchoOff="true"
              ConsoleToMSBuild="true"
              WorkingDirectory="$(RootFolder)"/>

@ksidirop-laerdal
Copy link

ksidirop-laerdal commented Jan 31, 2024

Even if I specify the test-adapter-path explicitly to have it point to the local nuget-folder in the filesystem, it's still unable to discover the tests (I get the same warning):

        <PropertyGroup>
            <Params>$(Params) &quot;Foo.Tests/bin/Release/net8.0/publish/Laerdal.Assets.Installers.Tests.dll&quot;</Params>
            <Params>$(Params) --verbosity:normal</Params>
            <Params>$(Params) --logger:&quot;trx;LogFileName=Foo.Tests.trx&quot;</Params>
            <Params>$(Params) --results-directory:&quot;TestResults&quot;</Params>
            <Params>$(Params) --test-adapter-path:&quot;C:\Users\VssAdministrator\.nuget\packages\xunit.runner.visualstudio\2.5.6\build\net6.0&quot;</Params>

            <!-- also tried -->
            <!-- <Params>$(Params) --test-adapter-path:&quot;/c/Users/VssAdministrator/.nuget/packages/xunit.runner.visualstudio/2.5.6/build/net6.0&quot;</Params> -->
        </PropertyGroup>

        <Exec Command="  dotnet  test  $(Params)  "
              EchoOff="false"
              ConsoleToMSBuild="true"
              WorkingDirectory="$(RootFolder)"/>

I'm absolutely baffled by this

@nohwnd
Copy link
Member

nohwnd commented Feb 1, 2024

The original error is coming from the fact that runtimeconfig.json is not present. I would start by generating a binlog from the build locally and on the server and comparing what is going on with runtimeconfig.json. Binary log viewer should make this rather easy, because you will see the targets that are skipped, and why.

All the other errors can be explained also by a "broken" build, so I would start investigating why runtimeconfig.json is not generated first.

I would also try deleting all the bins before every attempt, to make sure that you are not seeing some previous state leaving files in the folder (and making it work).

Your project references all the right nugets, and when I try it locally it builds just fine (I am building via msbuild /t:Publish on the project).

@ksidirop-laerdal
Copy link

The original error is coming from the fact that runtimeconfig.json is not present. I would start by generating a binlog from the build locally and on the server and comparing what is going on with runtimeconfig.json. Binary log viewer should make this rather easy, because you will see the targets that are skipped, and why.

All the other errors can be explained also by a "broken" build, so I would start investigating why runtimeconfig.json is not generated first.

I would also try deleting all the bins before every attempt, to make sure that you are not seeing some previous state leaving files in the folder (and making it work).

Your project references all the right nugets, and when I try it locally it builds just fine (I am building via msbuild /t:Publish on the project).

I did a deep-clean on localdev (deleted bin and obj folders) and re-run my command line scriptlet: You're right the problem can be reproduced on my localdev in this fashion. If I build through my IDE first and then run my scriptlet I can't reproduce this problem anymore - it seems the IDE is taking care of copying additional dlls into the output folders somehow.

@ksidirop-laerdal
Copy link

ksidirop-laerdal commented Feb 1, 2024

Ok here's how I solved it: I simply reverted to calling 'dotnet test' directly on the .csproj while making sure to specify the correct configuration in the build script - somehow this solved all the problems I was witnessing:

        <PropertyGroup>
            <Params>$(Params) &quot;Foo.Tests.csproj&quot;</Params>
            <Params>$(Params) --logger:&quot;trx;LogFileName=Foo.Tests.trx&quot;</Params>
            <Params>$(Params) --verbosity:&quot;minimal&quot;</Params>
            <Params>$(Params) --configuration:&quot;Release&quot;</Params>
            <Params>$(Params) --results-directory:&quot;TestResults&quot;</Params>
        </PropertyGroup>

        <Message Importance="High" Text=""/>
        <Message Importance="High" Text="dotnet  test  $(Params)"/>
        <Message Importance="High" Text=""/>

        <Exec Command="  dotnet  test  $(Params)  "
              EchoOff="false"
              ConsoleToMSBuild="true"
              WorkingDirectory="$(RootFolder)"/>

The moral for me is that 'publish' doesn't quite publish all assets needed for the test-suite to be run properly.

@dfederm
Copy link
Member

dfederm commented Feb 15, 2024

I also ran into this recently and had to add more exclusions to testAssemblyVer2. in particular !**\Microsoft.Testing.Extensions.*.dll

  - task: VSTest@3
    displayName: VSTest
    inputs:
      testSelector: 'testAssemblies'
      testAssemblyVer2: |
        **\*test*.dll
        !**\testhost.dll
        !**\*TestAdapter.dll
        !**\*TestPlatform*.dll
        !**\Microsoft.Testing.Extensions.*.dll
        !**\obj\**

@nohwnd
Copy link
Member

nohwnd commented Feb 15, 2024

@DFeder maybe you could benefit from having the filter search just your bin:

**\bin\**\*Tests.dll
**\bin\**\*Test.dll

#4516

@dfederm
Copy link
Member

dfederm commented Feb 15, 2024

Maybe. It's unfortunate that the docs show examples of stuff that just doesn't work in many cases.

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/vstest-v3?view=azure-pipelines

@nohwnd
Copy link
Member

nohwnd commented Feb 16, 2024

@dfederm Thanks for pointing this out. I've made this to be the default in vstest@2 task on azdo some time ago. And even updated the doc for vstest@2. But now there is a new version of the task and it does not have that change. Made a bug here: microsoft/azure-pipelines-tasks#19555 unfortunately we don't own this task . :/

@nohwnd
Copy link
Member

nohwnd commented Jul 8, 2024

The change to the filter that prevents this was done, but regressed in azdo, unfortunately this is out of our hands, issue was filed here: microsoft/azure-pipelines-tasks#19555

The filter above ( #4743 (comment) ) is what we are recommending to use in azdo, and it was documented for VSTest@2 but not for the regressed @3.

@nohwnd nohwnd closed this as completed Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants