Skip to content

Commit d3c685a

Browse files
authored
Merge pull request #15156 from tamasvajk/standalone/temp-folder-structure
C#: Fix working directory structures in standalone
2 parents 4412415 + b1413a1 commit d3c685a

File tree

9 files changed

+61
-50
lines changed

9 files changed

+61
-50
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -936,9 +936,8 @@ public void TestDotnetVersionNotInstalled()
936936
{
937937
actions.RunProcess["dotnet --list-sdks"] = 0;
938938
actions.RunProcessOut["dotnet --list-sdks"] = "2.1.2 [C:\\Program Files\\dotnet\\sdks]\n2.1.4 [C:\\Program Files\\dotnet\\sdks]";
939-
actions.RunProcess[@"chmod u+x dotnet-install.sh"] = 0;
940-
actions.RunProcess[@"./dotnet-install.sh --channel release --version 2.1.3 --install-dir scratch/.dotnet"] = 0;
941-
actions.RunProcess[@"rm dotnet-install.sh"] = 0;
939+
actions.RunProcess[@"chmod u+x scratch/.dotnet/dotnet-install.sh"] = 0;
940+
actions.RunProcess[@"scratch/.dotnet/dotnet-install.sh --channel release --version 2.1.3 --install-dir scratch/.dotnet"] = 0;
942941
actions.RunProcess[@"scratch/.dotnet/dotnet --info"] = 0;
943942
actions.RunProcess[@"scratch/.dotnet/dotnet clean C:\Project/test.csproj"] = 0;
944943
actions.RunProcess[@"scratch/.dotnet/dotnet restore C:\Project/test.csproj"] = 0;
@@ -960,10 +959,11 @@ public void TestDotnetVersionNotInstalled()
960959
961960
</Project>");
962961
actions.LoadXml[@"C:\Project/test.csproj"] = xml;
963-
actions.DownloadFiles.Add(("https://dot.net/v1/dotnet-install.sh", "dotnet-install.sh"));
962+
actions.DownloadFiles.Add(("https://dot.net/v1/dotnet-install.sh", "scratch/.dotnet/dotnet-install.sh"));
963+
actions.CreateDirectories.Add(@"scratch/.dotnet");
964964

965965
var autobuilder = CreateAutoBuilder(false, dotnetVersion: "2.1.3");
966-
TestAutobuilderScript(autobuilder, 0, 8);
966+
TestAutobuilderScript(autobuilder, 0, 7);
967967
}
968968

969969
[Fact]
@@ -972,9 +972,8 @@ public void TestDotnetVersionAlreadyInstalled()
972972
actions.RunProcess["dotnet --list-sdks"] = 0;
973973
actions.RunProcessOut["dotnet --list-sdks"] = @"2.1.3 [C:\Program Files\dotnet\sdks]
974974
2.1.4 [C:\Program Files\dotnet\sdks]";
975-
actions.RunProcess[@"chmod u+x dotnet-install.sh"] = 0;
976-
actions.RunProcess[@"./dotnet-install.sh --channel release --version 2.1.3 --install-dir scratch/.dotnet"] = 0;
977-
actions.RunProcess[@"rm dotnet-install.sh"] = 0;
975+
actions.RunProcess[@"chmod u+x scratch/.dotnet/dotnet-install.sh"] = 0;
976+
actions.RunProcess[@"scratch/.dotnet/dotnet-install.sh --channel release --version 2.1.3 --install-dir scratch/.dotnet"] = 0;
978977
actions.RunProcess[@"scratch/.dotnet/dotnet --info"] = 0;
979978
actions.RunProcess[@"scratch/.dotnet/dotnet clean C:\Project/test.csproj"] = 0;
980979
actions.RunProcess[@"scratch/.dotnet/dotnet restore C:\Project/test.csproj"] = 0;
@@ -996,10 +995,11 @@ public void TestDotnetVersionAlreadyInstalled()
996995
997996
</Project>");
998997
actions.LoadXml[@"C:\Project/test.csproj"] = xml;
999-
actions.DownloadFiles.Add(("https://dot.net/v1/dotnet-install.sh", "dotnet-install.sh"));
998+
actions.DownloadFiles.Add(("https://dot.net/v1/dotnet-install.sh", "scratch/.dotnet/dotnet-install.sh"));
999+
actions.CreateDirectories.Add(@"scratch/.dotnet");
10001000

10011001
var autobuilder = CreateAutoBuilder(false, dotnetVersion: "2.1.3");
1002-
TestAutobuilderScript(autobuilder, 0, 8);
1002+
TestAutobuilderScript(autobuilder, 0, 7);
10031003
}
10041004

10051005
private void TestDotnetVersionWindows(Action action, int commandsRun)

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,30 +190,41 @@ BuildScript GetInstall(string pwsh) =>
190190
}
191191
else
192192
{
193+
var dotnetInstallPath = builder.Actions.PathCombine(FileUtils.GetTemporaryWorkingDirectory(
194+
builder.Actions.GetEnvironmentVariable,
195+
builder.Options.Language.UpperCaseName,
196+
out var shouldCleanUp), ".dotnet", "dotnet-install.sh");
197+
193198
var downloadDotNetInstallSh = BuildScript.DownloadFile(
194199
"https://dot.net/v1/dotnet-install.sh",
195-
"dotnet-install.sh",
200+
dotnetInstallPath,
196201
e => builder.Log(Severity.Warning, $"Failed to download 'dotnet-install.sh': {e.Message}"));
197202

198203
var chmod = new CommandBuilder(builder.Actions).
199204
RunCommand("chmod").
200205
Argument("u+x").
201-
Argument("dotnet-install.sh");
206+
Argument(dotnetInstallPath);
202207

203208
var install = new CommandBuilder(builder.Actions).
204-
RunCommand("./dotnet-install.sh").
209+
RunCommand(dotnetInstallPath).
205210
Argument("--channel").
206211
Argument("release").
207212
Argument("--version").
208213
Argument(version).
209214
Argument("--install-dir").
210215
Argument(path);
211216

212-
var removeScript = new CommandBuilder(builder.Actions).
213-
RunCommand("rm").
214-
Argument("dotnet-install.sh");
217+
var buildScript = downloadDotNetInstallSh & chmod.Script & install.Script;
218+
219+
if (shouldCleanUp)
220+
{
221+
var removeScript = new CommandBuilder(builder.Actions).
222+
RunCommand("rm").
223+
Argument(dotnetInstallPath);
224+
buildScript &= removeScript.Script;
225+
}
215226

216-
return downloadDotNetInstallSh & chmod.Script & install.Script & BuildScript.Try(removeScript.Script);
227+
return buildScript;
217228
}
218229
});
219230
}

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: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,26 @@ public static string NestPaths(ILogger logger, string? outerpath, string innerpa
144144
return nested;
145145
}
146146

147+
private static readonly Lazy<string> tempFolderPath = new Lazy<string>(() =>
148+
{
149+
var tempPath = Path.GetTempPath();
150+
var name = Guid.NewGuid().ToString("N").ToUpper();
151+
var tempFolder = Path.Combine(tempPath, "GitHub", name);
152+
Directory.CreateDirectory(tempFolder);
153+
return tempFolder;
154+
});
155+
147156
public static string GetTemporaryWorkingDirectory(Func<string, string?> getEnvironmentVariable, string lang, out bool shouldCleanUp)
148157
{
149-
shouldCleanUp = false;
150158
var tempFolder = getEnvironmentVariable($"CODEQL_EXTRACTOR_{lang}_SCRATCH_DIR");
151-
152-
if (string.IsNullOrEmpty(tempFolder))
159+
if (!string.IsNullOrEmpty(tempFolder))
153160
{
154-
var tempPath = Path.GetTempPath();
155-
var name = Guid.NewGuid().ToString("N").ToUpper();
156-
tempFolder = Path.Combine(tempPath, "GitHub", name);
157-
shouldCleanUp = true;
161+
shouldCleanUp = false;
162+
return tempFolder;
158163
}
159164

160-
return tempFolder;
165+
shouldCleanUp = true;
166+
return tempFolderPath.Value;
161167
}
162168

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

csharp/ql/integration-tests/all-platforms/standalone_dependencies_net48/Assemblies.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ private string getPath(Assembly a) {
44
not a.getCompilation().getOutputAssembly() = a and
55
exists(string s | s = a.getFile().getAbsolutePath() |
66
result =
7-
s.substring(s.indexOf("GitHub/packages/") + "GitHub/packages/".length() + 16, s.length())
7+
s.substring(s.indexOf("test-db/working/") + "test-db/working/".length() + 16 +
8+
"/packages".length(), s.length())
89
or
910
result = s and
10-
not exists(s.indexOf("GitHub/packages/"))
11+
not exists(s.indexOf("test-db/working/"))
1112
)
1213
}
1314

csharp/ql/integration-tests/posix-only/standalone_dependencies/Assemblies.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ private string getPath(Assembly a) {
44
not a.getCompilation().getOutputAssembly() = a and
55
exists(string s | s = a.getFile().getAbsolutePath() |
66
result =
7-
s.substring(s.indexOf("GitHub/packages/") + "GitHub/packages/".length() + 16, s.length())
7+
s.substring(s.indexOf("test-db/working/") + "test-db/working/".length() + 16 +
8+
"/packages".length(), s.length())
89
or
910
result = s and
10-
not exists(s.indexOf("GitHub/packages/"))
11+
not exists(s.indexOf("test-db/working/"))
1112
)
1213
}
1314

csharp/ql/integration-tests/posix-only/standalone_dependencies_multi_target/Assemblies.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ private string getPath(Assembly a) {
44
not a.getCompilation().getOutputAssembly() = a and
55
exists(string s | s = a.getFile().getAbsolutePath() |
66
result =
7-
s.substring(s.indexOf("GitHub/packages/") + "GitHub/packages/".length() + 16, s.length())
7+
s.substring(s.indexOf("test-db/working/") + "test-db/working/".length() + 16 +
8+
"/packages".length(), s.length())
89
or
910
result = s and
10-
not exists(s.indexOf("GitHub/packages/"))
11+
not exists(s.indexOf("test-db/working/"))
1112
)
1213
}
1314

csharp/ql/integration-tests/posix-only/standalone_dependencies_nuget/Assemblies.ql

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,9 @@ private string getPath(Assembly a) {
44
not a.getCompilation().getOutputAssembly() = a and
55
exists(string s | s = a.getFile().getAbsolutePath() |
66
result =
7-
s.substring(s.indexOf("GitHub/packages/") + "GitHub/packages/".length() + 16, s.length())
8-
or
9-
result =
10-
s.substring(s.indexOf("GitHub/legacypackages/") + "GitHub/legacypackages/".length() + 16,
11-
s.length())
12-
// TODO: excluding all other assemblies from the test result as mono installations seem problematic on ARM runners.
13-
// or
14-
// result = s.substring(s.indexOf("lib/mono/") + "lib/mono/".length(), s.length())
15-
// or
16-
// result = s and
17-
// not exists(s.indexOf("GitHub/packages/")) and
18-
// not exists(s.indexOf("GitHub/legacypackages/")) and
19-
// not exists(s.indexOf("lib/mono/"))
7+
s.substring(s.indexOf("test-db/working/") + "test-db/working/".length() + 16 +
8+
"/legacypackages".length(), s.length())
9+
// TODO: include all other assemblies from the test results. Initially disable because mono installations were problematic on ARM runners.
2010
)
2111
}
2212

csharp/ql/integration-tests/windows-only/standalone_dependencies/Assemblies.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ private string getPath(Assembly a) {
44
not a.getCompilation().getOutputAssembly() = a and
55
exists(string s | s = a.getFile().getAbsolutePath() |
66
result =
7-
s.substring(s.indexOf("GitHub/packages/") + "GitHub/packages/".length() + 16, s.length())
7+
s.substring(s.indexOf("test-db/working/") + "test-db/working/".length() + 16 +
8+
"/packages".length(), s.length())
89
or
910
result = s and
10-
not exists(s.indexOf("GitHub/packages/"))
11+
not exists(s.indexOf("test-db/working/"))
1112
)
1213
}
1314

0 commit comments

Comments
 (0)