Skip to content

Commit dd64b43

Browse files
committed
C#: Fix working directory structures in standalone
1 parent f50817e commit dd64b43

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
5252
this.progressMonitor = new ProgressMonitor(logger);
5353
this.sourceDir = new DirectoryInfo(srcDir);
5454

55-
packageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName));
55+
packageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName, "packages"));
5656
legacyPackageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName, "legacypackages"));
5757
missingPackageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName, "missingpackages"));
5858

@@ -467,15 +467,15 @@ private IEnumerable<FileInfo> GetAllFiles()
467467
/// with this source tree. Use a SHA1 of the directory name.
468468
/// </summary>
469469
/// <returns>The full path of the temp directory.</returns>
470-
private static string ComputeTempDirectory(string srcDir, string packages = "packages")
470+
private static string ComputeTempDirectory(string srcDir, string subfolderName)
471471
{
472472
var bytes = Encoding.Unicode.GetBytes(srcDir);
473473
var sha = SHA1.HashData(bytes);
474474
var sb = new StringBuilder();
475475
foreach (var b in sha.Take(8))
476476
sb.AppendFormat("{0:x2}", b);
477477

478-
return Path.Combine(FileUtils.GetTemporaryWorkingDirectory(out var _), "GitHub", packages, sb.ToString());
478+
return Path.Combine(FileUtils.GetTemporaryWorkingDirectory(out var _), sb.ToString(), subfolderName);
479479
}
480480

481481
/// <summary>
@@ -723,7 +723,7 @@ private void DownloadMissingPackages(List<FileInfo> allFiles, ISet<string> dllPa
723723
Parallel.ForEach(notYetDownloadedPackages, new ParallelOptions { MaxDegreeOfParallelism = options.Threads }, package =>
724724
{
725725
progressMonitor.NugetInstall(package);
726-
using var tempDir = new TemporaryDirectory(ComputeTempDirectory(package));
726+
using var tempDir = new TemporaryDirectory(ComputeTempDirectory(package, "missingpackages_workingdir"));
727727
var success = dotnet.New(tempDir.DirInfo.FullName);
728728
if (!success)
729729
{

csharp/extractor/Semmle.Util/FileUtils.cs

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

147+
private static string? tempFolderPath = null;
148+
private static readonly object lockObject = new();
149+
147150
public static string GetTemporaryWorkingDirectory(Func<string, string?> getEnvironmentVariable, string lang, out bool shouldCleanUp)
148151
{
149152
shouldCleanUp = false;
150153
var tempFolder = getEnvironmentVariable($"CODEQL_EXTRACTOR_{lang}_SCRATCH_DIR");
154+
if (!string.IsNullOrEmpty(tempFolder))
155+
{
156+
return tempFolder;
157+
}
151158

152-
if (string.IsNullOrEmpty(tempFolder))
159+
if (!string.IsNullOrEmpty(tempFolderPath))
153160
{
161+
shouldCleanUp = true;
162+
return tempFolderPath;
163+
}
164+
165+
lock (lockObject)
166+
{
167+
if (!string.IsNullOrEmpty(tempFolderPath))
168+
{
169+
shouldCleanUp = true;
170+
return tempFolderPath;
171+
}
172+
154173
var tempPath = Path.GetTempPath();
155174
var name = Guid.NewGuid().ToString("N").ToUpper();
156175
tempFolder = Path.Combine(tempPath, "GitHub", name);
176+
Directory.CreateDirectory(tempFolder);
177+
tempFolderPath = tempFolder;
157178
shouldCleanUp = true;
179+
return tempFolder;
158180
}
159-
160-
return tempFolder;
161181
}
162182

163183
public static string GetTemporaryWorkingDirectory(out bool shouldCleanUp) =>

0 commit comments

Comments
 (0)