typicode / husky

Git hooks made easy 🐢 woof!
https://typicode.github.io/husky
MIT License
32.61k stars 1.03k forks source link

[4.0.0 | Git Bash | Windows 10] stdin is not a tty #627

Closed wKovacs64 closed 3 years ago

wKovacs64 commented 4 years ago

Husky 4.0.0 isn't working in Git Bash for Windows 10, triggering a stdin is not a tty error for every git operation that triggers a hook.

Versions

OS: Windows 10 1903 Shell: Git Bash (GNU bash, version 4.4.23(1)-release (x86_64-pc-msys)) Git: git 2.24.1.windows.2

HUSKY_DEBUG=1 npm install husky --save-dev

$ HUSKY_DEBUG=1 npm install husky --save-dev

> husky@4.0.0 install C:\dev\test\husky4-tty-test\node_modules\husky
> node husky install

husky > Setting up git hooks
husky:debug Current working directory is C:\dev\test\husky4-tty-test\node_modules\husky
husky:debug INIT_CWD is set to C:\dev\test\husky4-tty-test
husky:debug Git rev-parse command returned:
husky:debug   --git-common-dir: undefined
husky:debug   --show-prefix:
The "path" argument must be of type string. Received type undefined
husky:debug TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received type undefined
    at validateString (internal/validators.js:112:11)
    at Object.resolve (path.js:139:9)
    at getDirs (C:\dev\test\husky4-tty-test\node_modules\husky\lib\installer\bin.js:27:49)
    at run (C:\dev\test\husky4-tty-test\node_modules\husky\lib\installer\bin.js:62:62)
    at Object.<anonymous> (C:\dev\test\husky4-tty-test\node_modules\husky\lib\installer\bin.js:85:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Module.require (internal/modules/cjs/loader.js:852:19)
husky > Failed to install

> husky@4.0.0 postinstall C:\dev\test\husky4-tty-test\node_modules\husky
> opencollective-postinstall || exit 0

Thank you for using husky!
If you rely on this package, please consider supporting our open collective:
> https://opencollective.com/husky/donate

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN husky4-tty-test@1.0.0 No description
npm WARN husky4-tty-test@1.0.0 No repository field.

+ husky@4.0.0
added 48 packages from 22 contributors and audited 48 packages in 2.047s

3 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

HUSKY_DEBUG=1 git commit

Note: on the fresh test repo, this command worked because the first command failed so the hooks did not install properly. On an existing repo with existing hooks, it looks like this:

husky:debug husky v4.0.0 (created at 1/7/2020, 1:24:18 PM)
husky:debug pre-commit hook started
husky:debug Current working directory is /c/dev/test/existing-repo
husky:debug pre-commit hook started
stdin is not a tty
husky:debug yarn husky-run exited with 1 exit code
wKovacs64 commented 4 years ago

Adding exec < /dev/tty to ~/.huskyrc as mentioned in several other similar issues gets around the problem but I didn't see anything about it in the change log and suspect it shouldn't be required based on those other discussions.

typicode commented 4 years ago

Thanks again for the report.

I've been able to reproduce the first issue by creating a non git project:

$ mkdir foo && cd foo
$ npm init -y && npm install husky
# ...
husky > Setting up git hooks
The "path" argument must be of type string. Received type undefined
husky > Failed to install

Husky should have displayed a better error message.

Running git init or cloning an existing project should fix the issue.

$ mkdir foo && cd foo
$ git init && npm init -y && npm install husky

That said, I have trouble reproducing the one with stdin is not a tty. Could you provide some steps with a freshly created repo?

I've tried this in Git Bash on Windows 10:

$ mkdir foo && cd foo
$ git --version && npm --version
git version 2.24.1.windows.2
6.13.4
$ git init && npm init -y && npm i husky -D
$ echo '{ "hooks": { "pre-commit": "echo hello && exit 1" } }' > .huskyrc
$ git add .huskyrc && git commit -m foo
husky > pre-commit (node v10.10.0)
hello
wKovacs64 commented 4 years ago

Thanks for investigating. Seems to be an issue pertaining to yarn only (npm works, as you mentioned).

Replace npm i husky -D with yarn add husky -D (or even run the yarn one after the npm one if you want) and it will start producing the stdin is not a tty error.

Comparing .git/hooks/pre-commit, this is the only meaningful difference I see:

-packageManager=npm
+packageManager=yarn
typicode commented 4 years ago

Hmm, I've done the previous steps but used yarn 1.21.1 and I'm not getting stdin is not a tty error. What's the content of your .huskyrc?

wKovacs64 commented 4 years ago

In real projects, I'm using husky.config.js:

module.exports = {
  hooks: {
    'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
    'pre-commit': 'lint-staged',
  },
};

But I reproduced it using your example steps, so .huskyrc is just:

{ "hooks": { "pre-commit": "echo hello && exit 1" } }
jetersen commented 4 years ago

this is not limited to git bash, this happens in command prompt and powershell.

For me I only experienced it on git push

wKovacs64 commented 4 years ago

Hmm, it only happens to me in Git Bash and it's any/all hooks. Works as expected in PS and CMD.

typicode commented 4 years ago

What's your version of yarn?

wKovacs64 commented 4 years ago
$ yarn --version
1.21.1
jetersen commented 4 years ago
❯ yarn -v
1.21.1
typicode commented 4 years ago

The first issue (The "path" argument must be of type string.) should be fixed by Husky 4.0.3 and a better error should be displayed.

I'm still trying to reproduce the stdin error but I don't see yet what would be the difference between my env and yours πŸ€”

screen

jetersen commented 4 years ago

Seems it's related to eslint in my case:

Γ— eslint --fix found some errors. Please fix them and try committing again.

Oops! Something went wrong! :(

ESLint: 6.8.0.

ESLint couldn't find the config "xo-space/esnext" to extend from. Please check that the name of the config is correct.  
The config "xo-space/esnext" was referenced from the config file in "C:\git\code\release-drafter
ode_modules\husky\.eslintrc.js".

If you still have problems, please stop by https://gitter.im/eslint/eslint to chat with the team.
husky > pre-commit hook failed (add --no-verify to bypass)

Seems like you should limit what get's published in your npm package.

wKovacs64 commented 4 years ago

image

πŸ€·β€β™‚

You're sure you don't have exec < /dev/tty in your ~/.huskyrc or anything like that, @typicode?

typicode commented 4 years ago

Good idea. But no, just checked :/ (I don't have a ~/.huskyrc). There are threads about Yarn and stdin is not a tty error on Windows 10, maybe that can help.

Do you also have this error if you try in a fresh repo with Yarn 2: https://next.yarnpkg.com/

You just have to run yarn policies set-version berry before installing Husky.

wKovacs64 commented 4 years ago

Those threads are how I found exec < /dev/tty, which is the only thing has worked so far. But that shouldn't be necessary, right? I didn't need it in husky v3.1.0, anyway. πŸ˜• If that's the solution, so be it - it's just new and I'm not familiar with the repercussions in other areas of the system (maybe there are none).

Interestingly, the error does not occur with yarn v2.0.0-rc.21 (berry).

viceice commented 4 years ago

Seeing this on git push too

❯ git push -f
stdin is not a tty

Roleback to 3.1.0 as workaround

justinhp commented 4 years ago

I too saw this on git push from a Windows 10 machine using Git v2.20.1.windows.1 and Yarn v1.21.1. Downgrading to 3.1.0 fixed the issue.

jetersen commented 4 years ago

@ViceIce @justinhp if you update husky to v4.0.4 do you get any additional information?

jetersen commented 4 years ago

for me v4.0.4 still fails and seems to be related to yarn v1.21.1 if I go into pre-push hook shell script and change packageManager=yarn to packageManager=npm it I can suddenly git push again.

jetersen commented 4 years ago

after enough investigating it seems to me that this is bug located inside the yarn v1's shell script using winpty

I created two PRs, first one solve winpty problem if you still wish to use yarn run and second one uses npx instead of yarn run

should be noted that the first one is dependent on which version of yarn your using. So it might need some additional logic

typicode commented 4 years ago

Thanks for the PRs!

viceice commented 4 years ago

Strange, pre-commit works, but pre-push not.

❯ git push origin feature/maintenance
husky:debug husky v4.0.6 (created at 2020-1-10 9:40:27)
husky:debug pre-push hook started
husky:debug Current working directory is ~/projects/test
husky:debug pre-push hook started
stdin is not a tty
husky:debug yarn run --silent husky-run exited with 1 exit code
error: failed to push some refs to 'https://******/scm/test.git'
tobiasweibel commented 4 years ago

If I run npx husky then the git commands are working again. It's of course a workaround.

viceice commented 4 years ago

exec < /dev/tty does work for normal cmd /powershell, but crashes git commit in vscode (/dev/tty No such a device or address)

viceice commented 4 years ago

ok, this does the trick:

if [ -t 1 ]; then
  exec < /dev/tty
fi
jetersen commented 4 years ago

what does -t 1 validate?

jetersen commented 4 years ago

Tested and validated #634 feel free to review it :)

viceice commented 4 years ago

what does -t 1 validate?

It will test if we stdout is available.

https://stackoverflow.com/questions/911168/how-to-detect-if-my-shell-script-is-running-through-a-pipe

typicode commented 4 years ago

@casz @wKovacs64 @ViceIce I'd really like to be able to test locally. Working or reviewing PRs related to stdin error without a reproducible example is quite hard.

Would you have any ideas what I could do to have the error in my env? I've installed Git using https://git-scm.com/ with default options (tried to reinstall it also multiple times) and Yarn using npm i -g yarn. I tried on another computer with Windows 10 and couldn't reproduce either.

typicode commented 4 years ago

Found it, installed Yarn using the Windows installer (https://yarnpkg.com/en/docs/install#windows-stable) and it fails with stdin is not a tty πŸŽ‰

viceice commented 4 years ago

I installed git, nodejs-lts and yarn with chocolatey.

git: 2.24.1.windows.2 yarn: 1.21.1 node: 12.14.1

jetersen commented 4 years ago

I installed with chocoloately and had the issue.

jetersen commented 4 years ago

Let me add a PR with reproducible tests inside appveyor. :)

Sowed commented 4 years ago

If I run npx husky then the git commands are working again. It's of course a workaround.

@tobiasweibel Works like a charm! Thank. Just curious though, how is it working? Restarting the PC resets the env and the workaround ceases to work. I guess it's to do with how npx works.

smakhtin commented 4 years ago

Same for us, using workarounds mentioned above.

viceice commented 4 years ago

@jetersen Any timeframe to get this solved?

tobiasweibel commented 4 years ago

I think the problem was fixed already a longer time ago. Just update to the latest 4* version.

viceice commented 4 years ago

ok, seems to work πŸ˜… will remove the workaround

mgdeveloper79 commented 4 years ago

I had the same issue with version 3.x but version 4.1.0 has worked for me without needing any workarounds mentioned above.

oarthursilva commented 3 years ago

ok, this does the trick:

if [ -t 1 ]; then
  exec < /dev/tty
fi

thx viceice, it worked smoothly in windows 10

enzo-phinx commented 3 years ago

ok, this does the trick:

if [ -t 1 ]; then
  exec < /dev/tty
fi

thx viceice, it worked smoothly in windows 10

This worked for me.

codeyourwayup commented 2 years ago

Windows 11, bash. step1: cd ~ step2: $ touch .huskyrc step3: nano .huskyrc step4: paste

if [ -t 1 ]; then
  exec < /dev/tty
fi

step5: ctrl+x to save Success!!

turbidwater commented 4 months ago

Still running into this issue on husky 8.0.3 and yarn 1.22.22.

Fortunately the above fix in the .huskyrc still fixes it.

TyMick commented 4 months ago

I'm getting the stdin is not a tty error (Windows 10, PowerShell, Yarn v1.22.22) with husky v8.0.3, but no error with husky v9.0.11. πŸŽ‰