Skip to content

Ship hosting layer error codes as a public header - #131617

Merged
elinor-fung merged 3 commits into
dotnet:mainfrom
elinor-fung:host-error-codes-header
Jul 31, 2026
Merged

Ship hosting layer error codes as a public header#131617
elinor-fung merged 3 commits into
dotnet:mainfrom
elinor-fung:host-error-codes-header

Conversation

@elinor-fung

@elinor-fung elinor-fung commented Jul 30, 2026

Copy link
Copy Markdown
Member

Ships the hosting StatusCode enum as a public host_error_codes.h header (alongside nethost.h and hostfxr.h) so native host consumers no longer need to manually redefine the error/exit codes returned by the hostfxr, hostpolicy, and nethost APIs. The header is packaged in the Microsoft.NETCore.DotNetAppHost package and is validated to compile as pure C.

Resolves #110891

cc @dotnet/appmodel @AaronRobinsonMSFT

Note

This pull request was authored with the assistance of GitHub Copilot.

Ship the StatusCode enum as a public host_error_codes.h header so native
host consumers no longer need to manually redefine the error/exit codes
returned by the hostfxr, hostpolicy, and nethost APIs.

- Install error_codes.h as host_error_codes.h alongside the other corehost
  headers, and add it to the Microsoft.NETCore.DotNetAppHost package and the
  dotnet-nethost archive.
- Guard the C++-only STATUS_CODE_SUCCEEDED macro under __cplusplus so the
  shipped header is valid C.
- Include the header in test_c_api.c to verify it compiles as pure C.

Related to dotnet#110891

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: af20686e-c3d9-4b1d-bf4c-a077a82e74a3
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).
12 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes the host-layer StatusCode values available as a shipped public C header by installing src/native/corehost/error_codes.h as host_error_codes.h alongside other hosting headers and ensuring it can be included from pure C code.

Changes:

  • Install and package error_codes.h as host_error_codes.h (for nethost/apphost consumers).
  • Update packaging projects to include the new header in the output artifacts.
  • Extend the pure-C header inclusion test to include error_codes.h.
Show a summary per file
File Description
src/native/corehost/test/mockhostfxr/test_c_api.c Adds error_codes.h to the pure-C include validation test.
src/native/corehost/nethost/CMakeLists.txt Installs error_codes.h under the public name host_error_codes.h.
src/native/corehost/error_codes.h Wraps the C++-only static_cast macro usage to keep the header C-compatible.
src/installer/pkg/projects/Microsoft.NETCore.DotNetAppHost/Microsoft.NETCore.DotNetAppHost.pkgproj Adds host_error_codes.h to the DotNetAppHost package contents.
src/installer/pkg/archives/dotnet-nethost.proj Publishes host_error_codes.h into the dotnet-nethost archive output.

Copilot's findings

  • Files reviewed: 5/5 changed files
  • Comments generated: 2

Comment thread src/native/corehost/error_codes.h Outdated
- Provide a C-safe STATUS_CODE_SUCCEEDED definition for C consumers instead
  of omitting the macro, keeping the existing C++ static_cast version.
- Include host_error_codes.h in the Microsoft.NETCore.App.Host runtime pack
  alongside the other public hosting headers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: af20686e-c3d9-4b1d-bf4c-a077a82e74a3
Copilot AI review requested due to automatic review settings July 30, 2026 21:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Comments suppressed due to low confidence (1)

src/native/corehost/error_codes.h:59

  • STATUS_CODE_SUCCEEDED is now only defined for C++, which means C consumers (the stated target for this header) can't use the convenience macro at all. Since the goal is a header that works as pure C, consider providing a C-compatible definition under #else (using enum StatusCode + C-style casts) rather than omitting the macro entirely.
#ifdef __cplusplus
#define STATUS_CODE_SUCCEEDED(status_code) ((static_cast<int>(static_cast<StatusCode>(status_code))) >= 0)
#else
  • Files reviewed: 6/6 changed files
  • Comments generated: 0 new

Copilot AI review requested due to automatic review settings July 30, 2026 22:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Comments suppressed due to low confidence (1)

src/native/corehost/error_codes.h:61

  • This header is now being shipped publicly as host_error_codes.h (via CMake install/package). The include guard __ERROR_CODES_H__ is both very generic and uses a reserved identifier form (leading __), which increases collision risk for consumers and is technically undefined-behavior territory in public headers. Consider switching to a unique, non-reserved guard name (e.g., HAVE_HOST_ERROR_CODES_H to match other public hosting headers) and update the trailing #endif comment accordingly.
#ifdef __cplusplus
#define STATUS_CODE_SUCCEEDED(status_code) ((static_cast<int>(static_cast<StatusCode>(status_code))) >= 0)
#else
#define STATUS_CODE_SUCCEEDED(status_code) (((int)(status_code)) >= 0)
#endif // __cplusplus
  • Files reviewed: 6/6 changed files
  • Comments generated: 0 new

Comment thread src/native/corehost/error_codes.h Outdated
Co-authored-by: Aaron R Robinson <arobins@microsoft.com>
Copilot AI review requested due to automatic review settings July 30, 2026 22:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

Comments suppressed due to low confidence (2)

src/installer/pkg/projects/Microsoft.NETCore.DotNetAppHost/Microsoft.NETCore.DotNetAppHost.pkgproj:13

  • This NativeBinary header list appears to be kept in filename order (coreclr_delegates.h, host*, nethost.h). Adding host_error_codes.h after hostfxr.h breaks that ordering; please keep the list sorted to reduce churn in future diffs.
    <NativeBinary Include="$(DotNetHostBinDir)/coreclr_delegates.h" />
    <NativeBinary Include="$(DotNetHostBinDir)/hostfxr.h" />
    <NativeBinary Include="$(DotNetHostBinDir)/host_error_codes.h" />
    <NativeBinary Include="$(DotNetHostBinDir)/nethost.h" />

src/native/corehost/error_codes.h:57

  • STATUS_CODE_SUCCEEDED relies on casting to int and comparing >= 0. Since the StatusCode enum contains values larger than INT_MAX, the enum’s underlying type can become unsigned and the signed conversion is implementation-defined. For a public C header, it’s safer to check the high bit directly, while keeping static_cast for C++ builds.
#define STATUS_CODE_SUCCEEDED(status_code) (((int)(status_code)) >= 0)
  • Files reviewed: 6/6 changed files
  • Comments generated: 0 new

@elinor-fung

Copy link
Copy Markdown
Member Author

/ba-g dead-letter in unrelated wasm leg

@elinor-fung
elinor-fung merged commit db6e4f9 into dotnet:main Jul 31, 2026
170 of 173 checks passed
@github-project-automation github-project-automation Bot moved this to Done in AppModel Jul 31, 2026
@elinor-fung
elinor-fung deleted the host-error-codes-header branch July 31, 2026 02:32
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-rc1 milestone Aug 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

hostfxr_* Functions Return int Instead of StatusCode Enum

4 participants