diff --git a/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateDetector.cs b/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateDetector.cs index 6f9e31b8a..b72cdde01 100644 --- a/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateDetector.cs +++ b/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateDetector.cs @@ -21,7 +21,7 @@ public class RustCrateDetector : FileComponentDetector public override IEnumerable SupportedComponentTypes => new[] { ComponentType.Cargo }; - public override int Version { get; } = 5; + public override int Version { get; } = 6; public override IEnumerable Categories => new List { "Rust" }; diff --git a/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateUtilities.cs b/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateUtilities.cs index 68742d7f7..c6396ff64 100644 --- a/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateUtilities.cs +++ b/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateUtilities.cs @@ -321,7 +321,20 @@ public static DependencySpecification GenerateDependencySpecifications(TomlTable return null; } - dependencySpecifications.Add(dependency, versionSpecifier); + // If the dependency is renamed, use the actual name of the package: + // https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml + string dependencyName; + if (dependencies[dependency].TomlType == TomlObjectType.Table && + dependencies.Get(dependency).ContainsKey("package")) + { + dependencyName = dependencies.Get(dependency).Get("package"); + } + else + { + dependencyName = dependency; + } + + dependencySpecifications.Add(dependencyName, versionSpecifier); } } } diff --git a/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateV2Detector.cs b/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateV2Detector.cs index 3fa2d34d1..6a817d04a 100644 --- a/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateV2Detector.cs +++ b/src/Microsoft.ComponentDetection.Detectors/rust/RustCrateV2Detector.cs @@ -21,7 +21,7 @@ public class RustCrateV2Detector : FileComponentDetector public override IEnumerable SupportedComponentTypes => new[] { ComponentType.Cargo }; - public override int Version { get; } = 4; + public override int Version { get; } = 5; public override IEnumerable Categories => new List { "Rust" }; diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/RustCrateDetectorTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/RustCrateDetectorTests.cs index 832176d8f..252bca2d2 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/RustCrateDetectorTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/RustCrateDetectorTests.cs @@ -787,6 +787,45 @@ public async Task TestRustDetector_UnequalButSemverCompatibleRoot() graph.GetDependenciesForComponent("c-ares 7.5.2 - Cargo").Should().BeEquivalentTo(cAresDependencies); } + [TestMethod] + public async Task TestRustDetector_RenamedDependency() + { + var testTomlString = @" +[package] +name = ""my_test_package"" +version = ""1.2.3"" +authors = [""example@example.com>""] + +[dependencies] +foo_dependency = { package = ""my_dependency"", version = ""1.0.0""} +"; + var testLockString = @" +[[package]] +name = ""my_dependency"" +version = ""1.0.0"" +source = ""registry+https://github.com/rust-lang/crates.io-index"" +"; + var (result, componentRecorder) = await detectorV2TestUtility + .WithFile("Cargo.lock", testLockString) + .WithFile("Cargo.toml", testTomlString, new List { "Cargo.toml" }) + .ExecuteDetector(); + + result.ResultCode.Should().Be(ProcessingResultCode.Success); + + var dependencyGraphs = componentRecorder.GetDependencyGraphsByLocation(); + dependencyGraphs.Count.Should().Be(1); + + var dependencyGraph = dependencyGraphs.Single().Value; + var foundComponents = dependencyGraph.GetComponents(); + foundComponents.Count().Should().Be(1); + + var rootComponents = new List + { + "my_dependency 1.0.0 - Cargo", + }; + rootComponents.ForEach(rootComponentId => dependencyGraph.IsComponentExplicitlyReferenced(rootComponentId).Should().BeTrue()); + } + /// /// (my_dependency, 1.0, root) /// (my_other_dependency, 0.1.0, root) diff --git a/test/Microsoft.ComponentDetection.VerificationTests/resources/rust/standard/Cargo.toml b/test/Microsoft.ComponentDetection.VerificationTests/resources/rust/standard/Cargo.toml index a6a83bd37..a7088109f 100644 --- a/test/Microsoft.ComponentDetection.VerificationTests/resources/rust/standard/Cargo.toml +++ b/test/Microsoft.ComponentDetection.VerificationTests/resources/rust/standard/Cargo.toml @@ -10,10 +10,10 @@ path = "main.rs" name = "component-detection" [dependencies] -serde = "1.0.136" +renamed_dependency = { package = "serde", version = "1.0.136"} [dev-dependencies] rand = "0.8.4" [build-dependencies] -autocfg = "1.0.1" \ No newline at end of file +autocfg = "1.0.1"