From 7f541e82a0b2908cc0cfef9a36b714eeab40c029 Mon Sep 17 00:00:00 2001 From: milaninfy <111582375+milaninfy@users.noreply.github.com> Date: Mon, 14 Oct 2024 13:05:59 -0400 Subject: [PATCH] fix: make pack and exec work with git hash refs (#7815) Passing arborist constructor to pacote.manifest call because internally when fetching git deps DirFetcher requires Arborist constructor from GitFetcher https://github.com/npm/pacote/blob/main/CHANGELOG.md#1400-pre3-2022-09-28 - [x] Trying to add some tests Fixes: https://github.com/npm/cli/issues/6723 --- lib/commands/pack.js | 3 ++- test/fixtures/git-test.tgz | Bin 0 -> 268 bytes test/lib/commands/exec.js | 24 ++++++++++++++++++ workspaces/libnpmexec/lib/index.js | 2 +- workspaces/libnpmpack/lib/index.js | 2 +- .../libnpmpack/test/fixtures/git-test.tgz | Bin 0 -> 245 bytes workspaces/libnpmpack/test/index.js | 14 ++++++++++ 7 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/git-test.tgz create mode 100644 workspaces/libnpmpack/test/fixtures/git-test.tgz diff --git a/lib/commands/pack.js b/lib/commands/pack.js index 79e7f49f819ec..6a1bb53acb8aa 100644 --- a/lib/commands/pack.js +++ b/lib/commands/pack.js @@ -29,12 +29,13 @@ class Pack extends BaseCommand { const unicode = this.npm.config.get('unicode') const json = this.npm.config.get('json') + const Arborist = require('@npmcli/arborist') // Get the manifests and filenames first so we can bail early on manifest // errors before making any tarballs const manifests = [] for (const arg of args) { const spec = npa(arg) - const manifest = await pacote.manifest(spec, this.npm.flatOptions) + const manifest = await pacote.manifest(spec, { ...this.npm.flatOptions, Arborist }) if (!manifest._id) { throw new Error('Invalid package, must have name and version') } diff --git a/test/fixtures/git-test.tgz b/test/fixtures/git-test.tgz new file mode 100644 index 0000000000000000000000000000000000000000..c06592708c2a0995543a3716238ea7b803e7a22e GIT binary patch literal 268 zcmV+n0rUPJiwFP!00002|Lv8}O2a@9hxfcsF~-BT&?K{(KcIKPgC~7~bUVt{W)pXk zS`pt}TALn<-i%V%?=tY4VPFm)(^lrWdZN_21#OZgixEJ?GSA_=e2dm4h|9b{t&0LP z5U8=dC_$k31A6W%FA#s>NJNA~JrT~&(w==fw=N~O2G=Y|N>4An^)ykBtftAUw;kQs z72Ur%Gf}RsW7?33JK`?p-WXz5Go_~2Qi{N7(eC}<*JR0!8^Qk~%YOF1#AE+;zMT*L zkI%aV0Ek>wQ*;H { ) } }) + +t.test('packs from git spec', async t => { + const spec = 'test/test#111111aaaaaaaabbbbbbbbccccccdddddddeeeee' + const pkgPath = path.resolve(__dirname, '../../fixtures/git-test.tgz') + + const srv = MockRegistry.tnock(t, 'https://codeload.github.com') + srv.get('/test/test/tar.gz/111111aaaaaaaabbbbbbbbccccccdddddddeeeee') + .times(2) + .reply(200, await fs.readFile(pkgPath)) + + const { npm } = await loadMockNpm(t, { + config: { + audit: false, + yes: true, + }, + }) + try { + await npm.exec('exec', [spec]) + const exists = await fs.stat(path.join(npm.prefix, 'npm-exec-test-success')) + t.ok(exists.isFile(), 'bin ran, creating file') + } catch (err) { + t.fail(err, "shouldn't throw") + } +}) diff --git a/workspaces/libnpmexec/lib/index.js b/workspaces/libnpmexec/lib/index.js index 8344471696e25..78633a8cadb3c 100644 --- a/workspaces/libnpmexec/lib/index.js +++ b/workspaces/libnpmexec/lib/index.js @@ -24,7 +24,7 @@ const manifests = new Map() const getManifest = async (spec, flatOptions) => { if (!manifests.has(spec.raw)) { - const manifest = await pacote.manifest(spec, { ...flatOptions, preferOnline: true }) + const manifest = await pacote.manifest(spec, { ...flatOptions, preferOnline: true, Arborist }) manifests.set(spec.raw, manifest) } return manifests.get(spec.raw) diff --git a/workspaces/libnpmpack/lib/index.js b/workspaces/libnpmpack/lib/index.js index b026ad1a935c7..bd3e0c7bd7232 100644 --- a/workspaces/libnpmpack/lib/index.js +++ b/workspaces/libnpmpack/lib/index.js @@ -12,7 +12,7 @@ async function pack (spec = 'file:.', opts = {}) { // gets spec spec = npa(spec) - const manifest = await pacote.manifest(spec, opts) + const manifest = await pacote.manifest(spec, { ...opts, Arborist }) const stdio = opts.foregroundScripts ? 'inherit' : 'pipe' diff --git a/workspaces/libnpmpack/test/fixtures/git-test.tgz b/workspaces/libnpmpack/test/fixtures/git-test.tgz new file mode 100644 index 0000000000000000000000000000000000000000..cc2b701d7df308f50056ace79de475e41d0a4b28 GIT binary patch literal 245 zcmVlZo)7ShCSygPB$=B;oO7_Su2Ll9SeiyT#KRDk!=bH z@$Qvl)T!!(Dpm5gob=iH@4r#$oqFf_m}k8W&WbxjL|xm$Pd?E!k06>wM^bhjwIE2! zMfy+T4>*LVydb*6kwgUF^Ph030MM#EV-FZPM0`l*lYKDGCMj#GDcij=lQMSTQ@st@ z2JOul1NM-@;1<&ZvfemY;nI8Od$11D1B8*ac{4n$5N0#rlZha)0=Q0TJ3b$sA5z@w vdNS$|H_oTPyzj$kwqYv#{PL9Dz`4b(GgjA16bgkx;TOIDc-H~Q00;m8E}M14 literal 0 HcmV?d00001 diff --git a/workspaces/libnpmpack/test/index.js b/workspaces/libnpmpack/test/index.js index f5eb647e4153b..62d5af1a80d2c 100644 --- a/workspaces/libnpmpack/test/index.js +++ b/workspaces/libnpmpack/test/index.js @@ -7,6 +7,7 @@ const spawk = tspawk(t) const fs = require('node:fs') const path = require('node:path') +const { resolve } = require('node:path') const pack = require('../lib/index.js') const tnock = require('./fixtures/tnock.js') @@ -133,6 +134,19 @@ t.test('packs from registry spec', async t => { t.ok(tarball) }) +t.test('packs from git spec', async t => { + const spec = 'test/test#111111aaaaaaaabbbbbbbbccccccdddddddeeeee' + const pkgPath = resolve(__dirname, 'fixtures/git-test.tgz') + + const srv = tnock(t, 'https://codeload.github.com') + srv.get('/test/test/tar.gz/111111aaaaaaaabbbbbbbbccccccdddddddeeeee') + .times(2) + .reply(200, fs.readFileSync(pkgPath)) + + const tarball = await pack(spec, { ...OPTS }) + t.ok(tarball) +}) + t.test('runs scripts in foreground when foregroundScripts === true', async t => { const testDir = t.testdir({ 'package.json': JSON.stringify({