Skip to content

Commit fc5252e

Browse files
authored
Add a flag to hostfxr_resolve_sdk2_flags_t to silence error messages (dotnet#119729)
hostfxr_resolve_sdk2 will print error messages to stderr by default. We can add a flag to the enum to allow for silencing these errors when we want to try to resolve an sdk, but a failure to resolve shouldn't be surfaced to the console. See dotnet/sdk#50632 for context.
1 parent c6f83d3 commit fc5252e

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

docs/design/features/hosting-layer-apis.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ If the application is successfully executed, this value will return the exit cod
6161
enum hostfxr_resolve_sdk2_flags_t
6262
{
6363
disallow_prerelease = 0x1,
64+
do_not_print_errors = 0x2,
6465
};
6566
6667
enum hostfxr_resolve_sdk2_result_key_t

src/installer/tests/Assets/Projects/HostApiInvokerApp/HostFXR.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal static class hostfxr
1616
internal enum hostfxr_resolve_sdk2_flags_t : int
1717
{
1818
disallow_prerelease = 0x1,
19+
do_not_print_errors = 0x2,
1920
}
2021

2122
internal enum hostfxr_resolve_sdk2_result_key_t : int

src/installer/tests/HostActivation.Tests/NativeHostApis.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,30 @@ public void Hostfxr_resolve_sdk2_NoGlobalJson_DisallowPrerelease()
159159
.And.HaveStdOutContaining($"{api} data:[{expectedData}]");
160160
}
161161

162+
[Theory]
163+
[InlineData(true)]
164+
[InlineData(false)]
165+
public void Hostfxr_resolve_sdk2_NoGlobalJson_NoWriteToStdResolvesTheSame(bool do_not_print_errors)
166+
{
167+
// With no global.json and disallowing previews, pick latest non-preview
168+
169+
var f = sharedTestState.SdkAndFrameworkFixture;
170+
string expectedData = string.Join(';', new[]
171+
{
172+
("resolved_sdk_dir", Path.Combine(f.LocalSdkDir, "1.2.300")),
173+
("global_json_state", "not_found"),
174+
});
175+
176+
string api = ApiNames.hostfxr_resolve_sdk2;
177+
string flags = do_not_print_errors ? "disallow_prerelease,do_not_print_errors" : "disallow_prerelease";
178+
TestContext.BuiltDotNet.Exec(sharedTestState.HostApiInvokerApp.AppDll, api, f.ExeDir, NoGlobalJson, flags)
179+
.EnableTracingAndCaptureOutputs()
180+
.Execute()
181+
.Should().Pass()
182+
.And.ReturnStatusCode(api, Constants.ErrorCode.Success)
183+
.And.HaveStdOutContaining($"{api} data:[{expectedData}]");
184+
}
185+
162186
[Fact]
163187
public void Hostfxr_resolve_sdk2_GlobalJson_DisallowPrerelease()
164188
{
@@ -301,6 +325,32 @@ public void Hostfxr_resolve_sdk2_GlobalJson_InvalidDataNoFallback()
301325
}
302326
}
303327

328+
[Theory]
329+
[InlineData(false)]
330+
[InlineData(true)]
331+
public void Hostfxr_resolve_sdk2_FailsToResolve_NoWriteToStd(bool do_not_print_errors)
332+
{
333+
var f = sharedTestState.SdkAndFrameworkFixture;
334+
string api = ApiNames.hostfxr_resolve_sdk2;
335+
using TestArtifact workingDir = TestArtifact.Create(nameof(Hostfxr_resolve_sdk2_FailsToResolve_NoWriteToStd));
336+
337+
string invalidVersion = "1.2.0"; // feature band < 1 triggers __invalid_data_no_fallback
338+
GlobalJson.CreateWithVersion(workingDir.Location, invalidVersion);
339+
340+
var result = TestContext.BuiltDotNet.Exec(sharedTestState.HostApiInvokerApp.AppDll, api, f.ExeDir, workingDir.Location, do_not_print_errors ? "do_not_print_errors" : "0")
341+
.CaptureStdOut()
342+
.CaptureStdErr()
343+
.Execute();
344+
345+
result.Should().Pass()
346+
.And.ReturnStatusCode(api, Constants.ErrorCode.SdkResolveFailure);
347+
348+
if (do_not_print_errors)
349+
result.StdErr.Should().BeEmpty();
350+
else
351+
result.StdErr.Should().Contain("A compatible .NET SDK was not found.");
352+
}
353+
304354
[Fact]
305355
public void Hostfxr_corehost_set_error_writer_test()
306356
{

src/native/corehost/fxr/hostfxr.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ SHARED_API int32_t HOSTFXR_CALLTYPE hostfxr_resolve_sdk(
166166
enum hostfxr_resolve_sdk2_flags_t : int32_t
167167
{
168168
disallow_prerelease = 0x1,
169+
do_not_print_errors = 0x2,
169170
};
170171

171172
enum class hostfxr_resolve_sdk2_result_key_t : int32_t
@@ -218,6 +219,8 @@ namespace
218219
// disallow_prerelease (0x1)
219220
// do not allow resolution to return a prerelease SDK version
220221
// unless prerelease version was specified via global.json.
222+
// do_not_print_errors (0x2)
223+
// do not write any error messages to stderr.
221224
//
222225
// result
223226
// Callback invoked to return values. It can be invoked more
@@ -282,7 +285,9 @@ SHARED_API int32_t HOSTFXR_CALLTYPE hostfxr_resolve_sdk2(
282285
working_dir,
283286
(flags & hostfxr_resolve_sdk2_flags_t::disallow_prerelease) == 0);
284287

285-
auto resolved_sdk_dir = resolver.resolve(exe_dir);
288+
auto resolved_sdk_dir = resolver.resolve(
289+
exe_dir,
290+
(flags & hostfxr_resolve_sdk2_flags_t::do_not_print_errors) == 0);
286291
if (!resolved_sdk_dir.empty())
287292
{
288293
result(

0 commit comments

Comments
 (0)