Skip to content

[msbuild/bgen] Run bgen in the MSBuild task process. Fixes #26584 - #26590

Open
rolfbjarne wants to merge 7 commits into
mainfrom
dev/rolf/in-process-bgen
Open

rolfbjarne wants to merge 7 commits into
mainfrom
dev/rolf/in-process-bgen

Conversation

@rolfbjarne

Copy link
Copy Markdown
Member

Compile bgen into Xamarin.MacDev.Tasks and invoke it directly for local builds, avoiding a separate bgen process by default.

Keep the external-process implementation available through the opt-in _BGenUseExternalProcess property, while preserving remote execution.

Add netstandard2.0 compatibility, cancellation support, isolated compiler environment handling, and task coverage for both execution modes.

Fixes #26584

🤖 Pull request created by Copilot

rolfbjarne and others added 7 commits September 9, 2026 20:23
Compile the binding generator into Xamarin.MacDev.Tasks and invoke its entry point directly, avoiding dependency on the bgen runtimeconfig framework version.

Add temporary netstandard2.0 compatibility paths and verify the task no longer uses the configured bgen executable for local execution.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Allow the internal _BGenUseExternalProcess MSBuild property to select the previous out-of-process bgen execution path while keeping in-process execution as the default.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Propagate the BGen task cancellation token into the binding generator and observe it between initialization and generated types.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Pass DOTNET_CUSTOM_HOME to compiler child processes without mutating the MSBuild process environment during in-process generation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Return a structured bgen error instead of terminating the hosting MSBuild process when an Export selector is invalid.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Forward in-process task cancellation to bgen compiler invocations and surface cancellation after terminating the child process.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Include bgen's command-line parser dependency in the netstandard2.0 task assembly so packaged tasks can execute bgen in-process.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings September 9, 2026 18:25

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.

🟡 Changes recommended

The in-process console redirection in BGen can interfere with other concurrently running tasks that also redirect Console.Out/Error, risking misrouted or lost build output.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR integrates bgen into Xamarin.MacDev.Tasks so local builds can run bgen in-process (avoiding a separate dotnet bgen.dll process by default), while keeping an opt-in external-process mode via _BGenUseExternalProcess and preserving remote execution behavior.

Changes:

  • Compile bgen sources into Xamarin.MacDev.Tasks and add an in-process execution path (with cancellation/custom HOME handling).
  • Keep an external-process fallback (UseExternalProcess) and add tests covering both modes.
  • Improve netstandard2.0 compatibility and convert a hard-exit invalid selector failure into a BindingException (BI1129) with docs/resources updates.
File summaries
File Description
tools/common/Driver.execution.cs Adds cancellation-aware RunCommand overloads for process execution.
tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/BGenTaskTest.cs Adds unit tests for in-process vs external-process execution modes.
src/Resources.resx Adds BI1129 resource string.
src/Resources.Designer.cs Adds BI1129 strongly-typed accessor.
src/ObjCRuntime/Stret.cs Adds netstandard-compatible null checking helper.
src/build/dotnet/generator-frameworks.g.cs Renames generated framework container type to BGenFrameworks.
src/bgen/TypeManager.cs Updates framework type reference to BGenFrameworks.
src/bgen/NullabilityInfoContext.cs Guards NullabilityInfo helpers behind #if NET for netstandard builds.
src/bgen/NetStandardCompatibility.cs Adds polyfills for newer attributes/types when not building with NET.
src/bgen/Models/MarshalTypeList.cs Updates framework type reference to BGenFrameworks.
src/bgen/Generator.cs Throws BI1129 instead of printing+exiting; adds cancellation checks in loops.
src/bgen/Frameworks.cs Renames Frameworks to BGenFrameworks.
src/bgen/Extensions/StringExtensions.cs Adds warning suppression for netstandard compilation.
src/bgen/Enums.cs Uses reflection overloads compatible with non-NET builds.
src/bgen/DocumentationManager.cs Adds netstandard-compatible string slicing and SZ-array detection.
src/bgen/Caches/TypeCache.cs Updates framework type reference to BGenFrameworks.
src/bgen/Caches/NamespaceCache.cs Updates framework type reference to BGenFrameworks.
src/bgen/BindingTouch.cs Adds Run(...) entrypoint, cancellation propagation, custom HOME support for compiler runs.
src/bgen/AttributeManager.cs Adds netstandard-compatible substring parsing.
scripts/generate-frameworks/README.md Updates example output to BGenFrameworks.
scripts/generate-frameworks/generate-frameworks.cs Emits partial class BGenFrameworks in generated output.
msbuild/Xamarin.Shared/Xamarin.Shared.targets Wires _BGenUseExternalProcess into the BGen task.
msbuild/Xamarin.MacDev.Tasks/Xamarin.MacDev.Tasks.csproj Compiles bgen sources into tasks assembly; adds Mono.Options and BI resources generation.
msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinTask.cs Fully qualifies ErrorHelper.WarningLevel to avoid ambiguity.
msbuild/Xamarin.MacDev.Tasks/Tasks/BGen.cs Implements in-process bgen execution path and external-process switch.
msbuild/ILMerge.targets Adds Mono.Options to merged assemblies list.
docs/website/generator-errors.md Documents BI1129.
Review details

Files not reviewed (1)

  • src/Resources.Designer.cs: Generated file
  • Files reviewed: 26/27 changed files
  • Comments generated: 2
  • Review effort level: Lite

Comment on lines +389 to +397
counter--;
if (counter == 0) {
if (originalStdout is null || originalStderr is null)
throw new InvalidOperationException ("The original console writers were not captured.");
Console.SetOut (originalStdout);
Console.SetError (originalStderr);
originalStdout = null;
originalStderr = null;
}
Comment thread src/bgen/Generator.cs
Comment on lines +1255 to +1256
if (export.Selector.IndexOfAny (invalid_selector_chars) != -1)
throw new BindingException (1129, true, export.Selector);
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
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: 888a0169ab6ae9946b52052309a08c4700e7f9fa [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #888a016] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 4 tests failed, 260 tests passed.

Failures

❌ monotouch tests (MacCatalyst)

1 tests failed, 24 tests passed.

Failed tests

  • monotouch-test/Mac Catalyst/Debug (CoreCLR): Failed (Test run failed.
    Tests run: 3795 Passed: 3630 Inconclusive: 19 Failed: 1 Ignored: 164)

Html Report (VSDrops) Download

❌ monotouch tests (macOS)

2 tests failed, 18 tests passed.

Failed tests

  • monotouch-test/macOS/Release (NativeAOT): TimedOut (Execution timed out after 1200 seconds.
    Test run crashed)
  • monotouch-test/macOS/Release (NativeAOT, .NET 11 defaults): TimedOut (Execution timed out after 1200 seconds.
    Test run crashed)

Html Report (VSDrops) Download

❌ windows tests

1 tests failed, 2 tests passed.

Failed tests

  • Remote .NET tests/Xamarin.Tests.PackTest.BindingXcFrameworksProjectOnRemoteWindowsUsingFallback(iOS,True,True,True): Failed: 'dotnet pack' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.PackTest.BindingXcFrameworksProjectOnRemoteWindowsUsingFallback(iOS,True,True,False): Failed: 'dotnet pack' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.PackTest.BindingXcFrameworksProjectOnRemoteWindowsUsingFallback(iOS,True,False,True): Failed: 'dotnet pack' failed with exit code 1

  • Remote .NET tests/Xamarin.Tests.PackTest.BindingXcFrameworksProjectOnRemoteWindowsUsingFallback(iOS,False,False,False): Failed: 'dotnet pack' failed with exit code 1

Html Report (VSDrops) Download

Successes

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): 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 7 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 31 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 31 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 31 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 25 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 25 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Tahoe (26): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: 888a0169ab6ae9946b52052309a08c4700e7f9fa [PR build]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make bgen resilient to .NET versions

3 participants