Skip to content

Commit e2881ae

Browse files
authored
Update error message on failing to find hostpolicy for self-contained (#33193)
- Specify if the config wasn't found versus didn't have a framework - Show message for apphost (not just dotnet)
1 parent ecd8378 commit e2881ae

2 files changed

Lines changed: 82 additions & 8 deletions

File tree

src/installer/corehost/cli/fxr/hostpolicy_resolver.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,17 +299,23 @@ bool hostpolicy_resolver::try_get_dir(
299299
// If it still couldn't be found, somebody upstack messed up. Flag an error for the "expected" location.
300300
trace::error(_X("A fatal error was encountered. The library '%s' required to execute the application was not found in '%s'."),
301301
LIBHOSTPOLICY_NAME, expected.c_str());
302-
if (mode == host_mode_t::muxer && !is_framework_dependent)
302+
if ((mode == host_mode_t::muxer || mode == host_mode_t::apphost) && !is_framework_dependent)
303303
{
304-
if (!pal::file_exists(get_app(fx_definitions).get_runtime_config().get_path()))
304+
trace::error(_X("Failed to run as a self-contained app."));
305+
const pal::string_t config_file_name = get_app(fx_definitions).get_runtime_config().get_path();
306+
if (!pal::file_exists(config_file_name))
305307
{
306-
trace::error(_X("Failed to run as a self-contained app. If this should be a framework-dependent app, add the %s file specifying the appropriate framework."),
307-
get_app(fx_definitions).get_runtime_config().get_path().c_str());
308+
trace::error(_X(" - The application was run as a self-contained app because '%s' was not found."),
309+
config_file_name.c_str());
310+
trace::error(_X(" - If this should be a framework-dependent app, add the '%s' file and specify the appropriate framework."),
311+
config_file_name.c_str());
308312
}
309313
else if (get_app(fx_definitions).get_name().empty())
310314
{
311-
trace::error(_X("Failed to run as a self-contained app. If this should be a framework-dependent app, specify the appropriate framework in %s."),
312-
get_app(fx_definitions).get_runtime_config().get_path().c_str());
315+
trace::error(_X(" - The application was run as a self-contained app because '%s' did not specify a framework."),
316+
config_file_name.c_str());
317+
trace::error(_X(" - If this should be a framework-dependent app, specify the appropriate framework in '%s'."),
318+
config_file_name.c_str());
313319
}
314320
}
315321
return false;

src/installer/test/HostActivation.Tests/PortableAppActivation.cs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.DotNet.Cli.Build;
56
using Microsoft.DotNet.Cli.Build.Framework;
67
using System;
78
using System.Diagnostics;
@@ -249,7 +250,7 @@ public void AppHost_FrameworkDependent_Succeeds()
249250
var appExe = fixture.TestProject.AppExe;
250251
File.Copy(sharedTestState.BuiltAppHost, appExe, overwrite: true);
251252
AppHostExtensions.BindAppHost(appExe);
252-
253+
253254
// Get the framework location that was built
254255
string builtDotnet = fixture.BuiltDotnet.BinPath;
255256

@@ -348,6 +349,61 @@ public void ComputedTPADoesntEndWithPathSeparator()
348349
.And.HaveStdErrMatching($"Property TRUSTED_PLATFORM_ASSEMBLIES = .*[^{Path.PathSeparator}]$", System.Text.RegularExpressions.RegexOptions.Multiline);
349350
}
350351

352+
[Theory]
353+
[InlineData(true)]
354+
[InlineData(false)]
355+
public void MissingRuntimeConfig_Fails(bool useAppHost)
356+
{
357+
Command command;
358+
if (useAppHost)
359+
{
360+
command = Command.Create(sharedTestState.MockApp.AppExe)
361+
.DotNetRoot(sharedTestState.BuiltDotNet.BinPath);
362+
}
363+
else
364+
{
365+
command = sharedTestState.BuiltDotNet.Exec(sharedTestState.MockApp.AppDll);
366+
}
367+
368+
string hostPolicyName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy");
369+
command.EnableTracingAndCaptureOutputs()
370+
.MultilevelLookup(false)
371+
.Execute()
372+
.Should().Fail()
373+
.And.HaveStdErrContaining($"The library '{hostPolicyName}' required to execute the application was not found")
374+
.And.HaveStdErrContaining("Failed to run as a self-contained app")
375+
.And.HaveStdErrContaining($"'{sharedTestState.MockApp.RuntimeConfigJson}' was not found");
376+
}
377+
378+
[Theory]
379+
[InlineData(true)]
380+
[InlineData(false)]
381+
public void MissingFrameworkInRuntimeConfig_Fails(bool useAppHost)
382+
{
383+
TestApp app = sharedTestState.MockApp.Copy();
384+
RuntimeConfig.FromFile(app.RuntimeConfigJson).Save();
385+
386+
Command command;
387+
if (useAppHost)
388+
{
389+
command = Command.Create(app.AppExe)
390+
.DotNetRoot(sharedTestState.BuiltDotNet.BinPath);
391+
}
392+
else
393+
{
394+
command = sharedTestState.BuiltDotNet.Exec(app.AppDll);
395+
}
396+
397+
string hostPolicyName = RuntimeInformationExtensions.GetSharedLibraryFileNameForCurrentPlatform("hostpolicy");
398+
command.EnableTracingAndCaptureOutputs()
399+
.MultilevelLookup(false)
400+
.Execute()
401+
.Should().Fail()
402+
.And.HaveStdErrContaining($"The library '{hostPolicyName}' required to execute the application was not found")
403+
.And.HaveStdErrContaining("Failed to run as a self-contained app")
404+
.And.HaveStdErrContaining($"'{app.RuntimeConfigJson}' did not specify a framework");
405+
}
406+
351407
[Theory]
352408
[InlineData(true)]
353409
[InlineData(false)]
@@ -459,7 +515,7 @@ public void AppHost_GUI_FrameworkDependent_DisabledGUIErrors_DialogNotShown()
459515

460516
var fixture = sharedTestState.PortableAppFixture_Built
461517
.Copy();
462-
518+
463519
string appExe = fixture.TestProject.AppExe;
464520
File.Copy(sharedTestState.BuiltAppHost, appExe, overwrite: true);
465521
AppHostExtensions.BindAppHost(appExe);
@@ -579,13 +635,18 @@ public class SharedTestState : IDisposable
579635
{
580636
public TestProjectFixture PortableAppFixture_Built { get; }
581637
public TestProjectFixture PortableAppFixture_Published { get; }
638+
582639
public RepoDirectoriesProvider RepoDirectories { get; }
583640
public string BuiltAppHost { get; }
641+
public DotNetCli BuiltDotNet { get; }
642+
643+
public TestApp MockApp { get; }
584644

585645
public SharedTestState()
586646
{
587647
RepoDirectories = new RepoDirectoriesProvider();
588648
BuiltAppHost = Path.Combine(RepoDirectories.HostArtifacts, RuntimeInformationExtensions.GetExeFileNameForCurrentPlatform("apphost"));
649+
BuiltDotNet = new DotNetCli(RepoDirectories.BuiltDotnet);
589650

590651
PortableAppFixture_Built = new TestProjectFixture("PortableApp", RepoDirectories)
591652
.EnsureRestored(RepoDirectories.CorehostPackages)
@@ -594,12 +655,19 @@ public SharedTestState()
594655
PortableAppFixture_Published = new TestProjectFixture("PortableApp", RepoDirectories)
595656
.EnsureRestored(RepoDirectories.CorehostPackages)
596657
.PublishProject();
658+
659+
MockApp = new TestApp(SharedFramework.CalculateUniqueTestDirectory(Path.Combine(TestArtifact.TestArtifactsPath, "portableAppActivation")), "App");
660+
Directory.CreateDirectory(MockApp.Location);
661+
File.WriteAllText(MockApp.AppDll, string.Empty);
662+
File.Copy(BuiltAppHost, MockApp.AppExe);
663+
AppHostExtensions.BindAppHost(MockApp.AppExe);
597664
}
598665

599666
public void Dispose()
600667
{
601668
PortableAppFixture_Built.Dispose();
602669
PortableAppFixture_Published.Dispose();
670+
MockApp.Dispose();
603671
}
604672
}
605673
}

0 commit comments

Comments
 (0)