zkat / pacote

programmatic npm package and metadata downloader (moved!)
https://github.com/npm/pacote
MIT License
280 stars 62 forks source link

"err.code.match is not a function" after a failed git clone #169

Closed parties closed 5 years ago

parties commented 5 years ago

I discovered this bug while using NPM to clone a private git repository and the clone failed (ssh_exchange_identification: Connection closed by remote host, could be a rate-limit issue from our internal server).

Error log from npm i --verbose:

npm verb stack TypeError: err.code.match is not a function
npm verb stack     at BB.try.catch.err (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/pacote/lib/with-tarball-stream.js:110:55)
npm verb stack     at tryCatcher (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/bluebird/js/release/util.js:16:23)
npm verb stack     at Promise._settlePromiseFromHandler (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:512:31)
npm verb stack     at Promise._settlePromise (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:569:18)
npm verb stack     at Promise._settlePromise0 (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:614:10)
npm verb stack     at Promise._settlePromises (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/bluebird/js/release/promise.js:690:18)
npm verb stack     at _drainQueueStep (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:138:12)
npm verb stack     at _drainQueue (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:131:9)
npm verb stack     at Async._drainQueues (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:147:5)
npm verb stack     at Immediate.Async.drainQueues (/Users/[REDACTED]/.nvm/versions/node/v8.14.0/lib/node_modules/npm/node_modules/bluebird/js/release/async.js:17:14)
npm verb stack     at runCallback (timers.js:810:20)
npm verb stack     at tryOnImmediate (timers.js:768:5)
npm verb stack     at processImmediate [as _immediateCallback] (timers.js:745:5)
npm verb cwd /Users/[REDACTED]
npm verb Darwin 18.2.0
npm verb argv "/Users/[REDACTED]/.nvm/versions/node/v8.14.0/bin/node" "/Users/[REDACTED]/.nvm/versions/node/v8.14.0/bin/npm" "i" "--verbose"
npm verb node v8.14.0
npm verb npm  v6.7.0
npm ERR! err.code.match is not a function

Unfortunately I don't know how to reproduce the bug in an environment outside our internal network (since it requires the remote host to close the connection).

Error message from inspecting err.cause.message in the Node debugger:

Command failed: git clone --mirror -q git@[REDACTED].git /Users/[REDACTED]/.npm/_cacache/tmp/git-clone-c6ce09c3/.git
warning: templates not found /var/folders/6d/t0qr1_gx7zngvgvtpbd6bg8wsq82z0/T/pacote-git-template-tmp/git-clone-6acc9b69
ssh_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I can confirm that I do have access to the repository, and running npm i will sometimes pass without any errors (which leads me to believe it's a rate-limit issue on our side).


Cause: it is coming from this line of code within with-tarball-stream.js.

if (opts.cache && err.code && !err.code.match(/^E\d{3}$/)) { ... }

The issue is that err.code is a Number, not a String, and the Number prototype does not have a match function.

I would assume the easiest fix would be to simply interpret the code as a String and match from there:

if (opts.cache && err.code && !String(err.code).match(/^E\d{3}$/)) {

Thoughts?

rkaw92 commented 5 years ago

Confirmed - this still appears to be an issue in 6.9.0. Not sure yet what the underlying error is, since it is getting masked by the bug.

michaellopez commented 5 years ago

I got hit by this too. Lost internet connection during and npm install. So that might be a way to trigger the bug, npm install and manually disconnect computer from the internet.

npm -v
6.9.0
node -v
v10.15.3
michaellopez commented 5 years ago

Forgot to say that I only use npm, not pacote.

parties commented 5 years ago

Forgot to say that I only use npm, not pacote.

pacote is a dependency of npm (package.json).

BigB007 commented 5 years ago

Issue still persists. Have anyone found the solution?

Cause: it is coming from this line of code within with-tarball-stream.js.

if (opts.cache && err.code && !err.code.match(/^E\d{3}$/)) { ... }

parties commented 5 years ago

170 aims to fix this.

ghost commented 5 years ago

@parties when #170 will be merged?

thundron commented 5 years ago

+1 on this

parties commented 5 years ago

@parties when #170 will be merged?

Not yet, you can watch the PR for more details.

I approved the changes, but I don't have permissions to merge, so we'll have to wait for official approval.

parties commented 5 years ago

170 is merged!