yarn icon indicating copy to clipboard operation
yarn copied to clipboard

Yarn fails under git-bash: Can't answer a question unless a user TTY

Open jmathew opened this issue 7 years ago • 36 comments

Do you want to request a feature or report a bug? Bug.

What is the current behavior? Running yarn init yields:

$ yarn init
yarn init v1.0.2
error An unexpected error occurred: "Can't answer a question unless a user TTY".

What is the expected behavior? Standard init process.

Please mention your node.js, yarn and operating system version. Versions:

Git-bash
You are downloading the latest (2.14.1) 64-bit version of Git for Windows. 

Which seems to install MinTTY 2.7.7

$ node -v
v8.1.4

$ npm -v
5.0.3

$ yarn -v
1.0.2

$ bash --version
GNU bash, version 4.4.12(1)-release (x86_64-pc-msys)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Background Research

The problem was previously noted in: https://github.com/yarnpkg/yarn/issues/743

This yielded a fix that tried to autodetect mintty and use winpty: https://github.com/yarnpkg/yarn/pull/2243

That fix worked for interactive operations but breaks any sort of piping.

This fix was removed in favor a explicit environment variable in: https://github.com/yarnpkg/yarn/issues/2998

The reason being yarn shell commands seemed to fail because of winpty when used in a pipe. And there appears to be some terminal output that shows git bash working without the need for winpty. This is not consistent with my experience.

Technical details

I believe yarn uses isTTY to check for an interactive terminal. The discussion attributed the issue to be caused by winpty however running node by itself can produce that error:

$  node -p -e "Boolean(process.stdout.isTTY)"
true

$  node -p -e "Boolean(process.stdout.isTTY)" | cat
stdout is not a tty

I think that's the check that's actually causing the issue when any yarn command is involved in a pipe. But weirdly using yarn in a pipe works for commands that are not interactive like --version.

Below is a bunch of commands I ran on the terminal that I hope help diagnose the issue.

$ yarn --version | cat
1.0.2

$ yarn init
yarn init v1.0.2
error An unexpected error occurred: "Can't answer a question unless a user TTY".
info If you think this is a bug, please open a bug report with the information provided in "C:\\Users\\jmathew\\work\\FatGolem\\yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/init for documentation about this command.

$ YARN_FORCE_WINPTY=1 yarn --version
1.0.2

$ YARN_FORCE_WINPTY=1 yarn --version  | cat
stdout is not a tty

$ bash -c 'yarn install'
yarn install v1.0.2
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
info Lockfile not saved, no dependencies.
Done in 0.05s.

$ bash -c 'YARN_FORCE_WINPTY=1 yarn install'
yarn install v1.0.2
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...

info Lockfile not saved, no dependencies.
Done in 0.05s.

There's a lot of moving pieces so its hard for me to understand the cause and effect here. So I'm not sure of a good solution that keeps this interactive terminal check.

My initial thought is that this check for interactive terminal isn't really worth the hassle of keeping around. If you design a script that has yarn init and fail to provide input or redirect properly and it spins forever waiting that should be on you right? Npm doesn't do this and I suspect its because of these kinds of problems.

Summary

It would seem that I have to use the YARN_FORCE_WINPTY when using an interactive command in git bash and specifically not use that when trying to do any piping.

Further, YARN_FORCE_WINPTY isn't documented anywhere as far as I know. Other than the old issues and the script itself. Maybe add that to the error message?

Regardless of all that I just want to be able to type yarn init and have it work consistently without having to do anything special to make it work on windows.

jmathew avatar Sep 18 '17 21:09 jmathew

This looks a lot like a duplicate of #2591 so closing in favor of that. Reopen if you don't agree.

BYK avatar Sep 26 '17 11:09 BYK

Please consider reopening. #2591 may have fixed a related issue, but this issue is still present today (or is present again).

image

@BYK @jmathew

alancnet avatar Oct 30 '18 12:10 alancnet

Use PowerShell instead.

Nekosandesu avatar Feb 21 '19 03:02 Nekosandesu

I'm not satisfied that this thread ends with "You should use my preferred tool instead of your preferred tool". This is still a problem. NPM works, Yarn doesn't. How about some action here?

@kittens @bestander @arcanis @sebmck @BYK @Daniel15 @rally25rs @zertosh @voxsim

alancnet avatar Jun 06 '19 18:06 alancnet

@alancnet sure, we always accept well-written and tested PRs

BYK avatar Jun 06 '19 19:06 BYK

My apologies @BYK, it seems this issue has been resolved in v1.17.0. It is still an issue in v1.16.0 which is what is published. I look forward to the next release.

image

alancnet avatar Jun 07 '19 16:06 alancnet

@alancnet don't really see anything that could have fixed this since 1.16.0: https://github.com/yarnpkg/yarn/compare/v1.16.0...master

Are you sure this is not something else?

BYK avatar Jun 07 '19 20:06 BYK

@BYK You're right. When you npm install yarn, the yarn shim is ignored, and a cmd-shim is created instead. The yarn shim has code that executes yarn with winpty. There is no functionality in npm to customize the shim.

Do you think it would be appropriate to add a postinstall script to replace the shim?

image

alancnet avatar Jun 10 '19 16:06 alancnet

@alancnet ooh, interesting find! I wonder why they diverge and I'd say this is a bug. So you have no issues when installing from npm but have issues when you install it from another place(which place btw, can you share)?

If so we should file a new bug to align them.

BYK avatar Jun 13 '19 20:06 BYK

@BYK The shim shown on the left of my screenshot is when installing from npm:

image

alancnet avatar Jun 17 '19 14:06 alancnet

image

lanseria avatar Jun 28 '19 01:06 lanseria

@alancnet sorry for the late response! I definitely remember hitting this issue. So when Yarn detects winpty it simply uses that without a way to turn off. I think there should be an option to turn it off or the automatic enabling should use a better test (not in git-bash for instance?).

Did some digging and seems like this behavior was introduced in #4577 to fix this exact issue and the code responsible lives in https://github.com/thetrompf/yarn/blob/master/bin/yarn

I'm reopening the issue. Would you like to try fixing this and submitting a PR to become the hero of all these people :) I am also using Windows and I'd be happy to assist you in the PR journey. Once it is merged, it'll be available in the nightly builds pretty quickly.

BYK avatar Jul 03 '19 11:07 BYK

A similar behavior around cmd-shim happens with yarn too: the main shim appears to launch "$basedir/../Data/global/node_modules/.bin/yarn", which is all good, but then you notice that the actual shim looks the same as npm's shim.

I don't think auto-using winpty is a bad idea. The current https://github.com/yarnpkg/yarn/blob/master/bin/yarn is doing it right by making sure it is trying to do winpty only under a cygwin tty. What needs to be changed is how yarn, or cmd-shim, handles the bin field. It needs to learn to search for an identically-named file without the js suffix and to recognize them as what the package author intended as the launcher script.

Artoria2e5 avatar Sep 06 '19 08:09 Artoria2e5

I'm also experiencing this on OS X 10.14.5

$ yarn -v
1.17.3
$ node -v
v10.15.3

rupurt avatar Sep 17 '19 18:09 rupurt

Looks like it's a permission problem. sudo yarn upgrade-interactive works for me.

rupurt avatar Sep 17 '19 18:09 rupurt

Has this re-emerged in 1.22?

image

Or I guess it was never fixed?

greenreign avatar Feb 14 '20 00:02 greenreign

Still not working on Windows with Git Bash.

$ yarn upgrade-interactive --latest
yarn upgrade-interactive v1.22.4
info Color legend :
 "<red>"    : Major Update backward-incompatible updates
 "<yellow>" : Minor Update backward-compatible features
 "<green>"  : Patch Update backward-compatible bug fixes
Done in 12.73s.
Error: Can't answer a question unless a user TTY

lekoaf avatar Mar 19 '20 10:03 lekoaf

Same problem

Leessonomy avatar Apr 09 '20 13:04 Leessonomy

Same problem

puncha avatar May 09 '20 02:05 puncha

Does prefixing yarn init with winpty make any difference?

E. g. winpty yarn init

lolmaus avatar May 09 '20 09:05 lolmaus

Usando o Windows10...com o git bash , ainda aparece esse mesmo erro . Minha versão do yarn v1.22.4 ao rodar yarn init no meu projeto apareceu:

" error An unexpected error occurred: "Can't answer a question unless a user TTY". info If you think this is a bug, please open a bug report with the information provided in "C:\curso-es6\yarn-error.log". info Visit https://yarnpkg.com/en/docs/cli/init for documentation about this command."

Mas usando o power shell modo administrador o yarn install funciona e gera o arquivo package.json

raffa-raffa avatar Aug 05 '20 02:08 raffa-raffa

Not sure if this thread is still relevant, in my case the fix was ensuring that my yarn version was set to yarn berry before I ran yarn init.

yarn set version berry

GuyARoss avatar Nov 05 '20 23:11 GuyARoss

Same fix for me as @GuyARoss

I had previously run yarn init v1.22.10 to run into this issue

KatieProchilo avatar Feb 04 '21 23:02 KatieProchilo

Not sure if this thread is still relevant but in my case, the fix was ensuring that my yarn version was set to yarn berry before I ran yarn init.

yarn set version berry

Worked for me initially. But then it was still giving error when I tried to require packages. Those who are looking a solution for this, just use your terminal in vs code for yarn init or the powershell. For me, yarn init was only giving error in my git bash

Pranav016 avatar Mar 03 '21 13:03 Pranav016

Running into this same error message and I found one solution.

I'm on Windows 10 and my project needs me to run yarn prepare which runs husky for... reasons. I was getting:

> yarn prepare
yarn run v1.22.10
$ husky install
node.exe : error Couldn't find the binary husky install

I tried the same command on:

  • Powershell --> Couldn't find the binary
  • Powershell ISE --> Couldn't find the binary
  • Git Bash --> Couldn't find the binary
  • Ubuntu 18.04 (WSL) --> worked.

I can't recall for sure but I think I installed yarn with Ubuntu 1-2 years ago. Maybe the other shells don't have the correct linkage or user permissions.

caqu avatar May 18 '21 15:05 caqu

Same problem Yarn Version: 1.22.7

LouieMartin avatar Jan 02 '22 04:01 LouieMartin

Didn't realize until I actually used git bash 🤣

LouieMartin avatar Jan 02 '22 04:01 LouieMartin

still the same problem, been 5 years or more. Latest yarn version on win 11 using gitbash :(

pabl0xf avatar Apr 04 '22 00:04 pabl0xf

Still happening but yarn init -y seems to work fine

JcynR avatar Apr 26 '22 08:04 JcynR

I think it doesn't work with git, I had the same problem but it started to work normally when I used powershell or command prompt.

xadxtya avatar Apr 26 '22 11:04 xadxtya