Skip to content

Commit 866f124

Browse files
committed
C#: Use CODEQL_EXTRACTOR_CSHARP_SCRATCH_DIR instead of Path.GetTempPath
1 parent 7819dcf commit 866f124

File tree

9 files changed

+65
-37
lines changed

9 files changed

+65
-37
lines changed

cpp/autobuilder/Semmle.Autobuild.Cpp.Tests/BuildScripts.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public void TestDefaultCppAutobuilder()
326326
public void TestCppAutobuilderSuccess()
327327
{
328328
Actions.RunProcess[@"cmd.exe /C nuget restore C:\Project\test.sln -DisableParallelProcessing"] = 1;
329-
Actions.RunProcess[@"cmd.exe /C C:\Project\.nuget\nuget.exe restore C:\Project\test.sln -DisableParallelProcessing"] = 0;
329+
Actions.RunProcess[@"cmd.exe /C scratch\.nuget\nuget.exe restore C:\Project\test.sln -DisableParallelProcessing"] = 0;
330330
Actions.RunProcess[@"cmd.exe /C CALL ^""C:\Program^ Files^ ^(x86^)\Microsoft^ Visual^ Studio^ 14.0\VC\vcvarsall.bat^"" && set Platform=&& type NUL && msbuild C:\Project\test.sln /t:rebuild /p:Platform=""x86"" /p:Configuration=""Release"""] = 0;
331331
Actions.RunProcessOut[@"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe -prerelease -legacy -property installationPath"] = "";
332332
Actions.RunProcess[@"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe -prerelease -legacy -property installationPath"] = 1;
@@ -337,10 +337,11 @@ public void TestCppAutobuilderSuccess()
337337
Actions.FileExists[@"C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"] = true;
338338
Actions.FileExists[@"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"] = true;
339339
Actions.FileExists[@"C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"] = true;
340+
Actions.GetEnvironmentVariable["CODEQL_EXTRACTOR_CPP_SCRATCH_DIR"] = "scratch";
340341
Actions.EnumerateFiles[@"C:\Project"] = "foo.cs\ntest.slx";
341342
Actions.EnumerateDirectories[@"C:\Project"] = "";
342-
Actions.CreateDirectories.Add(@"C:\Project\.nuget");
343-
Actions.DownloadFiles.Add(("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", @"C:\Project\.nuget\nuget.exe"));
343+
Actions.CreateDirectories.Add(@"scratch\.nuget");
344+
Actions.DownloadFiles.Add(("https://dist.nuget.org/win-x86-commandline/latest/nuget.exe", @"scratch\.nuget\nuget.exe"));
344345

345346
var autobuilder = CreateAutoBuilder(true);
346347
var solution = new TestSolution(@"C:\Project\test.sln");

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

Lines changed: 48 additions & 23 deletions
Large diffs are not rendered by default.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public BuildScript Analyse(IAutobuilder<CSharpAutobuildOptions> builder, bool au
8181
/// </summary>
8282
public static BuildScript WithDotNet(IAutobuilder<AutobuildOptionsShared> builder, Func<string?, IDictionary<string, string>?, BuildScript> f)
8383
{
84-
var installDir = builder.Actions.PathCombine(builder.Options.RootDirectory, ".dotnet");
84+
var installDir = builder.Actions.PathCombine(FileUtils.GetTemporaryWorkingDirectory(builder.Actions.GetEnvironmentVariable, builder.Options.Language.UpperCaseName, out var _), ".dotnet");
8585
var installScript = DownloadDotNet(builder, installDir);
8686
return BuildScript.Bind(installScript, installed =>
8787
{

csharp/autobuilder/Semmle.Autobuild.Shared/MsBuildRule.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public BuildScript Analyse(IAutobuilder<AutobuildOptionsShared> builder, bool au
6060
// Use `nuget.exe` from source code repo, if present, otherwise first attempt with global
6161
// `nuget` command, and if that fails, attempt to download `nuget.exe` from nuget.org
6262
var nuget = builder.GetFilename("nuget.exe").Select(t => t.Item1).FirstOrDefault() ?? "nuget";
63-
var nugetDownload = builder.Actions.PathCombine(builder.Options.RootDirectory, ".nuget", "nuget.exe");
63+
var nugetDownloadPath = builder.Actions.PathCombine(FileUtils.GetTemporaryWorkingDirectory(builder.Actions.GetEnvironmentVariable, builder.Options.Language.UpperCaseName, out var _), ".nuget", "nuget.exe");
6464
var nugetDownloaded = false;
6565

6666
var ret = BuildScript.Success;
@@ -96,13 +96,13 @@ BuildScript GetNugetRestoreScript() =>
9696
// If `nuget restore` fails, and we have not already attempted to download `nuget.exe`,
9797
// download it and reattempt `nuget restore`.
9898
var nugetDownloadAndRestore =
99-
BuildScript.Bind(DownloadNugetExe(builder, nugetDownload), exitCode =>
99+
BuildScript.Bind(DownloadNugetExe(builder, nugetDownloadPath), exitCode =>
100100
{
101101
nugetDownloaded = true;
102102
if (exitCode != 0)
103103
return BuildScript.Failure;
104104

105-
nuget = nugetDownload;
105+
nuget = nugetDownloadPath;
106106
return GetNugetRestoreScript();
107107
});
108108
ret &= BuildScript.Try(nugetRestore | nugetDownloadAndRestore | msbuildRestoreCommand.Script);

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ private static string ComputeTempDirectory(string srcDir, string packages = "pac
479479
foreach (var b in sha.Take(8))
480480
sb.AppendFormat("{0:x2}", b);
481481

482-
return Path.Combine(Path.GetTempPath(), "GitHub", packages, sb.ToString());
482+
return Path.Combine(FileUtils.GetTemporaryWorkingDirectory(out var _), "GitHub", packages, sb.ToString());
483483
}
484484

485485
/// <summary>

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/Razor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Text;
55
using System.Linq;
6+
using Semmle.Util;
67

78
namespace Semmle.Extraction.CSharp.DependencyFetching
89
{
@@ -51,7 +52,7 @@ private static void GenerateAnalyzerConfig(IEnumerable<string> cshtmls, string a
5152
public IEnumerable<string> GenerateFiles(IEnumerable<string> cshtmls, IEnumerable<string> references, string workingDirectory)
5253
{
5354
var name = Guid.NewGuid().ToString("N").ToUpper();
54-
var tempPath = Path.GetTempPath();
55+
var tempPath = FileUtils.GetTemporaryWorkingDirectory(out var _);
5556
var analyzerConfig = Path.Combine(tempPath, $"{name}.txt");
5657
var dllPath = Path.Combine(tempPath, $"{name}.dll");
5758
var cscArgsPath = Path.Combine(tempPath, $"{name}.rsp");

csharp/extractor/Semmle.Extraction/TrapWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public TrapWriter(ILogger logger, PathTransformer.ITransformedPath outputfile, s
4848

4949
writerLazy = new Lazy<StreamWriter>(() =>
5050
{
51-
var tempPath = trap ?? Path.GetTempPath();
51+
var tempPath = trap ?? FileUtils.GetTemporaryWorkingDirectory(out var _);
5252

5353
do
5454
{

csharp/extractor/Semmle.Util/EnvironmentVariables.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ public class EnvironmentVariables
77
public static string? GetExtractorOption(string name) =>
88
Environment.GetEnvironmentVariable($"CODEQL_EXTRACTOR_CSHARP_OPTION_{name.ToUpper()}");
99

10-
public static string? GetScratchDirectory() => Environment.GetEnvironmentVariable("CODEQL_EXTRACTOR_CSHARP_SCRATCH_DIR");
11-
1210
public static int GetDefaultNumberOfThreads()
1311
{
1412
if (!int.TryParse(Environment.GetEnvironmentVariable("CODEQL_THREADS"), out var threads) || threads == -1)

csharp/extractor/Semmle.Util/FileUtils.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ public static string NestPaths(ILogger logger, string? outerpath, string innerpa
144144
return nested;
145145
}
146146

147-
public static string GetTemporaryWorkingDirectory(out bool shouldCleanUp)
147+
public static string GetTemporaryWorkingDirectory(Func<string, string?> getEnvironmentVariable, string lang, out bool shouldCleanUp)
148148
{
149149
shouldCleanUp = false;
150-
var tempFolder = EnvironmentVariables.GetScratchDirectory();
150+
var tempFolder = getEnvironmentVariable($"CODEQL_EXTRACTOR_{lang}_SCRATCH_DIR");
151151

152152
if (string.IsNullOrEmpty(tempFolder))
153153
{
@@ -160,6 +160,9 @@ public static string GetTemporaryWorkingDirectory(out bool shouldCleanUp)
160160
return tempFolder;
161161
}
162162

163+
public static string GetTemporaryWorkingDirectory(out bool shouldCleanUp) =>
164+
GetTemporaryWorkingDirectory(Environment.GetEnvironmentVariable, "CSHARP", out shouldCleanUp);
165+
163166
public static FileInfo CreateTemporaryFile(string extension, out bool shouldCleanUpContainingFolder)
164167
{
165168
var tempFolder = GetTemporaryWorkingDirectory(out shouldCleanUpContainingFolder);

0 commit comments

Comments
 (0)