Skip to content

Commit a7394d3

Browse files
authored
[release/10.0] Allow tools to run on projects targeting multiple frameworks (#37301)
Fixes #37230
1 parent 199aeeb commit a7394d3

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

src/dotnet-ef/Project.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ public static Project FromFile(
8585
var exitCode = Exe.Run("dotnet", args, handleOutput: line => output.AppendLine(line));
8686
if (exitCode != 0)
8787
{
88+
if (framework == null && HasMultipleTargetFrameworks(file))
89+
{
90+
throw new CommandException(Resources.MultipleTargetFrameworks);
91+
}
92+
8893
throw new CommandException(Resources.GetMetadataFailed);
8994
}
9095

@@ -131,6 +136,21 @@ private record class ProjectMetadata
131136
public Dictionary<string, Dictionary<string, string>[]> Items { get; set; } = null!;
132137
}
133138

139+
private static bool HasMultipleTargetFrameworks(string file)
140+
{
141+
var args = new List<string> { "msbuild", "/getProperty:TargetFrameworks", file };
142+
143+
var output = new StringBuilder();
144+
var exitCode = Exe.Run("dotnet", args, handleOutput: line => output.AppendLine(line));
145+
if (exitCode != 0)
146+
{
147+
return false;
148+
}
149+
150+
var outputString = output.ToString();
151+
return !string.IsNullOrWhiteSpace(outputString);
152+
}
153+
134154
private static void CopyBuildHost(
135155
Dictionary<string, string>[] runtimeCopyLocalItems,
136156
string targetDir)

src/dotnet-ef/Properties/Resources.Designer.cs

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dotnet-ef/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@
264264
<data name="MultipleStartupProjects" xml:space="preserve">
265265
<value>More than one project was found in the current working directory. Use the --startup-project option.</value>
266266
</data>
267+
<data name="MultipleTargetFrameworks" xml:space="preserve">
268+
<value>The project targets multiple frameworks. Use the --framework option to specify which target framework to use.</value>
269+
</data>
267270
<data name="NamespaceDescription" xml:space="preserve">
268271
<value>The namespace to use. Matches the directory by default.</value>
269272
</data>

src/dotnet-ef/RootCommand.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ protected override int Execute(string[] _)
6666
Reporter.WriteVerbose(Resources.UsingProject(projectFile));
6767
Reporter.WriteVerbose(Resources.UsingStartupProject(startupProjectFile));
6868

69-
var project = Project.FromFile(projectFile);
69+
var project = Project.FromFile(
70+
projectFile,
71+
_framework!.Value(),
72+
_configuration!.Value(),
73+
_runtime!.Value());
7074
var startupProject = Project.FromFile(
7175
startupProjectFile,
7276
_framework!.Value(),

0 commit comments

Comments
 (0)