Skip to content

Commit c99b8b5

Browse files
committed
fix(config): add flatOptions.npxCache
This adds a new `npxCache` flatOption for libnpmexec to look for to put its `_npx` content. libnpmexec will have to be patched to use that value, and continue to pass `flatOptions.cache` to pacote et al. The `flatOptions.cache` is the one that is intended to be passed down into other modules, as it has the `_cacache` suffix attached. What was happening before was that `npx` was creating a new alternate cache one directory up from where everything else was, and also putting the `_npx` content there. It is possible this is the source of at least some of our "npx doesn't find the right versions" bugs. PR-URL: #3430 Credit: @wraithgar Close: #3430 Reviewed-by: @ruyadorno
1 parent b19e56c commit c99b8b5

File tree

6 files changed

+17
-7
lines changed

6 files changed

+17
-7
lines changed

lib/exec.js

-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ class Exec extends BaseCommand {
6767
// can be named correctly
6868
async _exec (_args, { locationMsg, path, runPath }) {
6969
const args = [..._args]
70-
const cache = this.npm.config.get('cache')
7170
const call = this.npm.config.get('call')
7271
const color = this.npm.config.get('color')
7372
const {
@@ -88,7 +87,6 @@ class Exec extends BaseCommand {
8887
...flatOptions,
8988
args,
9089
call,
91-
cache,
9290
color,
9391
localBin,
9492
locationMsg,

lib/init.js

-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ class Init extends BaseCommand {
106106
}
107107

108108
const newArgs = [packageName, ...otherArgs]
109-
const cache = this.npm.config.get('cache')
110109
const { color } = this.npm.flatOptions
111110
const {
112111
flatOptions,
@@ -128,7 +127,6 @@ class Init extends BaseCommand {
128127
await libexec({
129128
...flatOptions,
130129
args: newArgs,
131-
cache,
132130
color,
133131
localBin,
134132
locationMsg,

lib/utils/config/definitions.js

+1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ define('cache', {
322322
`,
323323
flatten (key, obj, flatOptions) {
324324
flatOptions.cache = join(obj.cache, '_cacache')
325+
flatOptions.npxCache = join(obj.cache, '_npx')
325326
},
326327
})
327328

test/lib/exec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ let PROGRESS_ENABLED = true
2424
const LOG_WARN = []
2525
let PROGRESS_IGNORED = false
2626
const flatOptions = {
27+
npxCache: 'npx-cache-dir',
28+
cache: 'cache-dir',
2729
legacyPeerDeps: false,
2830
package: [],
2931
}
3032
const config = {
31-
cache: 'cache-dir',
33+
cache: 'bad-cache-dir', // this should never show up passed into libnpmexec
3234
yes: true,
3335
call: '',
3436
package: [],
@@ -134,6 +136,8 @@ t.test('npx foo, bin already exists locally', t => {
134136
t.match(RUN_SCRIPTS, [{
135137
pkg: { scripts: { npx: 'foo' }},
136138
args: ['one arg', 'two arg'],
139+
cache: flatOptions.cache,
140+
npxCache: flatOptions.npxCache,
137141
banner: false,
138142
path: process.cwd(),
139143
stdioString: true,

test/lib/init.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,16 @@ const npmLog = {
1212
silly: () => null,
1313
}
1414
const config = {
15+
cache: 'bad-cache-dir',
1516
'init-module': '~/.npm-init.js',
1617
yes: true,
1718
}
19+
const flatOptions = {
20+
cache: 'test-config-dir/_cacache',
21+
npxCache: 'test-config-dir/_npx',
22+
}
1823
const npm = mockNpm({
24+
flatOptions,
1925
config,
2026
log: npmLog,
2127
})
@@ -82,16 +88,18 @@ t.test('classic interactive npm init', t => {
8288
})
8389

8490
t.test('npm init <arg>', t => {
85-
t.plan(1)
91+
t.plan(3)
8692
npm.localPrefix = t.testdir({})
8793

8894
const Init = t.mock('../../lib/init.js', {
89-
libnpmexec: ({ args }) => {
95+
libnpmexec: ({ args, cache, npxCache }) => {
9096
t.same(
9197
args,
9298
['create-react-app'],
9399
'should npx with listed packages'
94100
)
101+
t.same(cache, flatOptions.cache)
102+
t.same(npxCache, flatOptions.npxCache)
95103
},
96104
})
97105
const init = new Init(npm)

test/lib/utils/config/definitions.js

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ t.test('cache', t => {
181181
defsNix.cache.flatten('cache', { cache: '/some/cache/value' }, flat)
182182
const {join} = require('path')
183183
t.equal(flat.cache, join('/some/cache/value', '_cacache'))
184+
t.equal(flat.npxCache, join('/some/cache/value', '_npx'))
184185

185186
t.end()
186187
})

0 commit comments

Comments
 (0)