@@ -30,6 +30,9 @@ internal sealed class BuildHostProcessManager : IAsyncDisposable
30
30
private readonly SemaphoreSlim _gate = new ( initialCount : 1 ) ;
31
31
private readonly Dictionary < BuildHostProcessKind , BuildHostProcess > _processes = [ ] ;
32
32
33
+ private static string MSBuildWorkspaceDirectory => Path . GetDirectoryName ( typeof ( BuildHostProcessManager ) . Assembly . Location ) ! ;
34
+ private static bool IsLoadedFromNuGetPackage => File . Exists ( Path . Combine ( MSBuildWorkspaceDirectory , ".." , ".." , "microsoft.codeanalysis.workspaces.msbuild.nuspec" ) ) ;
35
+
33
36
public BuildHostProcessManager ( ImmutableDictionary < string , string > ? globalMSBuildProperties = null , IBinLogPathProvider ? binaryLogPathProvider = null , ILoggerFactory ? loggerFactory = null )
34
37
{
35
38
_globalMSBuildProperties = globalMSBuildProperties ?? ImmutableDictionary < string , string > . Empty ;
@@ -186,10 +189,7 @@ private ProcessStartInfo CreateDotNetCoreBuildHostStartInfo(string pipeName)
186
189
187
190
internal static string GetNetCoreBuildHostPath ( )
188
191
{
189
- // The .NET Core build host is deployed as a content folder next to the application into the BuildHost-netcore path
190
- var buildHostPath = Path . Combine ( Path . GetDirectoryName ( typeof ( BuildHostProcessManager ) . Assembly . Location ) ! , "BuildHost-netcore" , "Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.dll" ) ;
191
- AssertBuildHostExists ( buildHostPath ) ;
192
- return buildHostPath ;
192
+ return GetBuildHostPath ( "BuildHost-netcore" , "Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.dll" ) ;
193
193
}
194
194
195
195
private ProcessStartInfo CreateDotNetFrameworkBuildHostStartInfo ( string pipeName )
@@ -221,16 +221,34 @@ private ProcessStartInfo CreateMonoBuildHostStartInfo(string pipeName)
221
221
222
222
private static string GetDotNetFrameworkBuildHostPath ( )
223
223
{
224
- // The .NET Framework build host is deployed as a content folder next to the application into the BuildHost-net472 path
225
- var netFrameworkBuildHost = Path . Combine ( Path . GetDirectoryName ( typeof ( BuildHostProcessManager ) . Assembly . Location ) ! , "BuildHost-net472" , "Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.exe" ) ;
226
- AssertBuildHostExists ( netFrameworkBuildHost ) ;
227
- return netFrameworkBuildHost ;
224
+ return GetBuildHostPath ( "BuildHost-net472" , "Microsoft.CodeAnalysis.Workspaces.MSBuild.BuildHost.exe" ) ;
228
225
}
229
226
230
- private static void AssertBuildHostExists ( string buildHostPath )
227
+ private static string GetBuildHostPath ( string contentFolderName , string assemblyName )
231
228
{
229
+ // Possible BuildHost paths are relative to where the Workspaces.MSBuild assembly was loaded.
230
+ string buildHostPath ;
231
+
232
+ if ( IsLoadedFromNuGetPackage )
233
+ {
234
+ // When Workspaces.MSBuild is loaded from the NuGet package (as is the case in .NET Interactive, NCrunch, and possibly other use cases)
235
+ // the Build host is deployed under the contentFiles folder.
236
+ //
237
+ // Workspaces.MSBuild.dll Path - .nuget/packages/microsoft.codeanalysis.workspaces.msbuild/{version}/lib/{tfm}/Microsoft.CodeAnalysis.Workspaces.MSBuild.dll
238
+ // MSBuild.BuildHost.dll Path - .nuget/packages/microsoft.codeanalysis.workspaces.msbuild/{version}/contentFiles/any/any/{contentFolderName}/{assemblyName}
239
+
240
+ buildHostPath = Path . GetFullPath ( Path . Combine ( MSBuildWorkspaceDirectory , ".." , ".." , "contentFiles" , "any" , "any" , contentFolderName , assemblyName ) ) ;
241
+ }
242
+ else
243
+ {
244
+ // When Workspaces.MSBuild is deployed as part of an application the build host is deployed as a content folder next to the application.
245
+ buildHostPath = Path . Combine ( MSBuildWorkspaceDirectory , contentFolderName , assemblyName ) ;
246
+ }
247
+
232
248
if ( ! File . Exists ( buildHostPath ) )
233
249
throw new Exception ( string . Format ( WorkspaceMSBuildResources . The_build_host_could_not_be_found_at_0 , buildHostPath ) ) ;
250
+
251
+ return buildHostPath ;
234
252
}
235
253
236
254
private void AppendBuildHostCommandLineArgumentsConfigureProcess ( ProcessStartInfo processStartInfo , string pipeName )
0 commit comments