Skip to content

Commit b91ad04

Browse files
authored
Merge pull request #17257 from tamasvajk/buildless/temp-locations
C#: Change random temp folder names to hash values
2 parents b625557 + 658326d commit b91ad04

File tree

42 files changed

+1288
-1434
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1288
-1434
lines changed

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public NugetPackageRestorer(
4444
this.logger = logger;
4545
this.compilationInfoContainer = compilationInfoContainer;
4646

47-
PackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath(fileProvider.SourceDir.FullName, "packages"), "package", logger);
48-
legacyPackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath(fileProvider.SourceDir.FullName, "legacypackages"), "legacy package", logger);
49-
missingPackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath(fileProvider.SourceDir.FullName, "missingpackages"), "missing package", logger);
47+
PackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath("packages"), "package", logger);
48+
legacyPackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath("legacypackages"), "legacy package", logger);
49+
missingPackageDirectory = new TemporaryDirectory(ComputeTempDirectoryPath("missingpackages"), "missing package", logger);
5050
}
5151

5252
public string? TryRestore(string package)
@@ -338,7 +338,7 @@ private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<Dep
338338
}
339339

340340
logger.LogInfo($"Found {notYetDownloadedPackages.Count} packages that are not yet restored");
341-
using var tempDir = new TemporaryDirectory(ComputeTempDirectoryPath(fileProvider.SourceDir.FullName, "nugetconfig"), "generated nuget config", logger);
341+
using var tempDir = new TemporaryDirectory(ComputeTempDirectoryPath("nugetconfig"), "generated nuget config", logger);
342342
var nugetConfig = fallbackNugetFeeds is null
343343
? GetNugetConfig()
344344
: CreateFallbackNugetConfig(fallbackNugetFeeds, tempDir.DirInfo.FullName);
@@ -771,19 +771,19 @@ public void Dispose()
771771
}
772772

773773
/// <summary>
774-
/// Computes a unique temp directory for the packages associated
775-
/// with this source tree. Use a SHA1 of the directory name.
774+
/// Returns the full path to a temporary directory with the given subfolder name.
776775
/// </summary>
777-
/// <returns>The full path of the temp directory.</returns>
778-
private static string ComputeTempDirectoryPath(string srcDir, string subfolderName)
776+
private static string ComputeTempDirectoryPath(string subfolderName)
779777
{
780-
var bytes = Encoding.Unicode.GetBytes(srcDir);
781-
var sha = SHA1.HashData(bytes);
782-
var sb = new StringBuilder();
783-
foreach (var b in sha.Take(8))
784-
sb.AppendFormat("{0:x2}", b);
778+
return Path.Combine(FileUtils.GetTemporaryWorkingDirectory(out _), subfolderName);
779+
}
785780

786-
return Path.Combine(FileUtils.GetTemporaryWorkingDirectory(out _), sb.ToString(), subfolderName);
781+
/// <summary>
782+
/// Computes a unique temporary directory path based on the source directory and the subfolder name.
783+
/// </summary>
784+
private static string ComputeTempDirectoryPath(string srcDir, string subfolderName)
785+
{
786+
return Path.Combine(FileUtils.GetTemporaryWorkingDirectory(out _), FileUtils.ComputeHash(srcDir), subfolderName);
787787
}
788788
}
789789
}

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ protected override IEnumerable<string> Run()
8383
var targetDir = GetTemporaryWorkingDirectory(FileType.ToLowerInvariant());
8484

8585
return groupedFiles
86-
.SelectMany(group => sourceGenerator.RunSourceGenerator(group.Value, group.Key, references, targetDir));
86+
.SelectMany(group => sourceGenerator.RunSourceGenerator(group.Value, group.Key, references, targetDir, fileProvider.SourceDir.FullName));
8787
}
8888
catch (Exception ex)
8989
{

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorWrapper/DotnetSourceGeneratorWrapper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ public DotnetSourceGeneratorWrapper(
3333

3434
protected abstract void GenerateAnalyzerConfig(IEnumerable<string> additionalFiles, string csprojFile, string analyzerConfigPath);
3535

36-
public IEnumerable<string> RunSourceGenerator(IEnumerable<string> additionalFiles, string csprojFile, IEnumerable<string> references, string targetDir)
36+
public IEnumerable<string> RunSourceGenerator(IEnumerable<string> additionalFiles, string csprojFile, IEnumerable<string> references, string targetDir, string sourceDir)
3737
{
3838
try
3939
{
40-
var name = Guid.NewGuid().ToString("N").ToUpper();
40+
var relativePathToCsProj = Path.GetRelativePath(sourceDir, csprojFile);
41+
var name = FileUtils.ComputeHash($"{relativePathToCsProj}\n{this.GetType().Name}");
4142
using var tempDir = new TemporaryDirectory(Path.Join(FileUtils.GetTemporaryWorkingDirectory(out _), "source-generator"), "source generator temporary", logger);
4243
var analyzerConfigPath = Path.Combine(tempDir.DirInfo.FullName, $"{name}.txt");
4344
var dllPath = Path.Combine(tempDir.DirInfo.FullName, $"{name}.dll");

csharp/extractor/Semmle.Util/FileUtils.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,32 @@ public static void TryDelete(string file)
8787
}
8888

8989
/// <summary>
90-
/// Computes the hash of <paramref name="filePath"/>.
90+
/// Computes the hash of the file at <paramref name="filePath"/>.
9191
/// </summary>
9292
public static string ComputeFileHash(string filePath)
9393
{
9494
using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
95-
using var shaAlg = SHA256.Create();
96-
var sha = shaAlg.ComputeHash(fileStream);
95+
var sha = SHA256.HashData(fileStream);
96+
return GetHashString(sha);
97+
}
98+
99+
/// <summary>
100+
/// Computes the hash of <paramref name="input"/>.
101+
/// </summary>
102+
public static string ComputeHash(string input)
103+
{
104+
var bytes = Encoding.Unicode.GetBytes(input);
105+
var sha = MD5.HashData(bytes); // MD5 to keep it shorter than SHA256
106+
return GetHashString(sha).ToUpper();
107+
}
108+
109+
private static string GetHashString(byte[] sha)
110+
{
97111
var hex = new StringBuilder(sha.Length * 2);
98112
foreach (var b in sha)
113+
{
99114
hex.AppendFormat("{0:x2}", b);
115+
}
100116
return hex.ToString();
101117
}
102118

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
| Program.cs |
22
| Views/Home/Index.cshtml |
3-
| _ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_test_test_Views_Home_Index_cshtml.g.cs |
43
| test-db/working/implicitUsings/GlobalUsings.g.cs |
4+
| test-db/working/razor/EC52D77FE9BF67AD10C5C3F248392316/Microsoft.CodeAnalysis.Razor.Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/[...]_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_test_test_Views_Home_Index_cshtml.g.cs |

csharp/ql/integration-tests/all-platforms/cshtml_standalone/Files.ql

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@ private string getPath(File f) {
44
result = f.getRelativePath() and
55
not exists(result.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_"))
66
or
7-
exists(int index |
8-
index =
7+
exists(int index1, int index2, string pattern |
8+
pattern = "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator" and
9+
index1 = f.getRelativePath().indexOf(pattern) and
10+
index2 =
911
f.getRelativePath()
1012
.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and
11-
result = f.getRelativePath().substring(index, f.getRelativePath().length())
13+
result =
14+
f.getRelativePath().substring(0, index1 + pattern.length()) + "/[...]" +
15+
f.getRelativePath().substring(index2, f.getRelativePath().length())
1216
)
1317
}
1418

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| Program.cs |
2-
| test-db/working/implicitUsings/GlobalUsings.g.cs |
1+
| Program.cs:0:0:0:0 | Program.cs |
2+
| test-db/working/implicitUsings/GlobalUsings.g.cs:0:0:0:0 | test-db/working/implicitUsings/GlobalUsings.g.cs |
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
import csharp
22

3-
private string getPath(File f) {
4-
result = f.getRelativePath() and
5-
not exists(
6-
result
7-
.indexOf("_semmle_code_target_codeql_csharp_integration_tests_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_")
8-
)
9-
or
10-
exists(int index |
11-
index =
12-
f.getRelativePath()
13-
.indexOf("_semmle_code_target_codeql_csharp_integration_tests_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and
14-
result = f.getRelativePath().substring(index, f.getRelativePath().length())
15-
)
16-
}
17-
183
from File f
194
where f.fromSource() or f.getExtension() = "cshtml"
20-
select getPath(f)
5+
select f
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
| Program.cs |
22
| Views/Home/Index.cshtml |
3-
| _ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_net6_test_test_Views_Home_Index_cshtml.g.cs |
43
| test-db/working/implicitUsings/GlobalUsings.g.cs |
4+
| test-db/working/razor/EC52D77FE9BF67AD10C5C3F248392316/[...]/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/[...]_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_net6_test_test_Views_Home_Index_cshtml.g.cs |

csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.ql

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ private string getPath(File f) {
44
result = f.getRelativePath() and
55
not exists(result.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_"))
66
or
7-
exists(int index |
8-
index =
7+
exists(int index0, int index1, int index2, string pattern0, string pattern1 |
8+
// TODO: Remove index0 and pattern0. Currently there's some instability in the path depending on which dotnet SDK is being used. (See issue #448)
9+
pattern0 = "EC52D77FE9BF67AD10C5C3F248392316" and
10+
index0 = f.getRelativePath().indexOf(pattern0) and
11+
pattern1 = "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator" and
12+
index1 = f.getRelativePath().indexOf(pattern1) and
13+
index2 =
914
f.getRelativePath()
1015
.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and
11-
result = f.getRelativePath().substring(index, f.getRelativePath().length())
16+
result =
17+
f.getRelativePath().substring(0, index0 + pattern0.length()) + "/[...]/" +
18+
f.getRelativePath().substring(index1, index1 + pattern1.length()) + "/[...]" +
19+
f.getRelativePath().substring(index2, f.getRelativePath().length())
1220
)
1321
}
1422

0 commit comments

Comments
 (0)