From dcd87a9825f719e3b7c18ca905d67f2145560856 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Thu, 23 Jan 2025 11:12:33 +0100 Subject: [PATCH 1/2] C#: Add .NET 3 integration test --- .../linux/standalone_dotnet3/Files.expected | 1 + .../linux/standalone_dotnet3/Files.ql | 5 +++++ .../linux/standalone_dotnet3/Program.cs | 12 ++++++++++++ .../linux/standalone_dotnet3/global.json | 6 ++++++ .../standalone_dotnet3/standalone_dotnet3.csproj | 8 ++++++++ .../linux/standalone_dotnet3/test.py | 4 ++++ 6 files changed, 36 insertions(+) create mode 100644 csharp/ql/integration-tests/linux/standalone_dotnet3/Files.expected create mode 100644 csharp/ql/integration-tests/linux/standalone_dotnet3/Files.ql create mode 100644 csharp/ql/integration-tests/linux/standalone_dotnet3/Program.cs create mode 100644 csharp/ql/integration-tests/linux/standalone_dotnet3/global.json create mode 100644 csharp/ql/integration-tests/linux/standalone_dotnet3/standalone_dotnet3.csproj create mode 100644 csharp/ql/integration-tests/linux/standalone_dotnet3/test.py diff --git a/csharp/ql/integration-tests/linux/standalone_dotnet3/Files.expected b/csharp/ql/integration-tests/linux/standalone_dotnet3/Files.expected new file mode 100644 index 000000000000..2085aa342e71 --- /dev/null +++ b/csharp/ql/integration-tests/linux/standalone_dotnet3/Files.expected @@ -0,0 +1 @@ +| Program.cs:0:0:0:0 | Program.cs | diff --git a/csharp/ql/integration-tests/linux/standalone_dotnet3/Files.ql b/csharp/ql/integration-tests/linux/standalone_dotnet3/Files.ql new file mode 100644 index 000000000000..bea5557a25f1 --- /dev/null +++ b/csharp/ql/integration-tests/linux/standalone_dotnet3/Files.ql @@ -0,0 +1,5 @@ +import csharp + +from File f +where f.fromSource() +select f diff --git a/csharp/ql/integration-tests/linux/standalone_dotnet3/Program.cs b/csharp/ql/integration-tests/linux/standalone_dotnet3/Program.cs new file mode 100644 index 000000000000..57340067c71d --- /dev/null +++ b/csharp/ql/integration-tests/linux/standalone_dotnet3/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace standalone_dotnet3 +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/csharp/ql/integration-tests/linux/standalone_dotnet3/global.json b/csharp/ql/integration-tests/linux/standalone_dotnet3/global.json new file mode 100644 index 000000000000..63dd7572fb2d --- /dev/null +++ b/csharp/ql/integration-tests/linux/standalone_dotnet3/global.json @@ -0,0 +1,6 @@ +{ + "sdk": { + "version": "3.1.403", + "rollForward": "latestFeature" + } +} \ No newline at end of file diff --git a/csharp/ql/integration-tests/linux/standalone_dotnet3/standalone_dotnet3.csproj b/csharp/ql/integration-tests/linux/standalone_dotnet3/standalone_dotnet3.csproj new file mode 100644 index 000000000000..c73e0d1692ab --- /dev/null +++ b/csharp/ql/integration-tests/linux/standalone_dotnet3/standalone_dotnet3.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/csharp/ql/integration-tests/linux/standalone_dotnet3/test.py b/csharp/ql/integration-tests/linux/standalone_dotnet3/test.py new file mode 100644 index 000000000000..3b57cc3d46fd --- /dev/null +++ b/csharp/ql/integration-tests/linux/standalone_dotnet3/test.py @@ -0,0 +1,4 @@ +import os + +def test(codeql, csharp): + codeql.database.create(build_mode="none") From ac4f82cfbb6db183d38b224e4df3ab3c64a4abb0 Mon Sep 17 00:00:00 2001 From: Tom Hvitved Date: Fri, 24 Jan 2025 15:13:27 +0100 Subject: [PATCH 2/2] C#: Verify that downloaded .NET CLIs are executable --- .../Semmle.Autobuild.CSharp/DotNetRule.cs | 4 ++- .../DependencyManager.cs | 1 - .../DotNet.cs | 26 ++++++++++++++----- .../Semmle.Extraction.Tests/DotNet.cs | 2 +- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs b/csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs index 43efcd0352f0..a29079d205bb 100644 --- a/csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs +++ b/csharp/autobuilder/Semmle.Autobuild.CSharp/DotNetRule.cs @@ -46,7 +46,9 @@ public BuildScript Analyse(IAutobuilder builder, bool au return WithDotNet(builder, ensureDotNetAvailable: false, (dotNetPath, environment) => { - var ret = GetInfoCommand(builder.Actions, dotNetPath, environment); + // When a custom .NET CLI has been installed, `dotnet --info` has already been executed + // to verify the installation. + var ret = dotNetPath is null ? GetInfoCommand(builder.Actions, dotNetPath, environment) : BuildScript.Success; foreach (var projectOrSolution in builder.ProjectsOrSolutionsToBuild) { var cleanCommand = GetCleanCommand(builder.Actions, dotNetPath, environment); diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs index b8773f0ae4a6..040e89dcec1d 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs @@ -41,7 +41,6 @@ public sealed partial class DependencyManager : IDisposable, ICompilationInfoCon private int conflictedReferences = 0; private readonly DirectoryInfo sourceDir; private string? dotnetPath; - private readonly TemporaryDirectory tempWorkingDirectory; private readonly bool cleanupTempWorkingDirectory; diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs index c1fdcc06e91b..dfabb7446186 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs @@ -19,17 +19,20 @@ public partial class DotNet : IDotNet private readonly ILogger logger; private readonly TemporaryDirectory? tempWorkingDirectory; - private DotNet(IDotNetCliInvoker dotnetCliInvoker, ILogger logger, TemporaryDirectory? tempWorkingDirectory = null) + private DotNet(IDotNetCliInvoker dotnetCliInvoker, ILogger logger, bool runDotnetInfo, TemporaryDirectory? tempWorkingDirectory = null) { this.tempWorkingDirectory = tempWorkingDirectory; this.dotnetCliInvoker = dotnetCliInvoker; this.logger = logger; - Info(); + if (runDotnetInfo) + { + Info(); + } } - private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, DependabotProxy? dependabotProxy) : this(new DotNetCliInvoker(logger, Path.Combine(dotNetPath ?? string.Empty, "dotnet"), dependabotProxy), logger, tempWorkingDirectory) { } + private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, DependabotProxy? dependabotProxy) : this(new DotNetCliInvoker(logger, Path.Combine(dotNetPath ?? string.Empty, "dotnet"), dependabotProxy), logger, dotNetPath is null, tempWorkingDirectory) { } - internal static IDotNet Make(IDotNetCliInvoker dotnetCliInvoker, ILogger logger) => new DotNet(dotnetCliInvoker, logger); + internal static IDotNet Make(IDotNetCliInvoker dotnetCliInvoker, ILogger logger, bool runDotnetInfo) => new DotNet(dotnetCliInvoker, logger, runDotnetInfo); public static IDotNet Make(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkingDirectory, DependabotProxy? dependabotProxy) => new DotNet(logger, dotNetPath, tempWorkingDirectory, dependabotProxy); @@ -169,7 +172,10 @@ private static BuildScript DownloadDotNet(IBuildActions actions, ILogger logger, if (versions.Count > 0) { - return DownloadDotNetVersion(actions, logger, tempWorkingDirectory, shouldCleanUp, installDir, versions); + return + DownloadDotNetVersion(actions, logger, tempWorkingDirectory, shouldCleanUp, installDir, versions) | + // if neither of the versions succeed, try the latest version + DownloadDotNetVersion(actions, logger, tempWorkingDirectory, shouldCleanUp, installDir, [LatestDotNetSdkVersion], needExactVersion: false); } if (ensureDotNetAvailable) @@ -269,6 +275,14 @@ BuildScript GetInstall(string pwsh) => Argument(path).Script; } + var dotnetInfo = new CommandBuilder(actions). + RunCommand(actions.PathCombine(path, "dotnet")). + Argument("--info").Script; + + Func getInstallAndVerify = version => + // run `dotnet --info` after install, to check that it executes successfully + getInstall(version) & dotnetInfo; + var installScript = prelude & BuildScript.Failure; var attempted = new HashSet(); @@ -283,7 +297,7 @@ BuildScript GetInstall(string pwsh) => // When there are multiple versions requested, we want to try to fetch them all, reporting // a successful exit code when at least one of them succeeds - return combinedExit != 0 ? getInstall(version) : BuildScript.Bind(getInstall(version), _ => BuildScript.Success); + return combinedExit != 0 ? getInstallAndVerify(version) : BuildScript.Bind(getInstallAndVerify(version), _ => BuildScript.Success); }); } diff --git a/csharp/extractor/Semmle.Extraction.Tests/DotNet.cs b/csharp/extractor/Semmle.Extraction.Tests/DotNet.cs index 3c1b41f54bf1..c584b607ec8e 100644 --- a/csharp/extractor/Semmle.Extraction.Tests/DotNet.cs +++ b/csharp/extractor/Semmle.Extraction.Tests/DotNet.cs @@ -45,7 +45,7 @@ public bool RunCommand(string args, string? workingDirectory, out IList public class DotNetTests { private static IDotNet MakeDotnet(IDotNetCliInvoker dotnetCliInvoker) => - DotNet.Make(dotnetCliInvoker, new LoggerStub()); + DotNet.Make(dotnetCliInvoker, new LoggerStub(), true); private static IList MakeDotnetRestoreOutput() => new List {