Skip to content

Commit 9773652

Browse files
committed
Merge branch 'main' into idrissrio/preprocessor-multiline
2 parents 13f4f48 + 0f1aee0 commit 9773652

File tree

31 files changed

+530
-242
lines changed

31 files changed

+530
-242
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public HashSet<AssemblyLookupLocation> Restore()
109109
if (checkNugetFeedResponsiveness && !CheckFeeds(out explicitFeeds))
110110
{
111111
// todo: we could also check the reachability of the inherited nuget feeds, but to use those in the fallback we would need to handle authentication too.
112-
var unresponsiveMissingPackageLocation = DownloadMissingPackagesFromSpecificFeeds(explicitFeeds);
112+
var unresponsiveMissingPackageLocation = DownloadMissingPackagesFromSpecificFeeds([], explicitFeeds);
113113
return unresponsiveMissingPackageLocation is null
114114
? []
115115
: [unresponsiveMissingPackageLocation];
@@ -166,11 +166,11 @@ public HashSet<AssemblyLookupLocation> Restore()
166166
.ToList();
167167
assemblyLookupLocations.UnionWith(paths.Select(p => new AssemblyLookupLocation(p)));
168168

169-
LogAllUnusedPackages(dependencies);
169+
var usedPackageNames = GetAllUsedPackageDirNames(dependencies);
170170

171171
var missingPackageLocation = checkNugetFeedResponsiveness
172-
? DownloadMissingPackagesFromSpecificFeeds(explicitFeeds)
173-
: DownloadMissingPackages();
172+
? DownloadMissingPackagesFromSpecificFeeds(usedPackageNames, explicitFeeds)
173+
: DownloadMissingPackages(usedPackageNames);
174174

175175
if (missingPackageLocation is not null)
176176
{
@@ -297,21 +297,21 @@ private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<Dep
297297
compilationInfoContainer.CompilationInfos.Add(("Failed project restore with package source error", nugetSourceFailures.ToString()));
298298
}
299299

300-
private AssemblyLookupLocation? DownloadMissingPackagesFromSpecificFeeds(HashSet<string>? feedsFromNugetConfigs)
300+
private AssemblyLookupLocation? DownloadMissingPackagesFromSpecificFeeds(IEnumerable<string> usedPackageNames, HashSet<string>? feedsFromNugetConfigs)
301301
{
302302
var reachableFallbackFeeds = GetReachableFallbackNugetFeeds(feedsFromNugetConfigs);
303303
if (reachableFallbackFeeds.Count > 0)
304304
{
305-
return DownloadMissingPackages(fallbackNugetFeeds: reachableFallbackFeeds);
305+
return DownloadMissingPackages(usedPackageNames, fallbackNugetFeeds: reachableFallbackFeeds);
306306
}
307307

308308
logger.LogWarning("Skipping download of missing packages from specific feeds as no fallback Nuget feeds are reachable.");
309309
return null;
310310
}
311311

312-
private AssemblyLookupLocation? DownloadMissingPackages(IEnumerable<string>? fallbackNugetFeeds = null)
312+
private AssemblyLookupLocation? DownloadMissingPackages(IEnumerable<string> usedPackageNames, IEnumerable<string>? fallbackNugetFeeds = null)
313313
{
314-
var alreadyDownloadedPackages = GetRestoredPackageDirectoryNames(PackageDirectory.DirInfo);
314+
var alreadyDownloadedPackages = usedPackageNames.Select(p => p.ToLowerInvariant());
315315
var alreadyDownloadedLegacyPackages = GetRestoredLegacyPackageNames();
316316

317317
var notYetDownloadedPackages = new HashSet<PackageReference>(fileContent.AllPackages);
@@ -418,17 +418,23 @@ private void RestoreProjects(IEnumerable<string> projects, out ConcurrentBag<Dep
418418
return nugetConfig;
419419
}
420420

421-
private void LogAllUnusedPackages(DependencyContainer dependencies)
421+
private IEnumerable<string> GetAllUsedPackageDirNames(DependencyContainer dependencies)
422422
{
423423
var allPackageDirectories = GetAllPackageDirectories();
424424

425425
logger.LogInfo($"Restored {allPackageDirectories.Count} packages");
426426
logger.LogInfo($"Found {dependencies.Packages.Count} packages in project.assets.json files");
427427

428-
allPackageDirectories
429-
.Where(package => !dependencies.Packages.Contains(package))
428+
var usage = allPackageDirectories.Select(package => (package, isUsed: dependencies.Packages.Contains(package)));
429+
430+
usage
431+
.Where(package => !package.isUsed)
430432
.Order()
431-
.ForEach(package => logger.LogDebug($"Unused package: {package}"));
433+
.ForEach(package => logger.LogDebug($"Unused package: {package.package}"));
434+
435+
return usage
436+
.Where(package => package.isUsed)
437+
.Select(package => package.package);
432438
}
433439

434440
private ICollection<string> GetAllPackageDirectories()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Improved dependency resolution in `build-mode: none` extraction to handle failing `dotnet restore` processes that managed to download a subset of the dependencies before the failure.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use lib::a_module::hello;
1+
use lib::a_module::hello; // $ item=HELLO
22

33
mod a_module;
44

55
fn main() {
6-
hello();
6+
hello(); // $ item=HELLO
77
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
pub fn hello() {
22
println!("Hello, world!");
3-
}
3+
} // HELLO

rust/ql/integration-tests/hello-workspace/path-resolution.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import utils.test.PathResolutionInlineExpectationsTest

rust/ql/integration-tests/hello-workspace/rust-project.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
"sysroot_src": "filled by the rust_project fixture",
33
"crates": [
44
{
5+
"display_name": "exe",
6+
"version": "0.1.0",
57
"root_module": "exe/src/main.rs",
68
"edition": "2021",
7-
"deps": [{"crate": 1, "name": "lib"}]
9+
"deps": [
10+
{
11+
"crate": 1,
12+
"name": "lib"
13+
}
14+
]
815
},
916
{
17+
"display_name": "lib",
18+
"version": "0.1.0",
1019
"root_module": "lib/src/lib.rs",
1120
"edition": "2021",
1221
"deps": []
1322
}
1423
]
15-
}
24+
}

rust/ql/integration-tests/hello-workspace/summary.cargo.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| Elements extracted | 87 |
1+
| Elements extracted | 90 |
22
| Elements unextracted | 0 |
33
| Extraction errors | 0 |
44
| Extraction warnings | 0 |

rust/ql/integration-tests/hello-workspace/summary.rust-project.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| Elements extracted | 87 |
1+
| Elements extracted | 90 |
22
| Elements unextracted | 0 |
33
| Extraction errors | 0 |
44
| Extraction warnings | 0 |

rust/ql/lib/codeql/rust/elements/internal/CrateImpl.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ private import codeql.rust.elements.internal.generated.Crate
1313
module Impl {
1414
private import rust
1515
private import codeql.rust.elements.internal.NamedCrate
16+
private import codeql.rust.internal.PathResolution
1617

1718
class Crate extends Generated::Crate {
1819
override string toStringImpl() {
@@ -58,6 +59,14 @@ module Impl {
5859
*/
5960
Crate getADependency() { result = this.getDependency(_) }
6061

62+
/** Gets the source file that defines this crate, if any. */
63+
SourceFile getSourceFile() { result.getFile() = this.getModule().getFile() }
64+
65+
/**
66+
* Gets a source file that belongs to this crate, if any.
67+
*/
68+
SourceFile getASourceFile() { result = this.(CrateItemNode).getASourceFile() }
69+
6170
override Location getLocation() { result = this.getModule().getLocation() }
6271
}
6372
}

0 commit comments

Comments
 (0)