diff --git a/src/Microsoft.ComponentDetection.Detectors/go/GoComponentDetector.cs b/src/Microsoft.ComponentDetection.Detectors/go/GoComponentDetector.cs index f1d4a100c..98e9da74b 100644 --- a/src/Microsoft.ComponentDetection.Detectors/go/GoComponentDetector.cs +++ b/src/Microsoft.ComponentDetection.Detectors/go/GoComponentDetector.cs @@ -1,4 +1,4 @@ -namespace Microsoft.ComponentDetection.Detectors.Go; +namespace Microsoft.ComponentDetection.Detectors.Go; using System; using System.Collections.Generic; @@ -257,6 +257,12 @@ private async Task UseGoCliToScanAsync(string location, ISingleFileCompone private void TryRegisterDependencyFromModLine(string line, ISingleFileComponentRecorder singleFileComponentRecorder) { + if (line.Trim().StartsWith("//")) + { + // this is a comment line, ignore it + return; + } + if (this.TryToCreateGoComponentFromModLine(line, out var goComponent)) { singleFileComponentRecorder.RegisterUsage(new DetectedComponent(goComponent)); diff --git a/test/Microsoft.ComponentDetection.Detectors.Tests/GoComponentDetectorTests.cs b/test/Microsoft.ComponentDetection.Detectors.Tests/GoComponentDetectorTests.cs index 9d96b793a..91525474f 100644 --- a/test/Microsoft.ComponentDetection.Detectors.Tests/GoComponentDetectorTests.cs +++ b/test/Microsoft.ComponentDetection.Detectors.Tests/GoComponentDetectorTests.cs @@ -60,6 +60,29 @@ public async Task TestGoModDetectorWithValidFile_ReturnsSuccessfullyAsync() discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Should().ContainSingle(); } + [TestMethod] + public async Task TestGoModDetector_CommentsOnFile_CommentsAreIgnoredAsync() + { + var goMod = + @"module github.com/Azure/azure-storage-blob-go + +require ( + // comment + github.com/kr/pretty v0.1.0 // indirect +)"; + var (scanResult, componentRecorder) = await this.DetectorTestUtility + .WithFile("go.mod", goMod) + .ExecuteDetectorAsync(); + + scanResult.ResultCode.Should().Be(ProcessingResultCode.Success); + + var detectedComponents = componentRecorder.GetDetectedComponents(); + detectedComponents.Should().ContainSingle("there is only one component definition on the file"); + + var discoveredComponents = detectedComponents.ToArray(); + discoveredComponents.Where(component => component.Component.Id == "github.com/kr/pretty v0.1.0 - Go").Should().ContainSingle(); + } + [TestMethod] public async Task TestGoSumDetectorWithValidFile_ReturnsSuccessfullyAsync() {