Skip to content

Commit 75fb0b8

Browse files
authored
Allow the build host to be copied from M.CA.Workspaces.MSBuild (#36708)
Add more diagnostic information on MSBuild workspace failure Remove MSBuildLocator usage as it's no longer needed Fixes #35638
1 parent 33ae58b commit 75fb0b8

File tree

6 files changed

+62
-16
lines changed

6 files changed

+62
-16
lines changed

src/EFCore.Design/Design/Internal/DbContextOperations.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Text;
5-
using Microsoft.Build.Locator;
65
using Microsoft.CodeAnalysis;
76
using Microsoft.CodeAnalysis.Editing;
87
using Microsoft.CodeAnalysis.MSBuild;
@@ -298,23 +297,41 @@ private IReadOnlyList<string> PrecompileQueries(
298297
{
299298
outputDir = Path.GetFullPath(Path.Combine(_projectDir, outputDir ?? "Generated"));
300299

301-
if (!MSBuildLocator.IsRegistered)
300+
// TODO: pass through properties
301+
MSBuildWorkspace workspace = null!;
302+
Project project;
303+
304+
try
302305
{
303-
MSBuildLocator.RegisterDefaults();
306+
workspace = MSBuildWorkspace.Create();
307+
workspace.LoadMetadataForReferencedProjects = true;
308+
workspace.RegisterWorkspaceFailedHandler(e =>
309+
{
310+
_reporter.WriteError(DesignStrings.MSBuildWorkspaceFailure(e.Diagnostic.Kind, e.Diagnostic.Message));
311+
});
312+
project = workspace.OpenProjectAsync(_project).GetAwaiter().GetResult();
313+
}
314+
catch (Exception ex)
315+
{
316+
if (workspace != null && !workspace.Diagnostics.IsEmpty)
317+
{
318+
var diagnosticMessages = Environment.NewLine + string.Join(Environment.NewLine,
319+
workspace.Diagnostics.Select(d => $" {d.Kind}: {d.Message}"));
320+
_reporter.WriteVerbose(DesignStrings.MSBuildWorkspaceDiagnostics(diagnosticMessages));
321+
}
322+
323+
throw new InvalidOperationException(
324+
DesignStrings.QueryPrecompilationProjectLoadFailed(_project, ex.Message), ex);
304325
}
305326

306-
// TODO: pass through properties
307-
var workspace = MSBuildWorkspace.Create();
308-
workspace.LoadMetadataForReferencedProjects = true;
309-
var project = workspace.OpenProjectAsync(_project).GetAwaiter().GetResult();
310327
if (!project.SupportsCompilation)
311328
{
312329
throw new NotSupportedException(DesignStrings.UncompilableProject(_project));
313330
}
314331

315332
var compilation = project.GetCompilationAsync().GetAwaiter().GetResult()!;
316333
var errorDiagnostics = compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error).ToArray();
317-
if (errorDiagnostics.Any())
334+
if (errorDiagnostics.Length != 0)
318335
{
319336
var errorBuilder = new StringBuilder();
320337
errorBuilder.AppendLine(DesignStrings.CompilationErrors);

src/EFCore.Design/EFCore.Design.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@
5858
<ItemGroup>
5959
<PackageReference Include="Humanizer.Core" />
6060
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
61-
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" />
62-
<PackageReference Include="Microsoft.Build.Locator" />
61+
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" PrivateAssets="analyzers;build" />
6362
<PackageReference Include="Microsoft.Extensions.DependencyModel" />
6463
<PackageReference Include="Microsoft.Extensions.HostFactoryResolver.Sources" PrivateAssets="All" />
6564
<PackageReference Include="Mono.TextTemplating" />

src/EFCore.Design/Properties/DesignStrings.Designer.cs

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/EFCore.Design/Properties/DesignStrings.resx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,12 @@ Consider changing your target project to the DbContext project by using the Pack
288288
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b =&gt; b.MigrationsAssembly("{assembly}")). By default, the migrations assembly is the assembly containing the DbContext.
289289
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.</value>
290290
</data>
291+
<data name="MSBuildWorkspaceDiagnostics" xml:space="preserve">
292+
<value>MSBuild Workspace diagnostics:{diagnostics}</value>
293+
</data>
294+
<data name="MSBuildWorkspaceFailure" xml:space="preserve">
295+
<value>MSBuild Workspace failure: {kind} - {message}</value>
296+
</data>
291297
<data name="MultipleAnnotationConflict" xml:space="preserve">
292298
<value>The annotation '{annotationName}' was specified twice with potentially different values. Specifying the same annotation multiple times for different providers is no longer supported. Review the generated Migration to ensure it is correct and, if necessary, edit the Migration to fix any issues.</value>
293299
</data>
@@ -384,6 +390,9 @@ Change your target project to the migrations project by using the Package Manage
384390
<data name="QueryPrecompilationErrors" xml:space="preserve">
385391
<value>Query precompilation failed with errors:</value>
386392
</data>
393+
<data name="QueryPrecompilationProjectLoadFailed" xml:space="preserve">
394+
<value>Failed to load project for query precompilation. Project: {project}. Error: {error}</value>
395+
</data>
387396
<data name="ReadOnlyFiles" xml:space="preserve">
388397
<value>No files were generated in directory '{outputDirectoryName}'. The following file(s) already exist(s) and must be made writeable to continue: {readOnlyFiles}.</value>
389398
</data>

src/EFCore.Tasks/buildTransitive/Microsoft.EntityFrameworkCore.Tasks.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
<Project ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
44
<PropertyGroup>
5-
<_TaskTargetFramework Condition="'$(MSBuildRuntimeType)' == 'core'">net10.0</_TaskTargetFramework>
6-
<_TaskTargetFramework Condition="'$(MSBuildRuntimeType)' != 'core'">net472</_TaskTargetFramework>
5+
<_TaskTargetFramework Condition="'$(MSBuildRuntimeType)' == 'Core'">net10.0</_TaskTargetFramework>
6+
<_TaskTargetFramework Condition="'$(MSBuildRuntimeType)' != 'Core'">net472</_TaskTargetFramework>
77
<_EFCustomTasksAssembly>$([MSBuild]::NormalizePath($(MSBuildThisFileDirectory),'..\tasks\$(_TaskTargetFramework)\$(MSBuildThisFileName).dll'))</_EFCustomTasksAssembly>
88
<EFScaffoldModelStage Condition="'$(EFScaffoldModelStage)'==''">publish</EFScaffoldModelStage>
99
<EFPrecompileQueriesStage Condition="'$(EFPrecompileQueriesStage)'==''">publish</EFPrecompileQueriesStage>

src/EFCore.Tasks/buildTransitive/Microsoft.EntityFrameworkCore.Tasks.targets

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55
<PropertyGroup>
66
<_FullOutputPath>$([MSBuild]::NormalizePath('$(MSBuildProjectDirectory)', '$(OutputPath)'))</_FullOutputPath>
77
<_FullIntermediateOutputPath>$([MSBuild]::NormalizePath('$(MSBuildProjectDirectory)', '$(IntermediateOutputPath)'))</_FullIntermediateOutputPath>
8-
<_FullIntermediateOutputPath Condition="'$(AppendRuntimeIdentifierToOutputPath)' == 'true' And '$(RuntimeIdentifier)' != '' And '$(_UsingDefaultRuntimeIdentifier)' != 'true' And '$(UseArtifactsIntermediateOutput)' != 'true'">
9-
$([MSBuild]::NormalizePath('$(_FullIntermediateOutputPath)',
10-
'../'))
11-
</_FullIntermediateOutputPath>
8+
<_FullIntermediateOutputPath Condition="'$(AppendRuntimeIdentifierToOutputPath)' == 'true' And '$(RuntimeIdentifier)' != '' And '$(_UsingDefaultRuntimeIdentifier)' != 'true' And '$(UseArtifactsIntermediateOutput)' != 'true'">$([MSBuild]::NormalizePath('$(_FullIntermediateOutputPath)', '../'))</_FullIntermediateOutputPath>
129
<EFGeneratedSourcesBuildFile Condition="'$(EFGeneratedSourcesBuildFile)' == ''">$(_FullIntermediateOutputPath)$(AssemblyName).EFGeneratedSources.Build.txt</EFGeneratedSourcesBuildFile>
1310
<EFGeneratedSourcesPublishFile Condition="'$(EFGeneratedSourcesPublishFile)' == ''">$(_FullIntermediateOutputPath)$(AssemblyName).EFGeneratedSources.Publish.txt</EFGeneratedSourcesPublishFile>
1411
<_AssemblyFullName>$(_FullOutputPath)$(AssemblyName).dll</_AssemblyFullName>

0 commit comments

Comments
 (0)