Skip to content

Commit

Permalink
Go CLI Replacement (#1052)
Browse files Browse the repository at this point in the history
* adding go cli replace strategy

* adding test for replace section

* revert gocomponent detector

* reverting

* adding replace module test

* adding tests and null checks

---------

Co-authored-by: Amitla Vannikumar <[email protected]>
  • Loading branch information
amitla1 and Amitla Vannikumar authored Apr 1, 2024
1 parent 90a1031 commit 2de3cc0
Show file tree
Hide file tree
Showing 2 changed files with 680 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public GoComponentWithReplaceDetector(
IObservableDirectoryWalkerFactory walkerFactory,
ICommandLineInvocationService commandLineInvocationService,
IEnvironmentVariableService envVarService,
ILogger<GoComponentDetector> logger)
ILogger<GoComponentWithReplaceDetector> logger)
{
this.ComponentStreamEnumerableFactory = componentStreamEnumerableFactory;
this.Scanner = walkerFactory;
Expand Down Expand Up @@ -313,57 +313,51 @@ private async Task ParseGoModFileAsync(
GoGraphTelemetryRecord goGraphTelemetryRecord)
{
using var reader = new StreamReader(file.Stream);
var inRequireBlock = false;
var inReplaceBlock = false;

// There can be multiple require( ) sections in go 1.17+. loop over all of them.
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync();

while (line != null && !line.StartsWith("require ("))
if (line != null && line.StartsWith("require ("))
{
if (line.StartsWith("go "))
{
goGraphTelemetryRecord.GoModVersion = line[3..].Trim();
}

// In go >= 1.17, direct dependencies are listed as "require x/y v1.2.3", and transitive dependencies
// are listed in the require () section
if (line.StartsWith("require "))
{
this.CreateListOfGoComponents(line[8..], singleFileComponentRecorder);
}

line = await reader.ReadLineAsync();
inRequireBlock = true;
continue;
}

// Stopping at the first ) restrict the detection to only the require section.
while ((line = await reader.ReadLineAsync()) != null && !line.EndsWith(")"))
else if (line != null && line.StartsWith("replace ("))
{
this.CreateListOfGoComponents(line, singleFileComponentRecorder);
inReplaceBlock = true;
continue;
}

while (line != null && !line.StartsWith("replace ("))
else if (line != null && line.StartsWith(")"))
{
if (line.StartsWith("go "))
{
goGraphTelemetryRecord.GoModVersion = line[3..].Trim();
}

// In go >= 1.17, direct dependencies are listed as "require x/y v1.2.3", and transitive dependencies
// are listed in the replace () section
if (line.StartsWith("replace "))
{
this.ReplaceGoComponents(line[8..], singleFileComponentRecorder);
}
inRequireBlock = false;
inReplaceBlock = false;
continue;
}

line = await reader.ReadLineAsync();
if (line != null && line.StartsWith("require "))
{
this.CreateListOfGoComponents(line[8..], singleFileComponentRecorder);
}
else if (line != null && line.StartsWith("replace "))
{
this.ReplaceGoComponents(line[8..], singleFileComponentRecorder);
}

// Stopping at the first ) restrict the detection to only the replace section.
while ((line = await reader.ReadLineAsync()) != null && !line.EndsWith(")"))
if (inRequireBlock)
{
this.CreateListOfGoComponents(line, singleFileComponentRecorder);
}
else if (inReplaceBlock)
{
this.ReplaceGoComponents(line, singleFileComponentRecorder);
}
else if (line != null && line.StartsWith("go "))
{
goGraphTelemetryRecord.GoModVersion = line[3..].Trim();
}
}

this.TryRegisterDependencyFromModLine(singleFileComponentRecorder);
Expand Down Expand Up @@ -516,7 +510,15 @@ private void RecordBuildDependencies(string goListOutput, ISingleFileComponentRe
continue;
}

var goComponent = new GoComponent(dependency.Path, dependency.Version);
GoComponent goComponent;
if (dependency.Replace != null)
{
goComponent = new GoComponent(dependency.Replace.Path, dependency.Replace.Version);
}
else
{
goComponent = new GoComponent(dependency.Path, dependency.Version);
}

if (dependency.Indirect)
{
Expand Down Expand Up @@ -556,5 +558,7 @@ private class GoBuildModule
public string Version { get; set; }

public bool Indirect { get; set; }

public GoBuildModule Replace { get; set; }
}
}
Loading

0 comments on commit 2de3cc0

Please sign in to comment.