Skip to content

Commit 3fe27d7

Browse files
authored
fix: Detected circular dependencies and add debug logs for nodeModulesCollector (#8864)
1 parent f4fc04a commit 3fe27d7

File tree

7 files changed

+59356
-310
lines changed

7 files changed

+59356
-310
lines changed

.changeset/swift-hotels-exercise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"app-builder-lib": patch
3+
---
4+
5+
Detected circular dependencies and add debug logs for nodeModulesCollector, and refactored YarnNodeModulesCollector to extend NpmNodeModulesCollector.

packages/app-builder-lib/src/node-module-collector/nodeModulesCollector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export abstract class NodeModulesCollector {
6666
result[parentKey] = { dependencies: [] }
6767
}
6868
result[parentKey].dependencies!.push(newKey)
69+
70+
if (node.__circularDependencyDetected) {
71+
continue
72+
}
6973
flatten(value, newKey)
7074
}
7175
}

packages/app-builder-lib/src/node-module-collector/npmNodeModulesCollector.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NodeModulesCollector } from "./nodeModulesCollector"
22
import { DependencyTree } from "./types"
3+
import { log } from "builder-util"
34

45
export class NpmNodeModulesCollector extends NodeModulesCollector {
56
constructor(rootDir: string) {
@@ -17,8 +18,11 @@ export class NpmNodeModulesCollector extends NodeModulesCollector {
1718
removeNonProductionDependencie(tree: DependencyTree) {
1819
const dependencies = tree.dependencies || {}
1920
const _dependencies = tree._dependencies || {}
20-
if (dependencies && Object.keys(dependencies).length === 0) {
21+
if (Object.keys(_dependencies).length > 0 && Object.keys(dependencies).length === 0) {
2122
tree.dependencies = this.allDependencies.get(`${tree.name}@${tree.version}`)?.dependencies || {}
23+
tree.__circularDependencyDetected = true
24+
log.debug({ name: tree.name, version: tree.version }, "circular dependency detected")
25+
return
2226
}
2327

2428
for (const [key, value] of Object.entries(dependencies)) {

packages/app-builder-lib/src/node-module-collector/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export interface NodeModuleInfo {
66
}
77

88
export interface DependencyTree {
9-
readonly version?: string
10-
readonly name?: string
11-
readonly from?: string
12-
readonly workspaces?: string[]
9+
readonly name: string
10+
readonly version: string
1311
readonly path: string
12+
readonly workspaces?: string[]
13+
__circularDependencyDetected?: boolean
1414
dependencies?: {
1515
[packageName: string]: DependencyTree
1616
}
Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,7 @@
1-
import { NodeModulesCollector } from "./nodeModulesCollector"
2-
import { DependencyTree } from "./types"
1+
import { NpmNodeModulesCollector } from "./npmNodeModulesCollector"
32

4-
export class YarnNodeModulesCollector extends NodeModulesCollector {
3+
export class YarnNodeModulesCollector extends NpmNodeModulesCollector {
54
constructor(rootDir: string) {
65
super(rootDir)
76
}
8-
9-
getCommand(): string {
10-
return process.platform === "win32" ? "npm.cmd" : "npm"
11-
}
12-
13-
getArgs(): string[] {
14-
return ["list", "-a", "--include", "prod", "--include", "optional", "--omit", "dev", "--json", "--long", "--silent"]
15-
}
16-
17-
removeNonProductionDependencie(tree: DependencyTree) {
18-
const dependencies = tree.dependencies || {}
19-
const _dependencies = tree._dependencies || {}
20-
if (dependencies && Object.keys(dependencies).length === 0) {
21-
tree.dependencies = this.allDependencies.get(`${tree.name}@${tree.version}`)?.dependencies || {}
22-
}
23-
24-
for (const [key, value] of Object.entries(dependencies)) {
25-
if (!_dependencies[key] || Object.keys(value).length === 0) {
26-
delete dependencies[key]
27-
continue
28-
}
29-
this.removeNonProductionDependencie(value)
30-
}
31-
}
327
}

0 commit comments

Comments
 (0)