Skip to content

Commit

Permalink
Fix Rust detector exception for non-source dependency (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
arlosi authored Aug 9, 2023
1 parent a0beddb commit f60574c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ComponentDetection.Detectors.Rust;
namespace Microsoft.ComponentDetection.Detectors.Rust;

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -213,12 +213,7 @@ private void ProcessDependency(

if (IsLocalPackage(childPackage))
{
if (!IsLocalPackage(parentPackage))
{
throw new FormatException($"In package with source '{parentComponent.Id}' found non-source dependency string: '{dependency}'");
}

// This is a dependency between packages without source
// This is a dependency on a package without a source
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Microsoft.ComponentDetection.Detectors.Tests;
namespace Microsoft.ComponentDetection.Detectors.Tests;

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -850,4 +850,41 @@ public async Task TestRustDetector_MultipleRegistriesAsync()

componentIds.ForEach(componentId => dependencyGraph.Contains(componentId).Should().BeTrue());
}

[TestMethod]
public async Task TestRustV2Detector_StdWorkspaceDependencyAsync()
{
var testCargoLock = @"
[[package]]
name = ""addr2line""
version = ""0.17.0""
source = ""registry+https://github.com/rust-lang/crates.io-index""
checksum = ""b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b""
dependencies = [
""rustc-std-workspace-alloc"",
]
[[package]]
name = ""rustc-std-workspace-alloc""
version = ""1.99.0""
dependencies = []
";

var (result, componentRecorder) = await this.DetectorTestUtility
.WithFile("Cargo.lock", testCargoLock)
.ExecuteDetectorAsync();

Assert.AreEqual(ProcessingResultCode.Success, result.ResultCode);
Assert.AreEqual(1, componentRecorder.GetDetectedComponents().Count());

var graph = componentRecorder.GetDependencyGraphsByLocation().Values.First(); // There should only be 1

// Verify explicitly referenced roots
var rootComponents = new List<string>
{
"addr2line 0.17.0 - Cargo",
};

rootComponents.ForEach(rootComponentId => graph.IsComponentExplicitlyReferenced(rootComponentId).Should().BeTrue());
}
}

0 comments on commit f60574c

Please sign in to comment.