[msbuild] Fix building binding projects with 'dotnet build /t:Compile'#25914
[msbuild] Fix building binding projects with 'dotnet build /t:Compile'#25914rolfbjarne wants to merge 1 commit into
Conversation
'dotnet watch' builds the 'Compile' target directly (it runs
'dotnet build /t:Compile'). For binding projects this failed, because the
binding-project override of CompileDependsOn prepends _GenerateBindings (which
in turn runs _CompileApiDefinitions) before $(CompileDependsOn) - and
$(CompileDependsOn) is where the default ResolveReferences lives.
During a normal build ResolveReferences runs earlier as part of CoreBuild, so
@(ReferencePath) is already populated when the binding targets run. But when the
Compile target is built directly, nothing runs ResolveReferences first, so
_CompileApiDefinitions compiles the API definitions with no references at all
and fails with thousands of errors ("Predefined type 'System.Void' is not
defined", missing System.Runtime, NSObject, ExportAttribute, etc.).
Fix this by adding ResolveReferences to the front of the binding-project
CompileDependsOn override, so references are always resolved before the
binding-generation targets that need them.
Also add a unit test (BuildBindingsTestWithCompileTarget) that builds a binding
project with '/t:Compile'.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes SDK-style binding projects failing when building the Compile target directly (notably dotnet watch, which runs dotnet build /t:Compile). The failure occurred because binding-generation ran before references were resolved, leaving @(ReferencePath) empty during API definition compilation.
Changes:
- Prepend
ResolveReferencesto the binding-projectCompileDependsOnoverride so binding-generation targets have@(ReferencePath)populated even when/t:Compileis invoked directly. - Add a unit test that builds a binding project with
target: "Compile"across Apple platforms to prevent regressions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
msbuild/Xamarin.Shared/Xamarin.Shared.targets |
Ensures ResolveReferences runs before binding-generation when building /t:Compile, fixing missing-reference compilation failures. |
tests/dotnet/UnitTests/ProjectTest.cs |
Adds coverage for building a binding project via /t:Compile (dotnet watch scenario). |
This comment has been minimized.
This comment has been minimized.
✅ [PR Build #78079fa] Build passed (Detect API changes) ✅Pipeline on Agent |
✅ [PR Build #78079fa] Build passed (Build packages) ✅Pipeline on Agent |
✅ API diff for current PR / commitNET (empty diffs)✅ API diff vs stableNET (empty diffs)ℹ️ Generator diffGenerator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes) Pipeline on Agent |
✅ [PR Build #78079fa] Build passed (Build macOS tests) ✅Pipeline on Agent |
🔥 [CI Build #78079fa] Test results 🔥Test results❌ Tests failed on VSTS: test results 1 tests crashed, 1 tests failed, 197 tests passed. Failures❌ dotnettests tests (tvOS)🔥 Failed catastrophically on VSTS: test results - dotnettests_tvos (no summary found). Html Report (VSDrops) Download ❌ monotouch tests (iOS)1 tests failed, 17 tests passed.Failed tests
Html Report (VSDrops) Download Successes✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download macOS tests✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download Linux Build VerificationPipeline on Agent |
'dotnet watch' builds the 'Compile' target directly (it runs 'dotnet build /t:Compile'). For binding projects this failed, because the binding-project override of CompileDependsOn prepends _GenerateBindings (which in turn runs _CompileApiDefinitions) before$(CompileDependsOn) - and $ (CompileDependsOn) is where the default ResolveReferences lives.
During a normal build ResolveReferences runs earlier as part of CoreBuild, so @(ReferencePath) is already populated when the binding targets run. But when the Compile target is built directly, nothing runs ResolveReferences first, so _CompileApiDefinitions compiles the API definitions with no references at all and fails with thousands of errors ("Predefined type 'System.Void' is not defined", missing System.Runtime, NSObject, ExportAttribute, etc.).
Fix this by adding ResolveReferences to the front of the binding-project CompileDependsOn override, so references are always resolved before the binding-generation targets that need them.
Also add a unit test (BuildBindingsTestWithCompileTarget) that builds a binding project with '/t:Compile'.
Contributes towards https://devdiv.visualstudio.com/DevDiv/_workitems/edit/3024114.