Skip to content

Commit

Permalink
Fix Go CLI cyclic dependency graph (#103)
Browse files Browse the repository at this point in the history
* Fix Go CLI cyclic dependency graph
  • Loading branch information
grvillic authored Apr 13, 2022
1 parent 6c26b9d commit 164770f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public class GoComponentDetector : FileComponentDetector

public override IEnumerable<ComponentType> SupportedComponentTypes { get; } = new[] { ComponentType.Go };

public override int Version => 3;
public override int Version => 4;

private HashSet<string> projectRoots = new HashSet<string>();

protected override async Task OnFileFound(ProcessRequest processRequest, IDictionary<string, string> detectorArgs)
{
var singleFileComponentRecorder = processRequest.SingleFileComponentRecorder;
var file = processRequest.ComponentStream;

var projectRootDirectory = Directory.GetParent(file.Location);
if (projectRoots.Any(path => projectRootDirectory.FullName.StartsWith(path)))
{
Expand All @@ -57,9 +57,10 @@ protected override async Task OnFileFound(ProcessRequest processRequest, IDictio
wasGoCliScanSuccessful = await UseGoCliToScan(file.Location, singleFileComponentRecorder);
}
}
catch
catch (Exception ex)
{
Logger.LogInfo("Failed to detect components using go cli.");
Logger.LogError($"Failed to detect components using go cli. Location: {file.Location}");
Logger.LogException(ex, isError: true, printException: true);
}
finally
{
Expand Down Expand Up @@ -231,7 +232,12 @@ private void PopulateDependencyGraph(string goGraphOutput, ISingleFileComponentR
}
else if (isParentParsed && isChildParsed)
{
// Go output guarantees that all parents will be output before children
// Go can have a cyclic dependency between modules, which could cause child components to be listed first than parents. Reproducible with Go 1.16
if (singleFileComponentRecorder.GetComponent(parentComponent.Id) == null)
{
singleFileComponentRecorder.RegisterUsage(new DetectedComponent(parentComponent));
}

singleFileComponentRecorder.RegisterUsage(new DetectedComponent(childComponent), parentComponentId: parentComponent.Id);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,34 @@ public async Task TestGoDetector_GoGraphHappyPath()
Assert.AreEqual(4, detectedComponents.Count());
}

[TestMethod]
public async Task TestGoDetector_GoGraphCyclicDependencies()
{
var goGraph = @"
github.com/prometheus/[email protected] github.com/prometheus/[email protected]
github.com/prometheus/[email protected] github.com/prometheus/[email protected]";
commandLineMock.Setup(x => x.CanCommandBeLocated("go", null, It.IsAny<DirectoryInfo>(), It.IsAny<string[]>()))
.ReturnsAsync(true);

commandLineMock.Setup(x => x.ExecuteCommand("go", null, It.IsAny<DirectoryInfo>(), new[] { "mod", "graph" }))
.ReturnsAsync(new CommandLineExecutionResult
{
ExitCode = 0,
StdOut = goGraph,
});

envVarService.Setup(x => x.DoesEnvironmentVariableExist("EnableGoCliScan")).Returns(true);

var (scanResult, componentRecorder) = await detectorTestUtility
.WithFile("go.mod", string.Empty)
.ExecuteDetector();

Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);

var detectedComponents = componentRecorder.GetDetectedComponents();
Assert.AreEqual(3, detectedComponents.Count());
}

[TestMethod]
public async Task TestGoDetector_GoCliRequiresEnvVarToRun()
{
Expand Down

0 comments on commit 164770f

Please sign in to comment.