Skip to content

Commit

Permalink
feat: add pruning to go mod projects at depgraph generation
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPatrickGill committed May 4, 2023
1 parent 3266baf commit f1a4ba3
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 36 deletions.
24 changes: 19 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down Expand Up @@ -597,10 +598,12 @@ function buildGraph(
currentParent: string,
childrenChain: Map<string, string[]>,
ancestorsChain: Map<string, string[]>,
visited?: Set<string>,
) {
const depPackagesLen = depPackages.length;

for (let i = depPackagesLen - 1; i >= 0; i--) {
visited = visited || new Set<string>();
const packageImport = depPackages[i];
let version = 'unknown';
if (isBuiltinPackage(packageImport)) {
Expand Down Expand Up @@ -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]);
Expand All @@ -651,6 +664,7 @@ function buildGraph(
packageImport,
childrenChain,
ancestorsChain,
visited,
);
}
}
Expand Down
186 changes: 155 additions & 31 deletions test/fixtures/gomod-small/expected-gomodules-depgraph.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
{
"schemaVersion": "1.2.0",
"pkgManager": { "name": "gomodules" },
"pkgManager": {
"name": "gomodules"
},
"pkgs": [
{
"id": "github.com/antonmedv/[email protected]",
"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/[email protected]",
"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/[email protected]",
"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/[email protected]",
Expand All @@ -31,15 +48,24 @@
},
{
"id": "github.com/gdamore/[email protected]",
"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/[email protected]",
"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/[email protected]",
"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/[email protected]",
Expand All @@ -57,7 +83,10 @@
},
{
"id": "github.com/gdamore/[email protected]",
"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",
Expand All @@ -68,7 +97,10 @@
},
{
"id": "golang.org/x/text/[email protected]",
"info": { "name": "golang.org/x/text/width", "version": "0.3.2" }
"info": {
"name": "golang.org/x/text/width",
"version": "0.3.2"
}
}
],
"graph": {
Expand All @@ -78,11 +110,21 @@
"nodeId": "root-node",
"pkgId": "github.com/antonmedv/[email protected]",
"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"
}
]
},
{
Expand All @@ -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"
}
]
},
{
Expand All @@ -119,12 +169,24 @@
"nodeId": "github.com/gdamore/tcell",
"pkgId": "github.com/gdamore/[email protected]",
"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"
}
]
},
{
Expand All @@ -136,15 +198,49 @@
"nodeId": "golang.org/x/text/encoding",
"pkgId": "golang.org/x/text/[email protected]",
"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/[email protected]",
"deps": [],
"info": {
"labels": {
"pruned": "true"
}
}
},
{
"nodeId": "golang.org/x/text/encoding/internal/identifier",
"pkgId": "golang.org/x/text/encoding/internal/[email protected]",
"deps": []
},
{
"nodeId": "github.com/mattn/go-runewidth:pruned",
"pkgId": "github.com/mattn/[email protected]",
"deps": [],
"info": {
"labels": {
"pruned": "true"
}
}
},
{
"nodeId": "github.com/lucasb-eyer/go-colorful:pruned",
"pkgId": "github.com/lucasb-eyer/[email protected]",
"deps": [],
"info": {
"labels": {
"pruned": "true"
}
}
},
{
"nodeId": "github.com/gdamore/tcell/terminfo",
"pkgId": "github.com/gdamore/tcell/[email protected]",
Expand All @@ -154,10 +250,34 @@
"nodeId": "github.com/gdamore/encoding",
"pkgId": "github.com/gdamore/[email protected]",
"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/[email protected]",
"deps": [],
"info": {
"labels": {
"pruned": "true"
}
}
},
{
"nodeId": "github.com/gdamore/tcell:pruned",
"pkgId": "github.com/gdamore/[email protected]",
"deps": [],
"info": {
"labels": {
"pruned": "true"
}
}
},
{
"nodeId": "github.com/antlr/antlr4/runtime/Go/antlr",
"pkgId": "github.com/antlr/antlr4/runtime/Go/antlr@#edae2a1c9b4b",
Expand All @@ -166,7 +286,11 @@
{
"nodeId": "golang.org/x/text/width",
"pkgId": "golang.org/x/text/[email protected]",
"deps": [{ "nodeId": "golang.org/x/text/transform" }]
"deps": [
{
"nodeId": "golang.org/x/text/transform:pruned"
}
]
}
]
}
Expand Down

0 comments on commit f1a4ba3

Please sign in to comment.