Skip to content

Commit 28bf476

Browse files
committed
debug
1 parent 7e3c8c4 commit 28bf476

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

src/shadow/npm-paths.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import Module from 'node:module'
22
import path from 'node:path'
33
import process from 'node:process'
44

5+
import { globSync } from 'tinyglobby'
6+
57
import constants from '../constants'
68
import { findBinPathDetailsSync, findNpmPathSync } from '../utils/path-resolve'
79

@@ -85,17 +87,32 @@ export function getNpmPath() {
8587
let _npmRequire: NodeJS.Require | undefined
8688
export function getNpmRequire(): NodeJS.Require {
8789
if (_npmRequire === undefined) {
88-
_npmRequire = Module.createRequire(path.join(getNpmPath(), '<dummy-basename>'))
90+
_npmRequire = Module.createRequire(
91+
path.join(getNpmPath(), '<dummy-basename>')
92+
)
8993
}
9094
return _npmRequire
9195
}
9296

9397
let _arboristPkgPath: string | undefined
9498
export function getArboristPackagePath() {
9599
if (_arboristPkgPath === undefined) {
96-
const pkgName = '@npmcli/arborist'
97-
const mainPath = getNpmRequire().resolve(pkgName)
98-
_arboristPkgPath = mainPath.slice(0, mainPath.indexOf(pkgName) + pkgName.length)
100+
try {
101+
const pkgName = '@npmcli/arborist'
102+
const mainPath = getNpmRequire().resolve(pkgName)
103+
_arboristPkgPath = mainPath.slice(
104+
0,
105+
mainPath.indexOf(pkgName) + pkgName.length
106+
)
107+
} catch {
108+
console.error(getNpmPath())
109+
console.error(
110+
JSON.stringify(
111+
globSync(['node_modules/**'], { cwd: getNpmPath() })
112+
, null, 2)
113+
)
114+
throw new Error()
115+
}
99116
}
100117
return _arboristPkgPath
101118
}

src/utils/path-resolve.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { existsSync, promises as fs, realpathSync, statSync } from 'node:fs'
1+
import { existsSync, promises as fs, realpathSync } from 'node:fs'
22
import path from 'node:path'
33
import process from 'node:process'
44

@@ -19,7 +19,7 @@ type GlobWithGitIgnoreOptions = GlobOptions & {
1919
socketConfig?: SocketYml | undefined
2020
}
2121

22-
const { NODE_MODULES, NPM, shadowBinPath } = constants
22+
const { NPM, shadowBinPath } = constants
2323

2424
async function filterGlobResultToSupportedFiles(
2525
entries: string[],
@@ -201,7 +201,6 @@ export function findBinPathDetailsSync(binName: string): {
201201
export function findNpmPathSync(npmBinPath: string): string | undefined {
202202
let thePath = npmBinPath
203203
while (true) {
204-
const nmPath = path.join(thePath, NODE_MODULES)
205204
if (
206205
// npm bin paths may look like:
207206
// /usr/local/share/npm/bin/npm
@@ -210,20 +209,11 @@ export function findNpmPathSync(npmBinPath: string): string | undefined {
210209
// OR
211210
// C:\Program Files\nodejs\npm.cmd
212211
//
213-
// In all cases the npm path contains a node_modules folder:
214-
// /usr/local/share/npm/bin/npm/node_modules
215-
// C:\Program Files\nodejs\node_modules
216-
//
217-
// Use existsSync here because statsSync, even with { throwIfNoEntry: false },
218-
// will throw an ENOTDIR error for paths like ./a-file-that-exists/a-directory-that-does-not.
219-
// See https://github.com/nodejs/node/issues/56993.
220-
existsSync(nmPath) &&
221-
statSync(nmPath, { throwIfNoEntry: false })?.isDirectory() &&
222212
// Optimistically look for the default location.
223-
(path.basename(thePath) === NPM ||
224-
// Chocolatey installs npm bins in the same directory as node bins.
225-
// Lazily access constants.WIN32.
226-
(constants.WIN32 && existsSync(path.join(thePath, `${NPM}.cmd`))))
213+
path.basename(thePath) === NPM ||
214+
// Chocolatey installs npm bins in the same directory as node bins.
215+
// Lazily access constants.WIN32.
216+
(constants.WIN32 && existsSync(path.join(thePath, `${NPM}.cmd`)))
227217
) {
228218
return thePath
229219
}

0 commit comments

Comments
 (0)