Skip to content

Conversation

@rolfbjarne
Copy link
Member

  • Enable support by default, except when Optimize=true (aka Release builds).
  • Set the STARTUP_HOOKS runtime property if the DOTNET_STARTUP_HOOKS
    environment variable is set - this is typically done at startup by CoreCLR
    or MonoVM, but we're using both in an embedding mode, so we have to do it
    ourselves.
  • Add a test.

Refs:

Fixes #24492.

* Enable support by default, except when Optimize=true (aka Release builds).
* Set the STARTUP_HOOKS runtime property if the DOTNET_STARTUP_HOOKS
  environment variable is set - this is typically done at startup by CoreCLR
  or MonoVM, but we're using both in an embedding mode, so we have to do it
  ourselves.
* Add a test.

Refs:

* dotnet/android#10670
* https://github.com/dotnet/runtime/blob/242f7b23752599f22157268de41fee91cb97ef6c/docs/design/features/host-startup-hook.md
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes Startup Hook support for .NET-on-Apple platforms by enabling it by default for non-optimized builds, explicitly flowing DOTNET_STARTUP_HOOKS into the runtime property bag, and adding a regression test to validate behavior.

Changes:

  • Enable StartupHookSupport by default except when Optimize=true (typical Release builds).
  • Pass STARTUP_HOOKS host property to the embedded runtime when DOTNET_STARTUP_HOOKS is set.
  • Add a new StartupHookTest app + NUnit test coverage, and extend the test helper to allow asserting non-zero exit codes.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
tests/dotnet/UnitTests/TestBaseClass.cs Allows asserting an expected (non-zero) exit code when running test apps.
tests/dotnet/UnitTests/StartupHookTest.cs Adds unit tests covering startup hook enabled/disabled behavior across configurations.
tests/dotnet/StartupHookTest/AppDelegate.cs Adds a minimal app that reports whether the startup hook ran.
tests/dotnet/StartupHookTest/shared.csproj Shared project definition for the new startup hook test app.
tests/dotnet/StartupHookTest/shared.mk Shared make configuration to integrate the new test app into existing test infrastructure.
tests/dotnet/StartupHookTest/Makefile Hooks the new test app into the dotnet test make targets.
tests/dotnet/StartupHookTest/iOS/StartupHookTest.csproj iOS target project for the new test app.
tests/dotnet/StartupHookTest/iOS/Makefile iOS make target for the new test app.
tests/dotnet/StartupHookTest/tvOS/StartupHookTest.csproj tvOS target project for the new test app.
tests/dotnet/StartupHookTest/tvOS/Makefile tvOS make target for the new test app.
tests/dotnet/StartupHookTest/macOS/StartupHookTest.csproj macOS target project for the new test app.
tests/dotnet/StartupHookTest/macOS/Makefile macOS make target for the new test app.
tests/dotnet/StartupHookTest/MacCatalyst/StartupHookTest.csproj Mac Catalyst target project for the new test app.
tests/dotnet/StartupHookTest/MacCatalyst/Makefile Mac Catalyst make target for the new test app.
runtime/runtime.m Forwards startup hooks from DOTNET_STARTUP_HOOKS into the runtime host property STARTUP_HOOKS.
dotnet/targets/Xamarin.Shared.Sdk.targets Adjusts default StartupHookSupport behavior and reserves the STARTUP_HOOKS runtimeconfig property.

Comment on lines +29 to +30
env = new Dictionary<string, string?> ();
ExecuteWithMagicWordAndAssert (appExecutable, env, expectedExitCode: 1); // this should fail
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

Passing an empty environment dictionary here does not unset DOTNET_STARTUP_HOOKS for the child process; Execution.RunAsync inherits the parent environment unless a key is explicitly removed (value=null). This can make the test flaky if DOTNET_STARTUP_HOOKS is set in the test runner environment. Set DOTNET_STARTUP_HOOKS to null in the env dictionary for the "no startup hook" case instead of using an empty dictionary.

Copilot uses AI. Check for mistakes.
Comment on lines +55 to +56
env = new Dictionary<string, string?> ();
ExecuteWithMagicWordAndAssert (appExecutable, env, expectedExitCode: 1); // this should fail
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

Same issue as above: using an empty env dictionary does not clear DOTNET_STARTUP_HOOKS inherited from the parent process. Explicitly set DOTNET_STARTUP_HOOKS=null to ensure startup hooks are disabled for this run.

Copilot uses AI. Check for mistakes.
Comment on lines +82 to +83
env = new Dictionary<string, string?> ();
ExecuteWithMagicWordAndAssert (appExecutable, env, expectedExitCode: 1); // this should fail
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

Same issue as above: passing an empty env dictionary won't remove DOTNET_STARTUP_HOOKS from the inherited environment. Set DOTNET_STARTUP_HOOKS=null for the "should fail" execution to make the test deterministic.

Copilot uses AI. Check for mistakes.
Comment on lines +21 to +24
public static bool Initialized { get; private set; }
public static void Initialize ()
{
Console.WriteLine ("STARTUP");
Copy link

Copilot AI Feb 10, 2026

Choose a reason for hiding this comment

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

Formatting/style: this repo's C# style uses a space before parentheses in method declarations/calls (e.g. Main (string [] args) in other dotnet test apps). Update Initialize() to Initialize () (and consider aligning brace placement with the surrounding code for consistency).

Copilot uses AI. Check for mistakes.
Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

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

The overall good looks good to me. 👍

But I think you should put the startup hook in a class library for testing.

Comment on lines +144 to +146
<!-- StartupHookSupport is used for Hot Reload, so we enable it unless the Optimize flag is set. -->
<StartupHookSupport Condition="'$(StartupHookSupport)' == '' And '$(Optimize)' != 'true'">true</StartupHookSupport>
<StartupHookSupport Condition="'$(StartupHookSupport)' == ''">false</StartupHookSupport>
Copy link
Member

Choose a reason for hiding this comment

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

On Android I also had this in a block for "applications" (that have OutputType=Exe or AndroidApplication=true).

It may not matter, though.

}
}

class StartupHook {
Copy link
Member

Choose a reason for hiding this comment

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

You may want to actually test this in a separate class library like dotnet watch will use. I got different startup behavior when I did this, because it was a library different than the main app.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #0feba87] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 0feba87bcd06d48b9d0132426e08393c282d5205 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [PR Build #0feba87] Build passed (Detect API changes) ✅

Pipeline on Agent
Hash: 0feba87bcd06d48b9d0132426e08393c282d5205 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

NET (empty diffs)

✅ API diff vs stable

NET (empty diffs)

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 0feba87bcd06d48b9d0132426e08393c282d5205 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ [CI Build #0feba87] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 0feba87bcd06d48b9d0132426e08393c282d5205 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #0feba87] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: 0feba87bcd06d48b9d0132426e08393c282d5205 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #0feba87] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: 0feba87bcd06d48b9d0132426e08393c282d5205 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #0feba87] Tests on macOS arm64 - Mac Sequoia (15) passed 💻

All tests on macOS arm64 - Mac Sequoia (15) passed.

Pipeline on Agent
Hash: 0feba87bcd06d48b9d0132426e08393c282d5205 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build #0feba87] Tests on macOS arm64 - Mac Tahoe (26) passed 💻

All tests on macOS arm64 - Mac Tahoe (26) passed.

Pipeline on Agent
Hash: 0feba87bcd06d48b9d0132426e08393c282d5205 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🔥 [CI Build #0feba87] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 6 tests failed, 122 tests passed.

Failures

❌ dotnettests tests (iOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsDynamicRegistra...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsManagedStaticRe...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsStaticRegistrar...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...

Html Report (VSDrops) Download

❌ dotnettests tests (MacCatalyst)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsDynamicRegistra...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsManagedStaticRe...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsStaticRegistrar...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...

Html Report (VSDrops) Download

❌ dotnettests tests (macOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsDynamicRegistra...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsManagedStaticRe...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsStaticRegistrar...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...

Html Report (VSDrops) Download

❌ dotnettests tests (tvOS)

1 tests failed, 0 tests passed.

Failed tests

  • DotNet tests: Failed (Execution failed with exit code 1)
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsDynamicRegistra...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsManagedStaticRe...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...
    • Xamarin.Tests.TrimmerWarningsTest.TrimmerWarningsStaticRegistrar...: Unexpected warning: ILLink: System.StartupHookProvider.ProcessStartupHooks(String): Using member 'System.StartupHookProvider.Cal...

Html Report (VSDrops) Download

❌ linker tests

1 tests failed, 43 tests passed.

Failed tests

  • link sdk/iOS - simulator/Debug: Crashed

Html Report (VSDrops) Download

❌ monotouch tests (MacCatalyst)

1 tests failed, 14 tests passed.

Failed tests

  • monotouch-test/Mac Catalyst/Release (NativeAOT): HarnessException (Harness exception for 'monotouch-test': System.Exception: Unable to evaluate the property OutputPath in /Users/builder/azdo/_work/3/s/macios/tests/xharness/bin/Debug/tmp-test-dir/monotouch-test576/monotouch-test.csproj, build failed with exit code 139. Timed out: False
    at Xharness.AppBundleLocator.GetPropertyByMSBuildEvaluationAsync(XmlDocument csproj, String projectPath, String evaluateProperty, String dependsOnTargets, Dictionary`2 properties) in /Users/builder/azdo/_work/3/s/macios/tests/xharness/AppBundleLocator.cs:line 121
    at Xharness.AppBundleLocator.LocateAppBundle(XmlDocument projectFile, String projectFilePath, TestTarget target, String buildConfiguration) in /Users/builder/azdo/_work/3/s/macios/tests/xharness/AppBundleLocator.cs:line 48
    at Xharness.Jenkins.TestTasks.MacExecuteTask.RunTestAsync() in /Users/builder/azdo/_work/3/s/macios/tests/xharness/Jenkins/TestTasks/MacExecuteTask.cs:line 52
    at Xharness.Jenkins.TestTasks.RunTest.ExecuteAsync() in /Users/builder/azdo/_work/3/s/macios/tests/xharness/Jenkins/TestTasks/RunTest.cs:line 113
    at Xharness.Jenkins.TestTasks.TestTasks.RunInternalAsync() in /Users/builder/azdo/_work/3/s/macios/tests/xharness/Jenkins/TestTasks/TestTask.cs:line 233)

Html Report (VSDrops) Download

Successes

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 12 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 11 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 0feba87bcd06d48b9d0132426e08393c282d5205 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

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

Successfully merging this pull request may close these issues.

Fix support for StartupHooks

4 participants