Skip to content

Commit 576eda3

Browse files
committed
C#: Add missing package directory to included dlls.
1 parent 4cebb7e commit 576eda3

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public sealed class DependencyManager : IDisposable
3131
private readonly IDotNet dotnet;
3232
private readonly FileContent fileContent;
3333
private readonly TemporaryDirectory packageDirectory;
34+
private readonly TemporaryDirectory missingPackageDirectory;
3435
private readonly TemporaryDirectory tempWorkingDirectory;
3536
private readonly bool cleanupTempWorkingDirectory;
3637

@@ -51,6 +52,8 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
5152
this.sourceDir = new DirectoryInfo(srcDir);
5253

5354
packageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName));
55+
missingPackageDirectory = new TemporaryDirectory(ComputeTempDirectory(sourceDir.FullName, "missingpackages"));
56+
5457
tempWorkingDirectory = new TemporaryDirectory(FileUtils.GetTemporaryWorkingDirectory(out cleanupTempWorkingDirectory));
5558

5659
try
@@ -106,7 +109,7 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
106109

107110
// TODO: Rename the dllDirNames var - it's not only dirs anymore.
108111
dllDirNames.AddRange(paths);
109-
DownloadMissingPackages(allNonBinaryFiles);
112+
DownloadMissingPackages(allNonBinaryFiles, dllDirNames);
110113
}
111114

112115
// Find DLLs in the .Net / Asp.Net Framework
@@ -429,15 +432,15 @@ private IEnumerable<FileInfo> GetAllFiles()
429432
/// with this source tree. Use a SHA1 of the directory name.
430433
/// </summary>
431434
/// <returns>The full path of the temp directory.</returns>
432-
private static string ComputeTempDirectory(string srcDir)
435+
private static string ComputeTempDirectory(string srcDir, string packages = "packages")
433436
{
434437
var bytes = Encoding.Unicode.GetBytes(srcDir);
435438
var sha = SHA1.HashData(bytes);
436439
var sb = new StringBuilder();
437440
foreach (var b in sha.Take(8))
438441
sb.AppendFormat("{0:x2}", b);
439442

440-
return Path.Combine(Path.GetTempPath(), "GitHub", "packages", sb.ToString());
443+
return Path.Combine(Path.GetTempPath(), "GitHub", packages, sb.ToString());
441444
}
442445

443446
/// <summary>
@@ -660,7 +663,7 @@ private void RestoreProjects(IEnumerable<string> projects, out IEnumerable<strin
660663
assets = assetFiles;
661664
}
662665

663-
private void DownloadMissingPackages(List<FileInfo> allFiles)
666+
private void DownloadMissingPackages(List<FileInfo> allFiles, List<string> dllDirNames)
664667
{
665668
var nugetConfigs = allFiles.SelectFileNamesByName("nuget.config").ToArray();
666669
string? nugetConfig = null;
@@ -701,13 +704,15 @@ private void DownloadMissingPackages(List<FileInfo> allFiles)
701704
return;
702705
}
703706

704-
success = RestoreProject(tempDir.DirInfo.FullName, forceDotnetRefAssemblyFetching: false, out var _, pathToNugetConfig: nugetConfig);
707+
dotnet.RestoreProjectToDirectory(tempDir.DirInfo.FullName, missingPackageDirectory.DirInfo.FullName, forceDotnetRefAssemblyFetching: false, out var _, pathToNugetConfig: nugetConfig);
705708
// TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
706709
if (!success)
707710
{
708711
progressMonitor.FailedToRestoreNugetPackage(package);
709712
}
710713
});
714+
715+
dllDirNames.Add(missingPackageDirectory.DirInfo.FullName);
711716
}
712717

713718
private void AnalyseSolutions(IEnumerable<string> solutions)
@@ -732,6 +737,7 @@ public void Dispose()
732737
try
733738
{
734739
packageDirectory?.Dispose();
740+
missingPackageDirectory?.Dispose();
735741
}
736742
catch (Exception exc)
737743
{

0 commit comments

Comments
 (0)