typicode / husky

Git hooks made easy 🐶 woof!
https://typicode.github.io/husky
MIT License
31.82k stars 1k forks source link

Hook works, but then: TypeError: dest.end is not a function #459

Closed papb closed 4 years ago

papb commented 5 years ago

Summary of the bug

My pre-commit hook calls npm test. The test itself succeeds but then husky fails with the following error:

_stream_readable.js:628
    dest.end();
         ^

TypeError: dest.end is not a function
    at Socket.onend (_stream_readable.js:628:10)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
npm ERR! Test failed.  See above for more details.
husky > pre-commit hook failed (add --no-verify to bypass)

My test uses cypress. I am opening the issue here on husky and not on cypress because npm test works fine when called directly, but the pre-commit hook fails.

Fully detalied steps to reproduce the bug

  1. mkdir test && cd test
  2. git init
  3. Create a .gitignore file for node_modules
  4. npm init -y
  5. npm i --save-dev husky cypress
  6. npx cypress open
  7. Close cypress
  8. Create a file cypress/integration/sample_spec.js with the following content:
describe('My First Test', function() {
  it('Does not do much!', function() {
    expect(true).to.equal(true)
  })
})
  1. Delete the other default tests just to make it run much faster: rm -r cypress/integration/examples
  2. Go to package.json and change the test script to be "cypress run"
  3. npm test - Just to observe that the test command works perfectly
  4. Add the following husky config to package.json:
{
  "husky": {
    "hooks": {
      "pre-commit": "npm test"
    }
  }
}
  1. git commit -m "Initial commit". The cypress test will run and succeed, but then, right after:
_stream_readable.js:628
    dest.end();
         ^

TypeError: dest.end is not a function
    at Socket.onend (_stream_readable.js:628:10)
    at Object.onceWrapper (events.js:273:13)
    at Socket.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
npm ERR! Test failed.  See above for more details.
husky > pre-commit hook failed (add --no-verify to bypass)

Versions

husky: 1.3.1 node: 10.15.0 npm: 6.4.1 cypress: 3.1.5 OS: Windows_NT x64 6.1.7601

papb commented 5 years ago

@ryan-snyder Thanks for the info! Maybe this is something with cypress then. I will open an issue there. Thanks.

alexk0ch commented 5 years ago

https://nodejs.org/api/stream.html#stream_event_end

By default, stream.end() is called on the destination Writable stream when the source Readable stream emits 'end', so that the destination is no longer writable. To disable this default behavior, the end option can be passed as false, causing the destination stream to remain open:

I had the following in my code:

testProcess.stdout.pipe(process.stdout);

basically, one stream pipes to another one which can't handle END event. Hope this helps, for me it worked via adding options object:

testProcess.stdout.pipe(process.stdout, { end: false });
papb commented 5 years ago

@alexk0ch Hello, thanks for this, looks promising, but where exactly should I add your code above? Thanks!

alexk0ch commented 5 years ago

yes, if cypress is piping streams there was a breaking change as far as I understand in node 10. We've started to get this error after migration to node 10. Code above is from our custom build tool.

papb commented 5 years ago

@alexk0ch Hello again, sorry to bother, I still don't understand where exactly should I place that line of code you posted, can you clarify? Thanks!

renanperalta commented 5 years ago

@alexk0ch I also dont understand where exactly should I place that line of code you posted, can you clarify? Thanks!

Note: I tried with Node 8.16, but the error continues.

alexk0ch commented 5 years ago

node v10.15.3, 8.12.0

"cypress": "^3.2.0", "husky": "^2.2.0"

can't reproduce issue from @papb's steps

papb commented 5 years ago

@alexk0ch interesting. Perhaps the new version of husky fixed this? Can you test with older versions?

dmauldin commented 5 years ago

@alexk0ch @papb I'm still getting this exact issue with

husky@2.3.0
cypress@3.2.0
papb commented 5 years ago

@dmauldin I haven't had the time to test it yet, so thanks for your comment :grin: Maybe this is an OS issue? I was using windows, what did you use? And @alexk0ch, what did you use? Thanks!

alexk0ch commented 5 years ago

@dmauldin I haven't had the time to test it yet, so thanks for your comment 😁 Maybe this is an OS issue? I was using windows, what did you use? And @alexk0ch, what did you use? Thanks!

osx, latest

dmauldin commented 5 years ago

Windows, as well. Node 10.15.3. Powershell.

Also tried downgrading husky to 2.2.0 and upgrading cypress to 3.3.1 (separately), neither of which made any difference.

Wouldn't be the first time Windows gave me grief trying to do something that works in a non-windows environment. ;)

dmauldin commented 5 years ago

So, from the cypress issue listed above, it looks like their fix is to run in bash on windows (ie: no actual fix in cypress). It at least looks like it's a Windows issue, since every single person that says it doesn't work is doing it on Windows and everybody that can't repro is not on Windows.

Maybe it's time to close this issue and just refer people to the cypress issue?

papb commented 5 years ago

@dmauldin I can't test right now, can you test with Git Bash for example and see if it works? Thanks a lot!

papb commented 5 years ago

@ryan-snyder Just to make sure I understand, the cypress issue didn't solve your problem (yet), right? Did you try renanperalta's comment?

(again, I'm asking because I didn't have time to test anything yet)

dmauldin commented 5 years ago

@papb that fix is specifically for gitlab on Windows, which I'm not doing. I'm just using husky locally on Windows. I tried using git-bash, but it's the same failure. Using WSL bash may work, but that's a long stretch as then you have to install a metric tonne of libraries to support cypress, as even when running headless, it still needs all the X libs, gtk, etc.

I'm going to spend about another hour on this today and then move on, since the cypress team knows about it, but just aren't visibly doing anything about it. I'm hoping I may be able to find a simple change to the existing libs to get around the problem.

papb commented 5 years ago

@dmauldin thanks again for all the info. Hopefully I'll have some time soon to take another look on this...

@jennifer-shehane sorry to bother but can you give your opinion on this? Do you think this is on cypress or on husky? Thanks!

tejas-hosamani commented 4 years ago

Hi, any update/fix on this issue? I am using:

 "husky": {
      "version": "2.7.0",
}

"cypress": {
      "version": "3.4.1",
}

Facing the same exact issue.

bahmutov commented 4 years ago

We believe this is an issue with Cypress on Windows and not with Husky. We have an open PR that fixes this in https://github.com/cypress-io/cypress/pull/5045 (fixes the direction of the STDIN pipe)

papb commented 4 years ago

@bahmutov Nice!!

ARemaity commented 8 months ago

Not related to husky , but im using koa in strapi to pipe pdf content to the response, i faced same , i used the solution mentioned by @alexk0ch ,and its working, thanks!

if (fs.existsSync(getUrlName)) { let createstream = fs.createReadStream(getUrlName); ctx.response.set("content-type", "application/pdf"); ctx.response.set("Content-Disposition", "attachment; filename=${fetchedfileName}.pdf"); ctx.body= ctx.req.pipe(createstream,{ end: false }); return await next(); }