Skip to content

Commit 0cd7dd5

Browse files
committed
Ensure temp compilation uses .NET 9
1 parent 4dcf801 commit 0cd7dd5

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/Neo.Compiler.CSharp/CompilationEngine/CompilationEngine.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.IO;
2424
using System.Linq;
2525
using System.Reflection;
26+
using System.Runtime.Versioning;
2627
using System.Threading.Tasks;
2728
using System.Xml.Linq;
2829
using BigInteger = System.Numerics.BigInteger;
@@ -146,11 +147,13 @@ public List<CompilationContext> CompileSources(CompilationSourceReferences refer
146147
{string.Join(Environment.NewLine, references.Projects!.Select(u => $" <ProjectReference Include =\"{u}\"/>"))}
147148
</ItemGroup>";
148149

150+
string targetFramework = GetTargetFrameworkMoniker();
151+
149152
var csproj = $@"
150153
<Project Sdk=""Microsoft.NET.Sdk"">
151154
152155
<PropertyGroup>
153-
<TargetFramework>{AppContext.TargetFrameworkName!}</TargetFramework>
156+
<TargetFramework>{targetFramework}</TargetFramework>
154157
<ImplicitUsings>enable</ImplicitUsings>
155158
<Nullable>enable</Nullable>
156159
</PropertyGroup>
@@ -216,6 +219,30 @@ public List<CompilationContext> CompileProject(string csproj, List<INamedTypeSym
216219
return targetContractName == null ? CompileProjectContractsWithPrepare(sortedClasses, classDependencies, allClassSymbols) : [CompileProjectContractWithPrepare(sortedClasses, classDependencies, allClassSymbols, targetContractName)];
217220
}
218221

222+
private static string GetTargetFrameworkMoniker()
223+
{
224+
const string fallback = "net9.0";
225+
string? tfm = AppContext.TargetFrameworkName;
226+
if (string.IsNullOrEmpty(tfm))
227+
return fallback;
228+
229+
try
230+
{
231+
FrameworkName framework = new(tfm);
232+
if (!string.Equals(framework.Identifier, ".NETCoreApp", StringComparison.OrdinalIgnoreCase))
233+
return fallback;
234+
235+
if (framework.Version.Major < 9)
236+
return fallback;
237+
238+
return $"net{framework.Version.Major}.{framework.Version.Minor}";
239+
}
240+
catch
241+
{
242+
return fallback;
243+
}
244+
}
245+
219246
public (List<INamedTypeSymbol> sortedClasses, Dictionary<INamedTypeSymbol, List<INamedTypeSymbol>> classDependencies, List<INamedTypeSymbol?> allClassSymbols) PrepareProjectContracts(string csproj)
220247
{
221248
Compilation ??= GetCompilation(csproj);

0 commit comments

Comments
 (0)