Skip to content

Commit 71d8fb4

Browse files
macdja38zkat
authored andcommittedNov 26, 2018
publish: Close the file descriptor if exiting upload via an error. (#80)
This fixes https://npm.community/t/unhelpful-error-message-when-publishing-without-logging-in-error-eperm-operation-not-permitted-unlink/1377/3 and the other dozen or so issues that that link references, and possibly many more involving poor error messages from errors thrown by the upload function. @zkat you mentioned you could take a look at any fixes / answer any questions, if you could look this over and let me know if this is a good / valid approach that would be fantastic, thanks! (it wasn't a race condition, luckily :P). it may also be helpful to add something like ``` if (!auth.token || !(auth.username && auth.password)) { log.warn('publish', 'not logged in') } ``` just before we even open the first file descriptor to make sure that even if the error message is completely wrong something in the log will give users a clue what may be going on. I took the method of looking for login creds from the logout method, I'm not sure that's valid or if alternatives to npm exist that don't require credentials but users could still publish to. Triage of the issue: 1. The upload function throws an error 2. As that error bubbles through [cacache](https://www.npmjs.com/package/cacache#with-tmp) it tries to delete the tmpdir as it should 3. It can't delete the temp dir as the upload function's readFileStream to the tar it was trying to upload is still open. 4. [cacache](https://www.npmjs.com/package/cacache#with-tmp) throws an error about it's inability to remove the dir, which suppresses the upload function's error. Fixes: https://npm.community/t/unhelpful-error-message-when-publishing-without-logging-in-error-eperm-operation-not-permitted-unlink/1377/3 PR-URL: #80 Credit: @macdja38 Reviewed-By: @zkat
1 parent d3e8a7c commit 71d8fb4

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed
 

‎lib/publish.js

+10
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ function upload (arg, pkg, isRetry, cached) {
167167
auth: auth
168168
}
169169

170+
function closeFile () {
171+
if (!npm.config.get('dry-run')) {
172+
params.body.close()
173+
}
174+
}
175+
170176
// registry-frontdoor cares about the access level, which is only
171177
// configurable for scoped packages
172178
if (config.get('access')) {
@@ -195,13 +201,17 @@ function upload (arg, pkg, isRetry, cached) {
195201
return BB.fromNode((cb) => {
196202
npm.commands.unpublish([pkg._id], cb)
197203
}).finally(() => {
204+
// close the file we are trying to upload, we will open it again.
205+
closeFile()
198206
// ignore errors. Use the force. Reach out with your feelings.
199207
return upload(arg, pkg, true, cached).catch(() => {
200208
// but if it fails again, then report the first error.
201209
throw err
202210
})
203211
})
204212
} else {
213+
// close the file we are trying to upload, all attempts to resume will open it again
214+
closeFile()
205215
throw err
206216
}
207217
})

0 commit comments

Comments
 (0)
Please sign in to comment.