Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git bash for windows in terminal does not break lines correctly #9359

Open
RedX2501 opened this issue Mar 3, 2021 · 28 comments
Open

git bash for windows in terminal does not break lines correctly #9359

RedX2501 opened this issue Mar 3, 2021 · 28 comments
Labels
Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) Product-Terminal The new Windows Terminal.
Milestone

Comments

@RedX2501
Copy link

RedX2501 commented Mar 3, 2021

Environment

Platform ServicePack Version      VersionString
-------- ----------- -------      -------------
 Win32NT             10.0.18363.0 Microsoft Windows NT 10.0.18363.0

Windows Terminal Preview
Version: 1.7.572.0

Setting for git bash:

            {
                "name": "git bash",
                "commandline": "%PROGRAMFILES%/git/usr/bin/bash.exe -i -l",
                "icon": "%PROGRAMFILES%/Git/mingw64/share/git/git-for-windows.ico",
                "startingDirectory" : "%USERPROFILE%"
            },

Steps to reproduce

  1. Open a terminal window with git for windows bash
  2. Do a rebase operation

Expected behavior

Line breaks start at the beginning of the next line.

Actual behavior

git rod is an alias for git rebase --autostash -i origin/develop

$git rod
error: could not apply 6b04f92e... DuplicateInput now supports generating code for arrays for the VfcTypeImplementation
                                                                                                                                  Resolve all conflicts manually, mark them as resolved with
                                                                                                                                                                                            "git add/rm <conflicted_files>", then run "git rebase --continue".
                                                                                                                                                                                                                                                              You can instead skip this commit: run "git rebase --skip".
                                           To abort and get back to the state before "git rebase", run "git rebase --abort".
                                                                                                                            Recorded preimage for 'src/App/Changelog.md'
                                                                                                                                                                        Could not apply 6b04f92e... DuplicateInput now supports generating code for arrays for the VfcTypeImplementation
                      Auto-merging src/App/Tests/Silt.Plugin.SimpleTransformersGeneratorTests/GeneratorTests/DuplicateInputTransformerTests.cs
                                                                                                                                              Auto-merging src/App/Plugins/Silt.Plugin.SimpleTransformersGenerator/GeneratorStrategy/DuplicateInputTransformer.cs
                                                                                                                                                                                                                                                                 Auto-merging src/App/Changelog.md
                     CONFLICT (content): Merge conflict in src/App/Changelog.md

It seems that only the console without \r is not returning the operator to the beginnig of the line.

Not sure if there is a setting I need to configure for this to work.

@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Mar 3, 2021
@zadjii-msft
Copy link
Member

@DHowett can you sanity check what's going on here? I forget how mingw works some times. We've had this one and #9312 in the course of a single week, and I don't think we changed anything about our console mode handling.

@zadjii-msft zadjii-msft added the Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) label Mar 3, 2021
@RedX2501
Copy link
Author

RedX2501 commented Mar 4, 2021

Forgot to mention I saw this on preview. I have not checked on the "official" version yet. I have added the version number to the post above.

@RedX2501
Copy link
Author

RedX2501 commented Mar 4, 2021

I also see this in the official version

Windows Terminal
Version: 1.6.10571.0
$git rebase -i develop
Successfully rebased and updated refs/heads/testing.
                                                    myPc /c/data/sw testing

@RedX2501
Copy link
Author

RedX2501 commented Mar 5, 2021

Today this led to a buffer overrun exception:

image001

@zadjii-msft
Copy link
Member

Well that's super weird. I don't think that's necessarily caused by the same thing that's causing the \r to get dropped here. We've seen that before in #7763, but that had more to do with resizing the buffer with DBCS characters in it, and presumably that's not happening in this case.

If that happens again, can you use task manager to capture a dump file of Terminal when that dialog pops up?

@ffes
Copy link

ffes commented Jul 9, 2021

I see this behavior with every git commit I do. Other git subcommands seem to work for me, though I haven't used rebase recently. Never ran into the buffer overrun, mentioned above.

FYI:
Windows Terminal Preview Version: 1.9.1523.0
git version 2.31.1.windows.1

@KalleOlaviNiemitalo
Copy link

Does stty onlcr help?

@KalleOlaviNiemitalo
Copy link

Regarding the error message:

The system detected an overrun of a stack-based buffer in the application. This overrun could potentially allow a malicious user to gain control of this application.

As described in STATUS_STACK_BUFFER_OVERRUN doesn’t mean that there was a stack buffer overrun, the same exception code is nowadays used for other purposes too. I don't know whether Windows nowadays bothers to check the fast-fail code before it pops up the message box with this wording.

@zadjii-msft zadjii-msft added Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) Product-Terminal The new Windows Terminal. labels Sep 15, 2021
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Sep 15, 2021
@zadjii-msft zadjii-msft added this to the Terminal v2.0 milestone Sep 15, 2021
@poke
Copy link

poke commented Sep 16, 2021

To add what we know from the other issue #10152, can you check your TERM environment variable when you experience the formatting issue or even the crash? In my situation, it depends on what value it has. Apparently, Git Bash defaults to xterm, and I personally hit these issues with the value msys when using PowerShell.

Terminal definitely behaves different here from other console emulators like the default conhost, mintty or ConEmu.

@ffes
Copy link

ffes commented Sep 20, 2021

In my case in Windows Terminal Preview v1.11.2421.0:

$ echo $TERM
xterm-256color

When I run "Git Bash" from the Start Menu (which uses mintty):

$ echo $TERM
xterm

@RedX2501
Copy link
Author

Both are xterm-256color for me.

@KalleOlaviNiemitalo
Copy link

How about the stty command?

On Linux, Git would output just a LF, the Linux tty driver would convert that to CR LF, and the terminal would move the cursor to the start of the line on CR and then to the next line on LF. This behaviour of the terminal driver can be enabled or disabled with the ONLCR flag in the tcsetattr function, or with the stty program.

On Windows, the console host does not convert LF to CR LF, so I assume this is instead done by some emulation layer in the Git for Windows package. If the emulated ONLCR flag has become turned off (e.g. because an editor such as vi turned it off and then was killed before it was able to turn the flag back on), that could cause the symptom described here. In this situation, stty onlcr or stty sane would turn the flag back on and make the output look OK again.

@j4james
Copy link
Collaborator

j4james commented Sep 30, 2021

FYI, on Windows this behaviour is controlled by the DISABLE_NEWLINE_AUTO_RETURN output mode. So maybe someone is setting that mode and not restoring it on exit. If that's the problem, it could potentially be fixed by #4954.

@KalleOlaviNiemitalo
Copy link

I was mislead by MicrosoftDocs/Console-Docs#132.

Anyway, if I run stty -onlcr in Git Bash on Windows Terminal Preview 1.11.2421.0, and then run git --help | cat, it produces the ugly output. If I then start cmd in the same pane and run type to display a file that uses only LFs as line terminators, each line starts at the left margin. After that, if I exit from cmd to Git Bash and run git --help | cat again, the output is still ugly. This makes me believe the ONLCR flag is being emulated by MSYS (which is used by stty and cat but not by git) rather than mapped to DISABLE_NEWLINE_AUTO_RETURN.

@carenas
Copy link

carenas commented Sep 30, 2021

So maybe someone is setting that mode and not restoring it on exit.

Correct; vim takes an output console from git with ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT adds DISABLE_NEWLINE_AUTO_RETURN | ENABLE_VIRTUAL_TERMINAL_PROCESSING and returns it dirty (even if it doesn't crash AFAIK).

adding functionality in Git for Windows that resets the terminal after vi returns workarounds the issue and will be present in a future release

carenas added a commit to carenas/git that referenced this issue Sep 30, 2021
Obviously a hack, as this needs to be done properly in compat/terminal
but as a workaround to issue microsoft/terminal#9359 that is caused
by some misbehaving vim (msys, included with Git for Windows)

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/git that referenced this issue Sep 30, 2021
When EDITOR is invoked to modify a commit message, it will likely
change the console settings, and if it misbehaves, will leave the
console output as shown in microsoft/terminal#9359.

Use the recently added {push,pop}_term() functions to save the
terminal state and recover safely, instead.

[1] microsoft/terminal#9359

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/git that referenced this issue Sep 30, 2021
When EDITOR is invoked to modify a commit message, it will likely
change the console settings, and if it misbehaves, will leave the
console output as shown in microsoft/terminal#9359.

Use the recently added {push,pop}_term() functions to save the
terminal state and recover safely, instead.

[1] microsoft/terminal#9359

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/git that referenced this issue Oct 2, 2021
When EDITOR is invoked to modify a commit message, it will likely
change the terminal settings, and if it misbehaves will leave the
terminal output as shown in microsoft/terminal#9359.

Instead use the recently added {push,pop}_term() functions to save
the terminal configuration and recover safely.

[1] microsoft/terminal#9359

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/git that referenced this issue Oct 2, 2021
When EDITOR is invoked to modify a commit message, it will likely
change the terminal settings, and if it misbehaves will leave the
terminal output as shown in microsoft/terminal#9359.

Instead use the recently added {push,pop}_term() functions to save
the terminal configuration and recover safely.

[1] microsoft/terminal#9359

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/git that referenced this issue Oct 4, 2021
When EDITOR is invoked to modify a commit message, it will likely
change the terminal settings, and if it misbehaves will leave the
terminal output damaged as shown in a recent report from Windows
Terminal[1]

Instead use the functions provided by compat/terminal to save the
settings and recover safely.

[1] microsoft/terminal#9359

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
@carenas
Copy link

carenas commented Oct 30, 2021

FWIW the workaround from git to reset the terminal settings after vi is invoked has been merged and is released as part of the last Git for Windows prerelease:

https://github.com/git-for-windows/git/releases/tag/v2.34.0-rc0.windows.1

This should avoid the most egregious cases until a final solution to keep them consistent is found

dscho added a commit to git-for-windows/build-extra that referenced this issue Nov 2, 2021
Git now works around [an issue with `vi`
and incorrect line breaks in the Windows
Terminal](microsoft/terminal#9359).

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
@ghost
Copy link

ghost commented Nov 18, 2021

image
Can some help, gitbash terminal break in middle when i clear the old log then the new log start in the bottom.

carenas added a commit to carenas/git that referenced this issue Nov 24, 2021
When EDITOR is invoked to modify a commit message, it will likely
change the terminal settings, and if it misbehaves will leave the
terminal output damaged as shown in recent reports from Windows
Terminal[1]

Instead use the functions provided by compat/terminal to save the
settings of the terminal and recover safely, but only do so if
instructed by the user through a new configuration "editor.rogue"
and the terminal is fully interactive.

[1] microsoft/terminal#9359

Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/git that referenced this issue Nov 25, 2021
When EDITOR is invoked to modify a commit message, it will likely
change the terminal settings, and if it misbehaves will leave the
terminal output damaged as shown in recent reports from Windows
Terminal[1]

Instead use the functions provided by compat/terminal to save the
settings of the terminal and recover safely, but only do so if
instructed by the user through a new configuration "editor.rogue"
and the terminal is fully interactive.

[1] microsoft/terminal#9359

Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/build-extra that referenced this issue Nov 25, 2021
Proactively activate the rogue.editor configuration option if the
user persists on using VIM even after being warned against it, to
avoid the possibility of terminal output corruption like the one
described in microsoft/terminal#9359.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/build-extra that referenced this issue Nov 25, 2021
Proactively activate the rogue.editor configuration option if the
user persists on using VIM even after being warned against it, to
avoid the possibility of terminal output corruption like the one
described in microsoft/terminal#9359.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/build-extra that referenced this issue Nov 25, 2021
Proactively activate the rogue.editor configuration option if the
user persists on using VIM even after being warned against it, to
avoid the possibility of terminal output corruption like the one
described in microsoft/terminal#9359.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
carenas added a commit to carenas/build-extra that referenced this issue Nov 25, 2021
Proactively activate the rogue.editor configuration option if the
user persists on using VIM even after being warned against it, to
avoid the possibility of terminal output corruption like the one
described in microsoft/terminal#9359.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
@carenas
Copy link

carenas commented Nov 25, 2021

FYI, I made a fatal mistake on the way this workaround was implemented originally within git and therefore the code had to be reverted for 2.34.1, but luckily the Git for Windows maintainer picked it up and might still work when using 2.34.1.windows.1 (or later).

If you are having problems, make sure to NOT have any of the environment variables used to override the editor defined in your environment (ex: VISUAL, EDITOR, GIT_EDITOR, GIT_SEQUENCE_EDITOR) AND NOT have a core.editor setting, OR that they are set exactly to 'vi' or 'vim' ('vim.exe' or '/usr/bin/vi' will not activate the workaround and still corrupt your terminal).

carenas added a commit to carenas/git that referenced this issue Dec 1, 2021
When EDITOR is invoked to modify a commit message, it will likely
change the terminal settings, and if it misbehaves will leave the
terminal output damaged as shown in recent reports from Windows
Terminal[1]

Instead use the functions provided by compat/terminal to save the
settings of the terminal and recover safely, but only do so if
instructed by the user through a new configuration "editor.rogue"
and the terminal is fully interactive.

[1] microsoft/terminal#9359

Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
gitster added a commit to git/git that referenced this issue Dec 2, 2021
When EDITOR is invoked to modify a commit message (or do some
other editing), and it is a terminal mode editor, it will need to
change the terminal settings, and if it misbehaves could leave the
terminal output damaged as shown in recent reports from Windows
Terminal[1]

Instead use the functions provided by compat/terminal to save the
settings of the terminal and recover safely, but only do so if
the editor is known to have issues and unless the user has told
us through a boolean configuration "editor.stty" that it is safe
not to do so.

[1] microsoft/terminal#9359

Signed-off-by: Junio C Hamano <gitster@pobox.com>
@zadjii-msft zadjii-msft modified the milestones: Terminal v2.0, 22H2 Jan 4, 2022
@C0DK
Copy link

C0DK commented Feb 18, 2022

I am experiencing something similar with WSL 2.0 and Ubuntu in the Windows Terminal, with git version 2.25.1 - yet that shouldn't be git for windows; but just plain old git:

image

It happens after having run an interactive rebase (which I assume is also when vim is opened.). I've set the editor to vim, (as per @carenas suggestion) rather than the full vim-path, yet it didn't do anything better or worse.

Is this related to the problem? and do you brilliant people know of any quick fix?

@carenas
Copy link

carenas commented Feb 18, 2022

Is this related to the problem? and do you brilliant people know of any quick fix?

the mentioned workaround only applies to git for windows (requires code in git that is not part of the regular codebase yet).

it definitely looks like the same problem, and as documented in #10152 another workaround that doesn't require code changes is to set TERM to something else than xterm*; ex:

> export TERM=vt100

it is important also to note that running reset after vi is invoked should also fix the terminal, as well as fixing your rbim command/alias so it doesn't attempt to run vi in a non terminal, if that is possible (expect might help there)

@C0DK
Copy link

C0DK commented May 16, 2022

I never tested vt100, @carenas - i did now.

image

vt100 didn't do much more than removing my beautiful colors. Reset is currently my preferred trick. but might just postfix the reset command to my alias.

@carenas
Copy link

carenas commented May 16, 2022

vt100 didn't do much more than removing my beautiful colors

will see if I can try again to get my fix merged into git upstream (hopefully for 2.37), which then could be maybe backported to your version; but considering your version is old and probably out of maintenance it might be worth upgrading it regardless.

@zadjii-msft
Copy link
Member

@carenas You know if this was ever fixed upstream? Or, if it wasn't, is there an upstream issue we could point this thread at?

@carenas
Copy link

carenas commented Nov 8, 2022

You know if this was ever fixed upstream?

AFAIK the "fix" implemented in git for windows[1] is only a workaround to the effects of this bug.

[1] git-for-windows/git@db157fb

@othmanalikhan
Copy link

othmanalikhan commented Feb 11, 2023

I'm experiencing a similar situation where Git Bash for Windows messed up line wrapping (and consequently doesn't break lines properly). To replicate:

  1. Open Git Bash session in Windows Terminal
  2. Just press up-arrow to load a command from history
  3. Move the cursor to the start of the line of the command that appears, and that becomes rather wonky (especially for long commands that should be line broken).

See this image to illustrate:

image

I should add that changing the $TERM variable can sometimes fix it but sometimes makes things even more wonkier.

EDIT: Nevermind, found my issue and it isn't caused by Windows Terminal... I had a function in my .bashrc that was overriding the $PS1 variable incorrectly. Fixed it and confirm everything is working. This issue was plaguing me for about a year now... But no longer.

@carlos-zamora carlos-zamora modified the milestones: 22H2, Backlog Feb 22, 2023
@carlos-zamora
Copy link
Member

Hi all! There's been a lot of changes in this space recently, and we're hoping it gets better with 1.18 which is slated to come out in a few months.

@carlos-zamora carlos-zamora removed the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label Feb 22, 2023
@lost-scripts
Copy link

FWIW, in my case what triggers the issue is this line...

export MSYS=winsymlinks:nativestrict # Allow symlink creation on Windows perpetually

...that I added at some point to my .bashrc file. IOW, I've just discovered that if I comment that line and relaunch the terminal I get the expected output:

20240702-2204_correct

Whereas as soon I uncomment it and relaunch the terminal, I start getting this again:

20240702-2206_wrong

Curious thing is for some reason the behavior only changes after the second interaction once activated/deactivated the culprit line (no matter if I relaunch or reload), as can be seen in the first capture. Well, I hope it somehow helps, or give some clue at least, on figuring out what could be going on cause it's certainly annoying... and what if (as happen to be the case) you need such or other similar settings to be active? 🙄 Cheers.


git version 2.45.1.windows.1
GNU bash, version 5.2.26(1)-release (x86_64-pc-msys)

@DHowett
Copy link
Member

DHowett commented Jul 2, 2024

If toggling an msys feature on and off changes the behaviour of msys, it may be worth filing a bug on msys! I'm not sure how that would impact Terminal, but it is a really great data point. Thanks!

FWIW: it looks like that terminal is mintty, so we might not even be involved here (since msys applications use a special connection they have with mintty to skip all the Windows Console code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Output Related to output processing (inserting text into buffer, retrieving buffer text, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-2 A description (P2) Product-Terminal The new Windows Terminal.
Projects
None yet
Development

No branches or pull requests