Skip to content

Commit 648c06c

Browse files
committed
Simplify dotnet SDK check in autobuilder
1 parent c0d82cb commit 648c06c

File tree

2 files changed

+24
-37
lines changed

2 files changed

+24
-37
lines changed

csharp/autobuilder/Semmle.Autobuild.CSharp.Tests/BuildScripts.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -558,8 +558,8 @@ public void TestVcVarsAllBatFiles()
558558
[Fact]
559559
public void TestLinuxBuildlessExtractionSuccess()
560560
{
561-
actions.RunProcess["dotnet --info"] = 0;
562-
actions.RunProcessOut["dotnet --info"] = "";
561+
actions.RunProcess["dotnet --list-sdks"] = 0;
562+
actions.RunProcessOut["dotnet --list-sdks"] = "any version";
563563
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone"] = 0;
564564
actions.FileExists["csharp.log"] = true;
565565
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";
@@ -575,8 +575,8 @@ public void TestLinuxBuildlessExtractionSuccess()
575575
[Fact]
576576
public void TestLinuxBuildlessExtractionFailed()
577577
{
578-
actions.RunProcess["dotnet --info"] = 0;
579-
actions.RunProcessOut["dotnet --info"] = "";
578+
actions.RunProcess["dotnet --list-sdks"] = 0;
579+
actions.RunProcessOut["dotnet --list-sdks"] = "any version";
580580
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone"] = 10;
581581
actions.FileExists["csharp.log"] = true;
582582
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";
@@ -592,8 +592,8 @@ public void TestLinuxBuildlessExtractionFailed()
592592
[Fact]
593593
public void TestLinuxBuildlessExtractionSolution()
594594
{
595-
actions.RunProcess["dotnet --info"] = 0;
596-
actions.RunProcessOut["dotnet --info"] = "";
595+
actions.RunProcess["dotnet --list-sdks"] = 0;
596+
actions.RunProcessOut["dotnet --list-sdks"] = "any version";
597597
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone"] = 0;
598598
actions.FileExists["csharp.log"] = true;
599599
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";
@@ -609,8 +609,8 @@ public void TestLinuxBuildlessExtractionSolution()
609609
[Fact]
610610
public void TestLinuxBuildlessExtractionNoDotnet()
611611
{
612-
actions.RunProcess["dotnet --info"] = 1;
613-
actions.RunProcessOut["dotnet --info"] = "";
612+
actions.RunProcess["dotnet --list-sdks"] = 1;
613+
actions.RunProcessOut["dotnet --list-sdks"] = "";
614614
actions.RunProcess[@"chmod u+x scratch/.dotnet/dotnet-install.sh"] = 0;
615615
actions.RunProcess[@"scratch/.dotnet/dotnet-install.sh --channel release --version 8.0.101 --install-dir scratch/.dotnet"] = 0;
616616
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone --dotnet scratch/.dotnet"] = 0;
@@ -915,8 +915,8 @@ public void TestSkipNugetMsBuild()
915915
[Fact]
916916
public void TestSkipNugetBuildless()
917917
{
918-
actions.RunProcess["dotnet --info"] = 0;
919-
actions.RunProcessOut["dotnet --info"] = "";
918+
actions.RunProcess["dotnet --list-sdks"] = 0;
919+
actions.RunProcessOut["dotnet --list-sdks"] = "any version";
920920
actions.RunProcess[@"C:\codeql\csharp/tools/linux64/Semmle.Extraction.CSharp.Standalone"] = 0;
921921
actions.FileExists["csharp.log"] = true;
922922
actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CSHARP_TRAP_DIR"] = "";

csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs

+14-27
Original file line numberDiff line numberDiff line change
@@ -163,18 +163,8 @@ private static BuildScript DownloadDotNet(IAutobuilder<AutobuildOptionsShared> b
163163

164164
if (ensureDotNetAvailable)
165165
{
166-
return BuildScript.Bind(GetInfoScript(builder.Actions), (infoLines, infoRet) =>
167-
{
168-
if (infoRet == 0)
169-
{
170-
return BuildScript.Failure;
171-
}
172-
173-
const string latestDotNetSdkVersion = "8.0.101";
174-
builder.Log(Severity.Info, $"No .NET Core SDK found. Attempting to install version {latestDotNetSdkVersion}.");
175-
return DownloadDotNetVersion(builder, installDir, latestDotNetSdkVersion, checkInstalledSdkVersion: false);
176-
});
177-
166+
const string latestDotNetSdkVersion = "8.0.101";
167+
return DownloadDotNetVersion(builder, installDir, latestDotNetSdkVersion, needExactVersion: false);
178168
}
179169

180170
return BuildScript.Failure;
@@ -186,20 +176,25 @@ private static BuildScript DownloadDotNet(IAutobuilder<AutobuildOptionsShared> b
186176
///
187177
/// See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-install-script.
188178
/// </summary>
189-
private static BuildScript DownloadDotNetVersion(IAutobuilder<AutobuildOptionsShared> builder, string path, string version, bool checkInstalledSdkVersion = true)
179+
private static BuildScript DownloadDotNetVersion(IAutobuilder<AutobuildOptionsShared> builder, string path, string version, bool needExactVersion = true)
190180
{
191-
var firstScript = checkInstalledSdkVersion
192-
? GetInstalledSdksScript(builder.Actions)
193-
: BuildScript.Success;
194-
195-
return BuildScript.Bind(firstScript, (sdks, sdksRet) =>
181+
return BuildScript.Bind(GetInstalledSdksScript(builder.Actions), (sdks, sdksRet) =>
196182
{
197-
if (checkInstalledSdkVersion && sdksRet == 0 && sdks.Count == 1 && sdks[0].StartsWith(version + " ", StringComparison.Ordinal))
183+
if (needExactVersion && sdksRet == 0 && sdks.Count == 1 && sdks[0].StartsWith(version + " ", StringComparison.Ordinal))
198184
{
199185
// The requested SDK is already installed (and no other SDKs are installed), so
200186
// no need to reinstall
201187
return BuildScript.Failure;
202188
}
189+
else if (!needExactVersion && sdksRet == 0 && sdks.Count > 0)
190+
{
191+
// there's at least one SDK installed, so no need to reinstall
192+
return BuildScript.Failure;
193+
}
194+
else if (!needExactVersion && sdksRet != 0)
195+
{
196+
builder.Log(Severity.Info, "No .NET Core SDK found.");
197+
}
203198

204199
builder.Log(Severity.Info, "Attempting to download .NET Core {0}", version);
205200

@@ -269,14 +264,6 @@ private static BuildScript GetInstalledSdksScript(IBuildActions actions)
269264
return listSdks.Script;
270265
}
271266

272-
private static BuildScript GetInfoScript(IBuildActions actions)
273-
{
274-
var info = new CommandBuilder(actions, silent: true).
275-
RunCommand("dotnet").
276-
Argument("--info");
277-
return info.Script;
278-
}
279-
280267
private static string DotNetCommand(IBuildActions actions, string? dotNetPath) =>
281268
dotNetPath is not null ? actions.PathCombine(dotNetPath, "dotnet") : "dotnet";
282269

0 commit comments

Comments
 (0)