Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
disable ws with workspaceEnabled=false and add includeWorkspaceRoot
Browse files Browse the repository at this point in the history
PR-URL: #327
Credit: @fritzy
Close: #327
Reviewed-by: @isaacs
  • Loading branch information
fritzy authored and isaacs committed Sep 30, 2021
1 parent bb4b906 commit 6bda3e3
Show file tree
Hide file tree
Showing 14 changed files with 881 additions and 22 deletions.
11 changes: 10 additions & 1 deletion lib/arborist/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const AuditReport = require('../audit-report.js')
// shared with reify
const _global = Symbol.for('global')
const _workspaces = Symbol.for('workspaces')
const _includeWorkspaceRoot = Symbol.for('includeWorkspaceRoot')

module.exports = cls => class Auditor extends cls {
async audit (options = {}) {
Expand All @@ -23,7 +24,15 @@ module.exports = cls => class Auditor extends cls {
process.emit('time', 'audit')
const tree = await this.loadVirtual()
if (this[_workspaces] && this[_workspaces].length) {
options.filterSet = this.workspaceDependencySet(tree, this[_workspaces])
options.filterSet = this.workspaceDependencySet(
tree,
this[_workspaces],
this[_includeWorkspaceRoot]
)
}
if (!options.workspacesEnabled) {
options.filterSet =
this.excludeWorkspacesDependencySet(tree)
}
this.auditReport = await AuditReport.load(tree, options)
const ret = options.fix ? this.reify(options) : this.auditReport
Expand Down
20 changes: 15 additions & 5 deletions lib/arborist/build-ideal-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const _checkEngine = Symbol('checkEngine')
const _checkPlatform = Symbol('checkPlatform')
const _virtualRoots = Symbol('virtualRoots')
const _virtualRoot = Symbol('virtualRoot')
const _includeWorkspaceRoot = Symbol.for('includeWorkspaceRoot')

const _failPeerConflict = Symbol('failPeerConflict')
const _explainPeerConflict = Symbol('explainPeerConflict')
Expand All @@ -115,12 +116,13 @@ module.exports = cls => class IdealTreeBuilder extends cls {
options.registry = this.registry = registry.replace(/\/+$/, '') + '/'

const {
idealTree = null,
global = false,
follow = false,
force = false,
global = false,
globalStyle = false,
idealTree = null,
includeWorkspaceRoot = false,
legacyPeerDeps = false,
force = false,
packageLock = true,
strictPeerDeps = false,
workspaces = [],
Expand Down Expand Up @@ -162,6 +164,8 @@ module.exports = cls => class IdealTreeBuilder extends cls {
// don't hold onto references for nodes that are garbage collected.
this[_peerSetSource] = new WeakMap()
this[_virtualRoots] = new Map()

this[_includeWorkspaceRoot] = includeWorkspaceRoot
}

get explicitRequests () {
Expand Down Expand Up @@ -394,8 +398,14 @@ module.exports = cls => class IdealTreeBuilder extends cls {
if (!this[_workspaces].length) {
await this[_applyUserRequestsToNode](tree, options)
} else {
await Promise.all(this.workspaceNodes(tree, this[_workspaces])
.map(node => this[_applyUserRequestsToNode](node, options)))
const nodes = this.workspaceNodes(tree, this[_workspaces])
if (this[_includeWorkspaceRoot]) {
nodes.push(tree)
}
const appliedRequests = nodes.map(
node => this[_applyUserRequestsToNode](node, options)
)
await Promise.all(appliedRequests)
}

process.emit('timeEnd', 'idealTree:userRequests')
Expand Down
29 changes: 28 additions & 1 deletion lib/arborist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Arborist extends Base {
cache: options.cache || `${homedir()}/.npm/_cacache`,
packumentCache: options.packumentCache || new Map(),
log: options.log || procLog,
workspacesEnabled: options.workspacesEnabled !== false,
}
if (options.saveType && !saveTypeMap.get(options.saveType)) {
throw new Error(`Invalid saveType ${options.saveType}`)
Expand All @@ -73,8 +74,15 @@ class Arborist extends Base {
}

// returns a set of workspace nodes and all their deps
workspaceDependencySet (tree, workspaces) {
workspaceDependencySet (tree, workspaces, includeWorkspaceRoot) {
const wsNodes = this.workspaceNodes(tree, workspaces)
if (includeWorkspaceRoot) {
for (const edge of tree.edgesOut.values()) {
if (edge.type !== 'workspace' && edge.to) {
wsNodes.push(edge.to)
}
}
}
const set = new Set(wsNodes)
const extraneous = new Set()
for (const node of set) {
Expand All @@ -96,6 +104,25 @@ class Arborist extends Base {
for (const extra of extraneous) {
set.add(extra)
}

return set
}

excludeWorkspacesDependencySet (tree) {
const set = new Set()
for (const edge of tree.edgesOut.values()) {
if (edge.type !== 'workspace' && edge.to) {
set.add(edge.to)
}
}
for (const node of set) {
for (const edge of node.edgesOut.values()) {
if (edge.to) {
set.add(edge.to)
}
}
}

return set
}
}
Expand Down
7 changes: 6 additions & 1 deletion lib/arborist/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const _addToBuildSet = Symbol('addToBuildSet')
const _checkBins = Symbol.for('checkBins')
const _queues = Symbol('queues')
const _scriptShell = Symbol('scriptShell')
const _includeWorkspaceRoot = Symbol.for('includeWorkspaceRoot')

const _force = Symbol.for('force')

Expand Down Expand Up @@ -77,7 +78,11 @@ module.exports = cls => class Builder extends cls {
if (!nodes) {
const tree = await this.loadActual()
if (this[_workspaces] && this[_workspaces].length) {
const filterSet = this.workspaceDependencySet(tree, this[_workspaces])
const filterSet = this.workspaceDependencySet(
tree,
this[_workspaces],
this[_includeWorkspaceRoot]
)
nodes = tree.inventory.filter(node => filterSet.has(node))
} else {
nodes = tree.inventory.values()
Expand Down
16 changes: 15 additions & 1 deletion lib/arborist/reify.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const _validateNodeModules = Symbol('validateNodeModules')
const _nmValidated = Symbol('nmValidated')
const _validatePath = Symbol('validatePath')
const _reifyPackages = Symbol.for('reifyPackages')
const _includeWorkspaceRoot = Symbol.for('includeWorkspaceRoot')

const _omitDev = Symbol('omitDev')
const _omitOptional = Symbol('omitOptional')
Expand Down Expand Up @@ -340,6 +341,15 @@ module.exports = cls => class Reifier extends cls {
filterNodes.push(actual)
}
}
if (this[_includeWorkspaceRoot] && (this[_workspaces].length > 0)) {
for (const tree of [this.idealTree, this.actualTree]) {
for (const {type, to} of tree.edgesOut.values()) {
if (type !== 'workspace' && to) {
filterNodes.push(to)
}
}
}
}
}

// find all the nodes that need to change between the actual
Expand Down Expand Up @@ -901,7 +911,11 @@ module.exports = cls => class Reifier extends cls {

// if we're operating on a workspace, only audit the workspace deps
if (this[_workspaces] && this[_workspaces].length) {
options.filterSet = this.workspaceDependencySet(tree, this[_workspaces])
options.filterSet = this.workspaceDependencySet(
tree,
this[_workspaces],
this[_includeWorkspaceRoot]
)
}

this.auditReport = AuditReport.load(tree, options)
Expand Down
Loading

0 comments on commit 6bda3e3

Please sign in to comment.