runtime: The library 'hostpolicy.dll' required to execute the application was not found in
From @sillyousu on Tuesday, 30 April 2019 02:19:35
Steps to reproduce
create a hello world project:
mkdir ClassLib
cd ClassLib
dotnet new classlib
cd ..
mkdir Main
cd Main
dotnet new console
dotnet add reference ..\ClassLib\ClassLib.csproj
dotnet build
dotnet .\bin\Debug\netcoreapp2.2\ClassLib.dll
.
├── ClassLib
│ ├── Class1.cs
│ └── ClassLib.csproj
└── Main
├── Main.csproj
└── Program.cs
Expected behavior
meaningful error message
Actual behavior
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Users\supei\xd\sims\fooo\Main\bin\Debug\netcoreapp2.2\'.
Failed to run as a self-contained app. If this should be a framework-dependent app, add the C:\Users\supei\xd\sims\fooo\Main\bin\Debug\netcoreapp2.2\FooA.runtimeconfig.json file specifying the appropriate framework.
Environment data
dotnet --info output:
PS C:\Users\supei\xd\sims\fooo\Main> dotnet.exe --info
.NET Core SDK(反映任何 global.json):
Version: 2.2.100
Commit: b9f2fa0ca8
运行时环境:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.100\
Host (useful for support):
Version: 2.2.0
Commit: 1249f08fed
.NET Core SDKs installed:
1.1.8 [C:\Program Files\dotnet\sdk]
1.1.9 [C:\Program Files\dotnet\sdk]
1.1.11 [C:\Program Files\dotnet\sdk]
2.1.101 [C:\Program Files\dotnet\sdk]
2.1.103 [C:\Program Files\dotnet\sdk]
2.1.104 [C:\Program Files\dotnet\sdk]
2.1.200 [C:\Program Files\dotnet\sdk]
2.1.201 [C:\Program Files\dotnet\sdk]
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.300 [C:\Program Files\dotnet\sdk]
2.1.403 [C:\Program Files\dotnet\sdk]
2.1.500 [C:\Program Files\dotnet\sdk]
2.1.602 [C:\Program Files\dotnet\sdk]
2.2.100 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 1.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.0.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 1.1.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
Copied from original issue: dotnet/cli#11267
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 25
- Comments: 72 (32 by maintainers)
@robertborr @borysl I found the solution to “My” problem. Notice the following line contains
**\*test*.dllThis means that “Any” file that has test in the filename and is a dll will be used.XUnit places a file called
testhost.dllin the output directory, which matches the glob and is based on.netcore 1.xhence the need of the runner to attempt to use that as the runner policy for that test.dllI changed the filer to be
**\*test.dll(removed the last*) and now all is happy again; i.e. the first message is not displayed and th xunit runner still works as expected.I’ve reproduced this issue when was running xunit tests with the project reference
Same here via a
azure-pipelines.ymlfile. Needing to run VSTest@2 due to needing to use rosyln against a .netcore 2.2 test dll that also performs runtime compile tests against output files. Here is the yml extract:I then get the following for each of the Test.dlls
But the files still run against
Xunit. (See the end) BUT, because of the errors, theVSTestaction fails!I have the same issue as @canpolat . It started as soon one XUnit test project referenced another one. The issue occurs only in the Azure DevOps Pipeline. VS 2019 is able to run all tests.
I can confirm: Upgrading to Microsoft.NET.Test.SDK version 16.6.1 did not solve it.
At the moment I use a very dirty workaround to overcome this error. Is there a better solution than removing the reference to another XUnit test project?
I have the exact same problem…
This error case happens whenever we try to run any class library (not just
netstandard)..runtimeconfig.json- so it thinks it should be a self-contained apphostpolicyeither - and failsI don’t think there’s any specific bit of information which the host could use to determine the exact scenario. What we could do is extend the error message with something like:
As mentioned in the preceding posts, in most cases the actual source of the issue seems to be that *test.dll is the default when creating a VSTest task. This includes testhost.dll and the test task tries to execute it as a test dll. One way to reduce the frequency in wich this issue occurs is to add the exclusion !*testhost.dll to the default for the VSTest task as it is already done with !*TestAdapter.dll. Concerning a better error message, adding something like "If this error occurs in a test task, excluding testhost.dll via !**testhost.dll may solve the issue" might help users to find a solution pretty fast.
I came to this page because of having the same message in my logs.
TLDR: I was able to solve my problem by removing a project reference from one test-assembly to another.
Long description with a hint about searching for causes of failed builds: Have the same issue with an in azure hosted build agent and with an on-prem build agent when running a VSTest task:
As you can see in the task definition, it specifies to only get files matching “YYY.*.Tests.dll”, which only includes my test projects.
Unfortunately, when clicking on the link of the commit hash of the “run”, you see the changes of that particular commit - not the changes between ins run and the previous run. Between the last “successful run” and the first “failing run” I did several commits locally that I pushed to Azure DevOps in one single push operation.
So, the changes shown with the “failing run” may not include the cause of the failure - you have to find the changes between the two commits manually.
I had this same problem today and upgrading to Microsoft.NET.Test.SDK version 16.6.1 did not solve it. But some other comments in this thread helped me, so here is my story, hopefully it helps someone 😃
The problem in my case was inheritance.
I had a Shared XUnit test project that:
Due to the second point, all other test projects were referencing to this Shared test project and therefore the Azure Pipelines test runner was trying to run the Shared test project after each and every other test project. And, for some reason that was failing.
I created a separate project for the two base test classes and removed the references to Shared test project. This solved the problem in my case (I also named the new project so that the filter does not pick it up).
BTW: Everything worked fine when run locally on Visual Studio, but the tests failed when I ran them in the Azure Pipeline.
Add this json to the application.runtimeconfig.json file. { “runtimeOptions”: { “tfm”: “netcoreapp2.2”, “framework”: { “name”: “Microsoft.NETCore.App”, “version”: “2.0.0” } } }
My guess is that:
.\srm.dlland because the process starts with current directory inD:\Windows\system32it gets resolved asD:\Windows\system32\srm.dll.D:\Windows\system32\app.dllexists - it’s part of Windows (it’s a native dll). But the host doesn’t look into the file, the fact that it exists is enough to “try to run it”.hostpolicy.dllin it. (Note that it thinks the application directory isD:\Windows\system32).If you rename your app to any name which cannot be found in System32 I would expect the process to fail, unless there’s some other magic which tries it multiple times with different “current directory”.
I think the root issue here is that the hosting should not run with current directory set to System32. There should be a well defined, application dependent directory which is used as the current directory. Or alternatively it should not allow relative paths in the configuration - as those can be easily misinterpreted.
When you encounter this problem with vstest (mstest, xunit or nunit), please update your Microsoft.NET.Test.SDK to 16.6.1 where this problem was solved. https://github.com/microsoft/vstest/commit/3726dae401bd4ca1e8cbe16d869cab5e9d673ed6
Hey all, I came across, and fixed (for me) the same error today when setting up a build in Azure. I was playing around with the command-line test runner, wondering if it was an issue with different assemblies using different .NET versions (I’ve had problems with that before, we have a couple of rather large solutions with multiple projects using different .NET versions), and noticed that one of the assemblies was appearing in the \bin directory for one of the others.
Once this was fixed (i.e. move the ‘shared’ code up into a common assembly), everything started working.
Before:
TestAssembly1 -> reference TestCommon TestAssembly2 -> reference TestCommon & TestAssembly1
After:
TestAssembly1 -> reference TestCommon TestAssembly2 -> reference TestCommon
Hope this helps someone else 😃
The host will always first look for
hostfxrnext to the executable, then fallback toDOTNET_ROOT, followed by shared global installs.For the
DOTNET_ROOTenvironment variable, it is expected to point to the root directory with a specific layout - when that variable is set, the host will look forhostfxrunder%DOTNET_ROOT%\host\fxr\<version>.I haven’t been able to think of a way to configure having the
testhost.exebe able to handle being given a self-contained config. Running thetesthost.dllthroughdotnet.exewould work though - e.g.dotnet exec --runtimeconfig <pathToRuntimeConfigJson> --depsfile <pathToDepsJson> <nugetCache>\microsoft.testplatform.testhost\16.4.0\build\netcoreapp2.1\x64\testhost.dllThanks for the repro @mikail-gultekin. Would you be able to check if you still hit this issue if you don’t make the test project self-contained (should just be removing the
<RuntimeIdentifier>property)?It looks like this is an issue when the test project is set to build self-contained. I was able to repro the same error by grabbing the unit-testing-using-dotnet-test sample and adding
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>.I think this is because, when run through VS,
testhost- which appears to be a built apphost - is used to discover tests and given the runtimeconfig and deps files of the test project through the--runtimeconfigand--depsfilecommand line arguments. The runtimeconfig of the test project indicates that it is self-contained (which dictates where the host looks forhostpolicy), but thetesthostapp is not, leading to the error. The desired host behaviour around this is not clear to me: #33207.The .NET Core Test Explorer extension in VSCode runs through
dotnet testdirectly to discover and then run, so it does not hit this.Hi,
In our team at my company, all the developers have this problem when we try to run tests on Visual Studio Test Explorer. Here is the test compiler output message:
Project settings (from .csproj):
We use x64 based PCs. Processor Architecture for AnyCPU Projects: x64
Any ideas to solve our case?
Thanks.
Depends on what the goal of the test is. I would keep at least some tests which start the process - to have a full E2E validation. But for unit-test-style testing, I would prefer something more direct. Either invoke Main or maybe even true unit testing by invoking internal classes (and use
InternalsVisibleToAttribute). But this is just a personal bias, so take it with a grain of salt. The main reasons I prefer more direct testing:I’m involved in projects which use both approaches
@asasine This is a known limitation of publish currently. In short all three
restore,buildandpublishneed to be done with the same properties, otherwise it’s not guaranteed to work (using--no-restoreand--no-buildthat is).In this case it’s exaggerated by the unfortunate behavior that specifying just
--runtime linux-x64automatically implies--self-contained true. So in your sample the build is done as self-contained, while the publish is then done as framework-dependent - and things breaks.I would recommend to not use
--no-build(and in some cases also--no-restore) as it tends to break publish in “weird” ways. If you set everything in the project file and only rundotnet build,dotnet publish --no-build- that should work, but adding more to the command line can cause weird behavior.I have this error when:
TestProjectA -> references TestProjectB
Solution is:
Or
e.g.
Just want to make some things easier to read:
runtimeconfig.jsonor it doesn’t containframework/frameworksproperty then it represents a self-contained app. (This includes the case where there’sincludedFrameworksproperty - that one is only used for self-contained apps and is only used for dynamic loading of dependencies - which is typically not used at all by test projects).hostfxr(andhostpolicy) in the same folder as the app executable.I think we need to first figure out what is the story around self-contained and tests:
Similarly I think we need to be explicit about supporting
OutputType=exewithin test projects. This does weird things:mytest.exein the output - which does nothing (it runs the auto-generatedMainwhich is a no-op). Basically very confusing - if I get an.exeI should be able to run it. Or at the very least it should tell me “this is not how you run this thing”.Also worth looking into is interaction between running tests and publish properties like
PublishTrimmedand similar.There are specific issues which makes the experience worse:
Other random observations:
dotnet test -r win-x64has completely different effect from my expectations - the-rin this case means “result directory”, not RID. Seems very confusing.Tried this and I am able to trigger this with:
Ultimately it boils down to this. The RED is the one that fails:
The problem can be replicated also by running the test host by itself using the commands we see in logs and reducing the parameters to just depsfile and runtime config (don’t get confused by the access denied error in the second case, that is test host running correctly):
@elinor-fung do you see anything that we could do to allow the test host to run?
@joanna1010 As was already mentioned above in this issue, classlib projects are not meant to be executed via
dotnet project.dll. Ignoring all other problems, what method should that actually invoke in the managed code? Applications have clearly defined entry point - theMain. But classlibs don’t have anything like that.I do agree that it would be nice to provide a better error message, and we’ll try to look into that.
Regarding Azure Functions - I don’t know how exactly should Azure Functions be executed, but I would be very surprised if it was
dotnet classlib.dll. If you’re having issues running your Azure Function project, please create an issue in the respective area. I don’t know for sure, but https://github.com/Azure/Azure-Functions looks promising.I don’t think that’s bug or something need change, you can not run
dotnet/dotnet execwith a class library, the class library had no entry point for an executable program, maybe more clearly error info