runtime: Dotnet build hangs while building libs.tests natively using cross built dotnet sdk

Description

Dotnet build hangs while building libs.tests natively using cross built dotnet sdk.

Brief Analysis and probable root cause:

  1. The hang occurs while building project using msbuild runtime/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.FirstCallAfterUpdate/System.Reflection.Metadata.ApplyUpdate.Test.FirstCallAfterUpdate.csproj
  2. Mono trace log for “Microsoft.DotNet.HotReload.Utils.Generator” shows there is FileNotFoundException.
  3. Traced the filename using strace command and found that the missing file is “/root/.nuget/packages/microsoft.dotnet.hotreload.utils.generator.buildtool/9.0.0-alpha.0.24168.2/tools/net9.0/BuildHost-netcore/Microsoft.Build.dll”.
  4. If we check the directory contents for /root/.nuget/packages/microsoft.dotnet.hotreload.utils.generator.buildtool/9.0.0-alpha.0.24168.2/tools/net9.0/BuildHost-netcore/ then there is a file with name “Microsoft.Build.Locator.dll” but not “Microsoft.Build.dll”.

Complete mono trace log for Microsoft.DotNet.HotReload.Utils.Generator namespace:

[root@devfirst-dotnet-rhel8-8-1 logs]# export MONO_ENV_OPTIONS="--trace=N:Microsoft.DotNet.HotReload.Utils.Generator"
[root@devfirst-dotnet-rhel8-8-1 logs]# /root/alhad/runtime/dotnet-sdk-ppc64le/.dotnet/dotnet /root/.nuget/packages/microsoft.dotnet.hotreload.utils.generator.buildtool/9.0.0-alpha.0.24168.2/build/../tools/net9.0/Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll -msbuild:/root/alhad/runtime/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.FirstCallAfterUpdate/System.Reflection.Metadata.ApplyUpdate.Test.FirstCallAfterUpdate.csproj -script:/root/alhad/runtime/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.FirstCallAfterUpdate/deltascript.json -p:Configuration=Debug
[0x7fff8a9a5160: 0.00000 0] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Config:Builder ()()
[0x7fff8a9a5160: 0.00019 0] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Config:Builder ()([.ConfigBuilder:0x7fff7c402bc8]
[0x7fff8a9a5160: 0.04096 0] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig:.ctor (Microsoft.DotNet.HotReload.Utils.Generator.Config/ConfigBuilder)(this:0x7fff7c405820[Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll], [.ConfigBuilder:0x7fff7c402bc8])
[0x7fff8a9a5160: 0.04116 1] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Config:.ctor (Microsoft.DotNet.HotReload.Utils.Generator.Config/ConfigBuilder)(this:0x7fff7c405820[Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll], [.ConfigBuilder:0x7fff7c402bc8])
[0x7fff8a9a5160: 0.04145 1] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Config:.ctor (Microsoft.DotNet.HotReload.Utils.Generator.Config/ConfigBuilder)(
[0x7fff8a9a5160: 0.04146 0] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig:.ctor (Microsoft.DotNet.HotReload.Utils.Generator.Config/ConfigBuilder)(
[0x7fff8a9a5160: 0.04437 0] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:Make (Microsoft.DotNet.HotReload.Utils.Generator.Config)([Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig:0x7fff7c405820])
[0x7fff8a9a5160: 0.04444 1] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Config:get_Live ()(this:0x7fff7c405820[Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll])
[0x7fff8a9a5160: 0.04445 1] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Config:get_Live ()(result=0
[0x7fff8a9a5160: 0.04500 0] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:Make (Microsoft.DotNet.HotReload.Utils.Generator.Config)([Microsoft.DotNet.HotReload.Utils.Generator.Runners.ScriptRunner:0x7fff7c405e60]
[0x7fff8a9a5160: 0.04516 0] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:Run (System.Threading.CancellationToken)(this:0x7fff7c405e60[Microsoft.DotNet.HotReload.Utils.Generator.Runners.ScriptRunner Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll], [00,00,00,00,00,00,00,00,])
[0x7fff8a9a5160: 0.23871 0] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:Run (System.Threading.CancellationToken)([.AsyncStateMachineBox`1:0x7fff7c424560]
[0x7fff738cf140: 0.26416 0] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:PrepareCapabilities ()(this:0x7fff7c405e60[Microsoft.DotNet.HotReload.Utils.Generator.Runners.ScriptRunner Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll])
[0x7fff738cf140: 0.26422 1] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Config:get_EditAndContinueCapabilities ()(this:0x7fff7c405820[Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll])
[0x7fff738cf140: 0.26423 1] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Config:get_EditAndContinueCapabilities ()([System.String[]:0x7fff7c4025b8]
[0x7fff738cf140: 0.26446 1] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser:.cctor ()()
[0x7fff738cf140: 0.28896 1] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser:.cctor ()(
[0x7fff738cf140: 0.28897 1] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser:Parse (System.Collections.Generic.IEnumerable`1<string>)(XX)
[0x7fff738cf140: 0.28912 2] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser:Tokenize (System.Collections.Generic.IEnumerable`1<string>)(XX)
[0x7fff738cf140: 0.28918 2] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser:Tokenize (System.Collections.Generic.IEnumerable`1<string>)((unknown return type 15)
[0x7fff738cf140: 0.28940 2] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser:Parse (System.Collections.Generic.IEnumerable`1<Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser/Token>)(XX)
[0x7fff738cf140: 0.29011 2] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser:Parse (System.Collections.Generic.IEnumerable`1<Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser/Token>)((unknown return type 15)
[0x7fff738cf140: 0.29012 1] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.EditAndContinueCapabilitiesParser:Parse (System.Collections.Generic.IEnumerable`1<string>)((unknown return type 15)
[0x7fff738cf140: 0.29157 1] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:DefaultCapabilities ()(this:0x7fff7c405e60[Microsoft.DotNet.HotReload.Utils.Generator.Runners.ScriptRunner Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll])
[0x7fff738cf140: 0.29158 1] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:DefaultCapabilities ()(result=1023
[0x7fff738cf140: 0.29162 1] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Config:get_NoWarnUnknownCapabilities ()(this:0x7fff7c405820[Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll])
[0x7fff738cf140: 0.29163 1] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Config:get_NoWarnUnknownCapabilities ()(result=0
[0x7fff738cf140: 0.29180 0] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:PrepareCapabilities ()(result=1023
[0x7fff738cf140: 0.29196 0] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:SetupBaseline (Microsoft.DotNet.HotReload.Utils.Generator.EnC.EditAndContinueCapabilities,System.Threading.CancellationToken)(this:0x7fff7c405e60[Microsoft.DotNet.HotReload.Utils.Generator.Runners.ScriptRunner Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll], 1023, [00,00,00,00,00,00,00,00,])
[0x7fff738cf140: 0.29293 1] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:InitMSBuild ()()
[0x7fff738cf140: 1.12321 1] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:InitMSBuild ()(
[0x7fff738cf140: 1.12342 1] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.BaselineProject:Make (Microsoft.DotNet.HotReload.Utils.Generator.Config,Microsoft.DotNet.HotReload.Utils.Generator.EnC.EditAndContinueCapabilities,System.Threading.CancellationToken)([Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig:0x7fff7c405820], 1023, [00,00,00,00,00,00,00,00,])
[0x7fff738cf140: 1.12455 2] ENTER:c Microsoft.DotNet.HotReload.Utils.Generator.BaselineProject:PrepareMSBuildProject (Microsoft.DotNet.HotReload.Utils.Generator.Config,Microsoft.DotNet.HotReload.Utils.Generator.EnC.EditAndContinueCapabilities,System.Threading.CancellationToken)([Microsoft.DotNet.HotReload.Utils.Generator.MsbuildConfig:0x7fff7c405820], 1023, [00,00,00,00,00,00,00,00,])
[0x7fff738cf140:] EXCEPTION handling: System.IO.FileNotFoundException:
[0x7fff738cf140: 2.18797 2] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.BaselineProject:PrepareMSBuildProject (Microsoft.DotNet.HotReload.Utils.Generator.Config,Microsoft.DotNet.HotReload.Utils.Generator.EnC.EditAndContinueCapabilities,System.Threading.CancellationToken)((unknown return type 15)
[0x7fff738cf140: 2.18885 1] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.BaselineProject:Make (Microsoft.DotNet.HotReload.Utils.Generator.Config,Microsoft.DotNet.HotReload.Utils.Generator.EnC.EditAndContinueCapabilities,System.Threading.CancellationToken)((unknown return type 15)
[0x7fff738cf140: 2.18978 0] LEAVE:c Microsoft.DotNet.HotReload.Utils.Generator.Runner:SetupBaseline (Microsoft.DotNet.HotReload.Utils.Generator.EnC.EditAndContinueCapabilities,System.Threading.CancellationToken)((unknown return type 15)
[0x7fff738cf140:] EXCEPTION handling: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: C. Path '', line 0, position 0.
[0x7fff738cf140:] EXCEPTION handling: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: C. Path '', line 0, position 0.

Reproduction Steps

  1. Cross build dotnet sdk on x86_64
  2. Native build runtime and libs.tests on any machine ppc64le/s390x or x86_64

Expected behavior

The dotnet native build should not hang

Actual behavior

The dotnet native build hangs.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 31 (30 by maintainers)

Commits related to this issue

Most upvoted comments

We’ve done a bit more investigation on where this error is hit. We’re running

dotnet
/root/.nuget/packages/microsoft.dotnet.hotreload.utils.generator.buildtool/9.0.0-alpha.0.24168.2/build/../tools/net9.0/Microsoft.DotNet.HotReload.Utils.Generator.BuildTool.dll
-msbuild:/root/alhad/runtime/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.FirstCallAfterUpdate/System.Reflection.Metadata.ApplyUpdate.Test.FirstCallAfterUpdate.csproj
-script:/root/alhad/runtime/src/libraries/System.Runtime.Loader/tests/ApplyUpdate/System.Reflection.Metadata.ApplyUpdate.Test.FirstCallAfterUpdate/deltascript.json
-p:Configuration=Debug

which ends up calling the PrepareMSBuildProject routine here https://github.com/dotnet/hotreload-utils/blob/c667b519c4bcdf121106fac021a53d98120e825a/src/Microsoft.DotNet.HotReload.Utils.Generator/BaselineProject.cs#L23 which calls msw.OpenProjectAsync.

This creates a new “BuildHost” process running

dotnet --roll-forward LatestMajor
/root/.nuget/packages/microsoft.dotnet.hotreload.utils.generator.buildtool/9.0.0-alpha.0.24168.2/tools/net9.0/BuildHost-netcore/Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.dll
--property Configuration=Debug

and uses the Microsoft.CodeAnalysis.MSBuild.Rpc.RpcServer:ProcessRequestAsync method to invoke the Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.BuildHost:LoadProjectFileAsync routine there, which tries to create an instance of Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader:CreateProjectFile here: https://github.com/dotnet/roslyn/blob/fa7ece1ca25ef529db616ed6c839af9f027fa216/src/Workspaces/Core/MSBuild.BuildHost/BuildHost.cs#L180

This ends up in the Mono JIT attempting to load and initialize that type, which fails with:

Could not load signature of Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader:CreateProjectFile due to:
Could not load file or assembly 'Microsoft.Build, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

causing

An exception of type System.TypeLoadException was thrown:
VTable setup of type Microsoft.CodeAnalysis.CSharp.CSharpProjectFileLoader failed

The Mono JIT previously had attempted to search for Microsoft.Build unsuccessfully in these paths:

/root/.nuget/packages/microsoft.dotnet.hotreload.utils.generator.buildtool/9.0.0-alpha.0.24168.2/tools/net9.0/BuildHost-netcore/Microsoft.Build.dll
/root/.nuget/packages/microsoft.dotnet.hotreload.utils.generator.buildtool/9.0.0-alpha.0.24168.2/tools/net9.0/BuildHost-netcore/Microsoft.Build.exe
/root/.nuget/packages/microsoft.dotnet.hotreload.utils.generator.buildtool/9.0.0-alpha.0.24168.2/tools/net9.0/BuildHost-netcore/Microsoft.Build/Microsoft.Build.dll
/root/.nuget/packages/microsoft.dotnet.hotreload.utils.generator.buildtool/9.0.0-alpha.0.24168.2/tools/net9.0/BuildHost-netcore/Microsoft.Build/Microsoft.Build.exe

Now, there’s still a number of questions:

First of all, why is Microsoft.Build.dll not packaged with the microsoft.dotnet.hotreload.utils.generator.buildtool nupkg, even though a number of other Microsoft.Build assemblies are? If I’m understanding this correctly, that could be an effect of the ExcludeAssets="runtime" attribute here: https://github.com/dotnet/hotreload-utils/blob/c667b519c4bcdf121106fac021a53d98120e825a/src/Microsoft.DotNet.HotReload.Utils.Generator/Microsoft.DotNet.HotReload.Utils.Generator.csproj#L12 I’m not sure why this was added?

Second, if Microsoft.Build.dll is not found there, should it be taken from the host .NET SDK? The file is available there, but the Mono JIT doesn’t even appear to look there. Not sure if this is as intended, or if there are still some Mono loader search order issues.

Finally, why does this error only show up in Mono? Either it is because of the above-mentioned search order, or else it might be another instance of the problem where Mono is more eager in requiring to load types during JIT that may not actually be referenced later (where CoreCLR would defer the type loading to a later stage, which might not ever happen in this case).

Any suggestion on how to proceed here would be appreciated!

CC @giritrivedi @directhex @akoeplinger @lambdageek

Are there any further manual actions necessary, or will this get updated automatically soon?

The dotnet-maestro bot will make the PRs. There should be no more manual interventions (other than waiting for green CI):

We have just updated the hotreload-utils subscriptions:

https://github.com/dotnet/roslyn (VS 17.12) ==> 'https://github.com/dotnet/hotreload-utils' ('main')
  - Id: 382b0e78-e254-45c6-24d7-08d9158524d7
  - Update Frequency: EveryWeek
  - Enabled: True
  - Batchable: True
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies:
    Standard
  - Last Build: 20240906.1 (1ce264cab372119376a734a247e9f60fe898877a)
https://github.com/dotnet/roslyn (VS 17.3) ==> 'https://github.com/dotnet/hotreload-utils' ('release/6.0')
  - Id: 502362e8-82de-4374-ae45-08db360867bf
  - Update Frequency: EveryWeek
  - Enabled: True
  - Batchable: True
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies: []
  - Last Build: 20221026.13 (41a5af9d2c459a06c0795bf21a1c046200f375bf)
https://github.com/dotnet/roslyn (VS 17.8) ==> 'https://github.com/dotnet/hotreload-utils' ('release/8.0')
  - Id: cbf2cd55-5246-40b3-c9a2-08dbcf1dbc26
  - Update Frequency: EveryWeek
  - Enabled: True
  - Batchable: False
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies:
    Standard
  - Last Build: 20240823.6 (d8282e36f9e389ee9eaad96b5aba66b83b9a37fd)
https://github.com/dotnet/roslyn (VS 17.12) ==> 'https://github.com/dotnet/hotreload-utils' ('release/9.0')
  - Id: d81ef602-8555-458e-ac21-8113ab0aea0a
  - Update Frequency: EveryWeek
  - Enabled: True
  - Batchable: False
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies:
    Standard
  - Last Build: 20240906.1 (1ce264cab372119376a734a247e9f60fe898877a)

and

https://github.com/dotnet/hotreload-utils (.NET 10) ==> 'https://github.com/dotnet/runtime' ('main')
  - Id: bfe6dacf-8231-4ea1-e2fe-08d962847885
  - Update Frequency: EveryDay
  - Enabled: True
  - Batchable: True
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies:
    AllChecksSuccessful
      ignoreChecks =
                       [
                         "WIP",
                         "license/cla",
                         "Build Analysis",
                         "runtime-coreclr superpmi-diffs",
                         "runtime-coreclr superpmi-replay",
                         "runtime-coreclr jitstress-isas-avx512",
                         "runtime-libraries enterprise-linux",
                         "runtime-wasm-perf"
                       ]
    NoRequestedChanges
  - Last Build: 20240903.1 (22ca60ef848b380d6665318e02694f073a534133)
https://github.com/dotnet/hotreload-utils (.NET 6) ==> 'https://github.com/dotnet/runtime' ('release/6.0')
  - Id: 206e9efc-bf5a-4d71-5108-08db3604d504
  - Update Frequency: None
  - Enabled: True
  - Batchable: False
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies: []
https://github.com/dotnet/hotreload-utils (.NET 6) ==> 'https://github.com/dotnet/runtime' ('release/6.0-staging')
  - Id: dd5d0542-c4f7-4567-5ca9-08db3604d60f
  - Update Frequency: EveryBuild
  - Enabled: True
  - Batchable: False
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies: []
  - Last Build: 20240802.2 (036e630c228a06e61db385ecbe2b96ad995b0f61)
https://github.com/dotnet/hotreload-utils (.NET 8) ==> 'https://github.com/dotnet/runtime' ('release/8.0')
  - Id: 479b3557-3b19-4693-a785-08db9e4038dc
  - Update Frequency: None
  - Enabled: True
  - Batchable: False
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies: []
  - Last Build: 20231023.2 (8e108a21d0c7d8fa2050a1bdd4d4ba50d2b8df13)
https://github.com/dotnet/hotreload-utils (.NET 8) ==> 'https://github.com/dotnet/runtime' ('release/8.0-staging')
  - Id: c0fef358-3848-4a30-a438-08dbcfd61a5a
  - Update Frequency: EveryBuild
  - Enabled: True
  - Batchable: False
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies: []
  - Last Build: 20240903.2 (5339e12def2a3605d069c429840089ae27838728)
https://github.com/dotnet/hotreload-utils (.NET 9) ==> 'https://github.com/dotnet/runtime' ('release/9.0')
  - Id: 6181ce50-ca7a-4061-0a4d-08dcbc302537
  - Update Frequency: EveryDay
  - Enabled: True
  - Batchable: False
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies: []
  - Last Build: 20240903.1 (22ca60ef848b380d6665318e02694f073a534133)
https://github.com/dotnet/hotreload-utils (.NET 9) ==> 'https://github.com/dotnet/runtime' ('release/9.0-staging')
  - Id: b458125c-6cf0-41ea-981d-2709b660d5c5
  - Update Frequency: EveryBuild
  - Enabled: True
  - Batchable: False
  - PR Failure Notification tags:
  - Source-enabled: False
  - Merge Policies: []

Roslyn main should be the VS 17.12 channel now.

First of all, why is Microsoft.Build.dll not packaged with the microsoft.dotnet.hotreload.utils.generator.buildtool nupkg

MSBuild assemblies should basically always come from the .NET SDK, not be repackaged by tools–MSBuild logic tends to make a bunch of assumptions about where .props/targets/etc files are relative to the loaded MSBuild DLLs, and it’s also best to load the exact SDK that would be involved in dotnet build.

Second, if Microsoft.Build.dll is not found there, should it be taken from the host .NET SDK?

So yes. That’s what MSBuildLocator does: it adds some AssemblyLoad event hooks to help find the right files in the SDK.

Finally, why does this error only show up in Mono? Either it is because of the above-mentioned search order, or else it might be another instance of the problem where Mono is more eager in requiring to load types during JIT that may not actually be referenced later (where CoreCLR would defer the type loading to a later stage, which might not ever happen in this case).

The latter behavior sounds very interesting. On .NET Framework and coreclr, the load is triggered by JIT compilation of the first method containing a type from Microsoft.Build.dll. Locator thus requires that you call Register() from a method that is JITed before calling the first such method.

If Mono is more aggressive about trying to load the type (say when the class containing such a method is first seen, rather than the method itself), that might explain the problem