From f1a4ba3236511b503efed9888188da1228293a8e Mon Sep 17 00:00:00 2001 From: JamesPatrickGill Date: Wed, 3 May 2023 14:16:47 +0100 Subject: [PATCH] feat: add pruning to go mod projects at depgraph generation --- lib/index.ts | 24 ++- .../expected-gomodules-depgraph.json | 186 +++++++++++++++--- 2 files changed, 174 insertions(+), 36 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 79d296f..0a62aa5 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -153,12 +153,13 @@ async function getDependencies(root, targetFile, additionalArgs?: string[]) { let tempDirObj; const packageManager = pkgManagerByTarget(targetFile); if (packageManager === 'gomodules') { + const dependencyGraph = await buildDepGraphFromImportsAndModules( + root, + targetFile, + additionalArgs, + ); return { - dependencyGraph: await buildDepGraphFromImportsAndModules( - root, - targetFile, - additionalArgs, - ), + dependencyGraph, }; } @@ -597,10 +598,12 @@ function buildGraph( currentParent: string, childrenChain: Map, ancestorsChain: Map, + visited?: Set, ) { const depPackagesLen = depPackages.length; for (let i = depPackagesLen - 1; i >= 0; i--) { + visited = visited || new Set(); const packageImport = depPackages[i]; let version = 'unknown'; if (isBuiltinPackage(packageImport)) { @@ -636,8 +639,18 @@ function buildGraph( continue; } + if (visited.has(packageImport)) { + const prunedId = `${packageImport}:pruned`; + depGraphBuilder.addPkgNode(newNode, prunedId, { + labels: { pruned: 'true' }, + }); + depGraphBuilder.connectDep(currentParent, prunedId); + continue; + } + depGraphBuilder.addPkgNode(newNode, packageImport); depGraphBuilder.connectDep(currentParent, packageImport); + visited.add(packageImport); childrenChain.set(currentParent, [...currentChildren, packageImport]); ancestorsChain.set(packageImport, [...currentAncestors, currentParent]); @@ -651,6 +664,7 @@ function buildGraph( packageImport, childrenChain, ancestorsChain, + visited, ); } } diff --git a/test/fixtures/gomod-small/expected-gomodules-depgraph.json b/test/fixtures/gomod-small/expected-gomodules-depgraph.json index 76809b4..bf0eaad 100644 --- a/test/fixtures/gomod-small/expected-gomodules-depgraph.json +++ b/test/fixtures/gomod-small/expected-gomodules-depgraph.json @@ -1,26 +1,43 @@ { "schemaVersion": "1.2.0", - "pkgManager": { "name": "gomodules" }, + "pkgManager": { + "name": "gomodules" + }, "pkgs": [ { "id": "github.com/antonmedv/expr@0.0.0", - "info": { "name": "github.com/antonmedv/expr", "version": "0.0.0" } + "info": { + "name": "github.com/antonmedv/expr", + "version": "0.0.0" + } }, { "id": "github.com/sanity-io/litter@1.1.0", - "info": { "name": "github.com/sanity-io/litter", "version": "1.1.0" } + "info": { + "name": "github.com/sanity-io/litter", + "version": "1.1.0" + } }, { "id": "github.com/rivo/tview@#bd836ef13b4b", - "info": { "name": "github.com/rivo/tview", "version": "#bd836ef13b4b" } + "info": { + "name": "github.com/rivo/tview", + "version": "#bd836ef13b4b" + } }, { "id": "github.com/rivo/uniseg@#b9f5b9457d44", - "info": { "name": "github.com/rivo/uniseg", "version": "#b9f5b9457d44" } + "info": { + "name": "github.com/rivo/uniseg", + "version": "#b9f5b9457d44" + } }, { "id": "github.com/mattn/go-runewidth@0.0.4", - "info": { "name": "github.com/mattn/go-runewidth", "version": "0.0.4" } + "info": { + "name": "github.com/mattn/go-runewidth", + "version": "0.0.4" + } }, { "id": "github.com/lucasb-eyer/go-colorful@1.0.2", @@ -31,15 +48,24 @@ }, { "id": "github.com/gdamore/tcell@1.1.2", - "info": { "name": "github.com/gdamore/tcell", "version": "1.1.2" } + "info": { + "name": "github.com/gdamore/tcell", + "version": "1.1.2" + } }, { "id": "golang.org/x/text/transform@0.3.2", - "info": { "name": "golang.org/x/text/transform", "version": "0.3.2" } + "info": { + "name": "golang.org/x/text/transform", + "version": "0.3.2" + } }, { "id": "golang.org/x/text/encoding@0.3.2", - "info": { "name": "golang.org/x/text/encoding", "version": "0.3.2" } + "info": { + "name": "golang.org/x/text/encoding", + "version": "0.3.2" + } }, { "id": "golang.org/x/text/encoding/internal/identifier@0.3.2", @@ -57,7 +83,10 @@ }, { "id": "github.com/gdamore/encoding@1.0.0", - "info": { "name": "github.com/gdamore/encoding", "version": "1.0.0" } + "info": { + "name": "github.com/gdamore/encoding", + "version": "1.0.0" + } }, { "id": "github.com/antlr/antlr4/runtime/Go/antlr@#edae2a1c9b4b", @@ -68,7 +97,10 @@ }, { "id": "golang.org/x/text/width@0.3.2", - "info": { "name": "golang.org/x/text/width", "version": "0.3.2" } + "info": { + "name": "golang.org/x/text/width", + "version": "0.3.2" + } } ], "graph": { @@ -78,11 +110,21 @@ "nodeId": "root-node", "pkgId": "github.com/antonmedv/expr@0.0.0", "deps": [ - { "nodeId": "github.com/sanity-io/litter" }, - { "nodeId": "github.com/rivo/tview" }, - { "nodeId": "github.com/gdamore/tcell" }, - { "nodeId": "github.com/antlr/antlr4/runtime/Go/antlr" }, - { "nodeId": "golang.org/x/text/width" } + { + "nodeId": "github.com/sanity-io/litter" + }, + { + "nodeId": "github.com/rivo/tview" + }, + { + "nodeId": "github.com/gdamore/tcell:pruned" + }, + { + "nodeId": "github.com/antlr/antlr4/runtime/Go/antlr" + }, + { + "nodeId": "golang.org/x/text/width" + } ] }, { @@ -94,10 +136,18 @@ "nodeId": "github.com/rivo/tview", "pkgId": "github.com/rivo/tview@#bd836ef13b4b", "deps": [ - { "nodeId": "github.com/rivo/uniseg" }, - { "nodeId": "github.com/mattn/go-runewidth" }, - { "nodeId": "github.com/lucasb-eyer/go-colorful" }, - { "nodeId": "github.com/gdamore/tcell" } + { + "nodeId": "github.com/rivo/uniseg" + }, + { + "nodeId": "github.com/mattn/go-runewidth" + }, + { + "nodeId": "github.com/lucasb-eyer/go-colorful" + }, + { + "nodeId": "github.com/gdamore/tcell" + } ] }, { @@ -119,12 +169,24 @@ "nodeId": "github.com/gdamore/tcell", "pkgId": "github.com/gdamore/tcell@1.1.2", "deps": [ - { "nodeId": "golang.org/x/text/transform" }, - { "nodeId": "golang.org/x/text/encoding" }, - { "nodeId": "github.com/mattn/go-runewidth" }, - { "nodeId": "github.com/lucasb-eyer/go-colorful" }, - { "nodeId": "github.com/gdamore/tcell/terminfo" }, - { "nodeId": "github.com/gdamore/encoding" } + { + "nodeId": "golang.org/x/text/transform" + }, + { + "nodeId": "golang.org/x/text/encoding" + }, + { + "nodeId": "github.com/mattn/go-runewidth:pruned" + }, + { + "nodeId": "github.com/lucasb-eyer/go-colorful:pruned" + }, + { + "nodeId": "github.com/gdamore/tcell/terminfo" + }, + { + "nodeId": "github.com/gdamore/encoding" + } ] }, { @@ -136,15 +198,49 @@ "nodeId": "golang.org/x/text/encoding", "pkgId": "golang.org/x/text/encoding@0.3.2", "deps": [ - { "nodeId": "golang.org/x/text/transform" }, - { "nodeId": "golang.org/x/text/encoding/internal/identifier" } + { + "nodeId": "golang.org/x/text/transform:pruned" + }, + { + "nodeId": "golang.org/x/text/encoding/internal/identifier" + } ] }, + { + "nodeId": "golang.org/x/text/transform:pruned", + "pkgId": "golang.org/x/text/transform@0.3.2", + "deps": [], + "info": { + "labels": { + "pruned": "true" + } + } + }, { "nodeId": "golang.org/x/text/encoding/internal/identifier", "pkgId": "golang.org/x/text/encoding/internal/identifier@0.3.2", "deps": [] }, + { + "nodeId": "github.com/mattn/go-runewidth:pruned", + "pkgId": "github.com/mattn/go-runewidth@0.0.4", + "deps": [], + "info": { + "labels": { + "pruned": "true" + } + } + }, + { + "nodeId": "github.com/lucasb-eyer/go-colorful:pruned", + "pkgId": "github.com/lucasb-eyer/go-colorful@1.0.2", + "deps": [], + "info": { + "labels": { + "pruned": "true" + } + } + }, { "nodeId": "github.com/gdamore/tcell/terminfo", "pkgId": "github.com/gdamore/tcell/terminfo@1.1.2", @@ -154,10 +250,34 @@ "nodeId": "github.com/gdamore/encoding", "pkgId": "github.com/gdamore/encoding@1.0.0", "deps": [ - { "nodeId": "golang.org/x/text/transform" }, - { "nodeId": "golang.org/x/text/encoding" } + { + "nodeId": "golang.org/x/text/transform:pruned" + }, + { + "nodeId": "golang.org/x/text/encoding:pruned" + } ] }, + { + "nodeId": "golang.org/x/text/encoding:pruned", + "pkgId": "golang.org/x/text/encoding@0.3.2", + "deps": [], + "info": { + "labels": { + "pruned": "true" + } + } + }, + { + "nodeId": "github.com/gdamore/tcell:pruned", + "pkgId": "github.com/gdamore/tcell@1.1.2", + "deps": [], + "info": { + "labels": { + "pruned": "true" + } + } + }, { "nodeId": "github.com/antlr/antlr4/runtime/Go/antlr", "pkgId": "github.com/antlr/antlr4/runtime/Go/antlr@#edae2a1c9b4b", @@ -166,7 +286,11 @@ { "nodeId": "golang.org/x/text/width", "pkgId": "golang.org/x/text/width@0.3.2", - "deps": [{ "nodeId": "golang.org/x/text/transform" }] + "deps": [ + { + "nodeId": "golang.org/x/text/transform:pruned" + } + ] } ] }