Skip to content

Commit 0ab5ebf

Browse files
committed
Pass CancellationToken to ParseAsync
1 parent e2e6ff6 commit 0ab5ebf

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

src/Microsoft.ComponentDetection.Detectors/go/Parsers/GoCLIParser.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ namespace Microsoft.ComponentDetection.Detectors.Go;
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Diagnostics.CodeAnalysis;
76
using System.IO;
87
using System.Text;
98
using System.Text.Json;
9+
using System.Threading;
1010
using System.Threading.Tasks;
1111
using Microsoft.ComponentDetection.Common.Telemetry.Records;
1212
using Microsoft.ComponentDetection.Contracts;
@@ -26,11 +26,11 @@ public GoCLIParser(ILogger logger, IFileUtilityService fileUtilityService, IComm
2626
this.commandLineInvocationService = commandLineInvocationService;
2727
}
2828

29-
[SuppressMessage("Maintainability", "CA1508:Avoid dead conditional code", Justification = "False positive")]
3029
public async Task<bool> ParseAsync(
3130
ISingleFileComponentRecorder singleFileComponentRecorder,
3231
IComponentStream file,
33-
GoGraphTelemetryRecord record)
32+
GoGraphTelemetryRecord record,
33+
CancellationToken cancellationToken = default)
3434
{
3535
record.WasGraphSuccessful = false;
3636
record.DidGoCliCommandFail = false;
@@ -50,7 +50,7 @@ public async Task<bool> ParseAsync(
5050
"Detection time may be improved by activating fallback strategy (https://github.com/microsoft/component-detection/blob/main/docs/detectors/go.md#fallback-detection-strategy). " +
5151
"But, it will introduce noise into the detected components.");
5252

53-
var goDependenciesProcess = await this.commandLineInvocationService.ExecuteCommandAsync("go", null, workingDirectory: projectRootDirectory, ["list", "-mod=readonly", "-m", "-json", "all"]);
53+
var goDependenciesProcess = await this.commandLineInvocationService.ExecuteCommandAsync("go", null, projectRootDirectory, cancellationToken, "list", "-mod=readonly", "-m", "-json", "all");
5454
if (goDependenciesProcess.ExitCode != 0)
5555
{
5656
this.logger.LogError("Go CLI command \"go list -m -json all\" failed with error: {GoDependenciesProcessStdErr}", goDependenciesProcess.StdErr);
@@ -67,7 +67,8 @@ public async Task<bool> ParseAsync(
6767
this.logger,
6868
singleFileComponentRecorder,
6969
projectRootDirectory.FullName,
70-
record);
70+
record,
71+
cancellationToken);
7172

7273
return true;
7374
}

src/Microsoft.ComponentDetection.Detectors/go/Parsers/GoModParser.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace Microsoft.ComponentDetection.Detectors.Go;
55
using System.Collections.Generic;
66
using System.IO;
77
using System.Text.RegularExpressions;
8+
using System.Threading;
89
using System.Threading.Tasks;
910
using Microsoft.ComponentDetection.Common.Telemetry.Records;
1011
using Microsoft.ComponentDetection.Contracts;
@@ -71,7 +72,8 @@ private static void HandleReplaceDirective(
7172
public async Task<bool> ParseAsync(
7273
ISingleFileComponentRecorder singleFileComponentRecorder,
7374
IComponentStream file,
74-
GoGraphTelemetryRecord record)
75+
GoGraphTelemetryRecord record,
76+
CancellationToken cancellationToken = default)
7577
{
7678
// Collect replace directives
7779
var (replacePathDirectives, moduleReplacements) = await this.GetAllReplaceDirectivesAsync(file);
@@ -84,7 +86,7 @@ public async Task<bool> ParseAsync(
8486
// There can be multiple require( ) sections in go 1.17+. loop over all of them.
8587
while (!reader.EndOfStream)
8688
{
87-
var line = await reader.ReadLineAsync();
89+
var line = await reader.ReadLineAsync(cancellationToken);
8890

8991
while (line != null && !line.StartsWith("require ("))
9092
{
@@ -100,11 +102,11 @@ public async Task<bool> ParseAsync(
100102
this.TryRegisterDependencyFromModLine(file, line[StartString.Length..], singleFileComponentRecorder, replacePathDirectives, moduleReplacements);
101103
}
102104

103-
line = await reader.ReadLineAsync();
105+
line = await reader.ReadLineAsync(cancellationToken);
104106
}
105107

106108
// Stopping at the first ) restrict the detection to only the require section.
107-
while ((line = await reader.ReadLineAsync()) != null && !line.EndsWith(')'))
109+
while ((line = await reader.ReadLineAsync(cancellationToken)) != null && !line.EndsWith(')'))
108110
{
109111
this.TryRegisterDependencyFromModLine(file, line, singleFileComponentRecorder, replacePathDirectives, moduleReplacements);
110112
}

src/Microsoft.ComponentDetection.Detectors/go/Parsers/GoSumParser.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace Microsoft.ComponentDetection.Detectors.Go;
33

44
using System.IO;
55
using System.Text.RegularExpressions;
6+
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.ComponentDetection.Common.Telemetry.Records;
89
using Microsoft.ComponentDetection.Contracts;
@@ -24,12 +25,13 @@ public class GoSumParser : IGoParser
2425
public async Task<bool> ParseAsync(
2526
ISingleFileComponentRecorder singleFileComponentRecorder,
2627
IComponentStream file,
27-
GoGraphTelemetryRecord record)
28+
GoGraphTelemetryRecord record,
29+
CancellationToken cancellationToken = default)
2830
{
2931
using var reader = new StreamReader(file.Stream);
3032

3133
string line;
32-
while ((line = await reader.ReadLineAsync()) != null)
34+
while ((line = await reader.ReadLineAsync(cancellationToken)) != null)
3335
{
3436
if (this.TryToCreateGoComponentFromSumLine(line, out var goComponent))
3537
{
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#nullable disable
22
namespace Microsoft.ComponentDetection.Detectors.Go;
33

4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Microsoft.ComponentDetection.Common.Telemetry.Records;
67
using Microsoft.ComponentDetection.Contracts;
78

89
public interface IGoParser
910
{
10-
Task<bool> ParseAsync(ISingleFileComponentRecorder singleFileComponentRecorder, IComponentStream file, GoGraphTelemetryRecord record);
11+
Task<bool> ParseAsync(ISingleFileComponentRecorder singleFileComponentRecorder, IComponentStream file, GoGraphTelemetryRecord record, CancellationToken cancellationToken = default);
1112
}

0 commit comments

Comments
 (0)