vercel / turborepo

Build system optimized for JavaScript and TypeScript, written in Rust
https://turbo.build/repo/docs
MIT License
26.33k stars 1.83k forks source link

Terminating TUI with ctrl+c on Windows causes terminal to act weird #8860

Open espenja opened 3 months ago

espenja commented 3 months ago

Verify canary release

Link to code that reproduces this issue

https://github.com/espenja/turbo-mouse-scroll-tui

What package manager are you using / does the bug impact?

pnpm

What operating system are you using?

Windows

Which canary version will you have in your reproduction?

2.0.9

Describe the Bug

Running turbo watch dev on Windows and cancelling the process with ctrl+c does not gracefully shut down the process and causes the terminal to act in weird ways.

This happens when running turbo from global install (shown in video 1 in additional context).
Running turbo from pnpm does not seem to cause this "effect" (shown in video 2 in additional context).

What happens after ctrl+c after running global turbo watch dev

Disclaimer: This is reported for 2.0.9 and not the canary release. Discussed this with @Zertsov on Discord and they said to report it anyway.

Tested with

Expected Behavior

Expected behavior: Process exits to previous terminal state without fragments from TUI, and mouse wheel scrolls the terminal buffer/window
Actual behavior: Terminal has lots of fragments from TUI, and mouse wheel acts like arrow up and down and scrolls through terminal command history

To Reproduce

Additional context

Screencap of what happens

Failure scenario: Screencap of running turbo as a global install and scrolling with mouse wheel after cancelling process with ctrl+c (clicking image will take you to a YouTube video showing what happens)

Running global turbo install with turbo watch dev

Working scenario: Screencap of running turbo from pnpm and scrolling with mouse wheel after cancelling process with ctrl+c (clicking image will take you to a YouTube video showing what happens)

Running global turbo install with turbo watch dev

Possibly related/similar bug reported in tmux repository

https://github.com/tmux/tmux/issues/2148

espenja commented 3 months ago

Additional information: After reporting issue #8861, I tested what I reported here in that reproduction branch too (which is a pure reproduction repo created with npx create-turbo@canary -e with-shell-command), and I am able to reproduce with 2.0.10-canary.0

WillKirkmanM commented 3 months ago

Hey @espenja, it seems turborepo needs to flush the terminal to remove the fragments similar to how the clear command works. In the meantime there are two workarounds you can use:

Use CTRL+L / clear

This is to manually flush the terminal of the outputs.

Use Old Logging Style

Turborepo pushed an update which changed the default TUI from the old one to this new modal / tmux type editor. To go back to the old one, add this flag to your run command

--log-order=stream 

A full command would look like

bunx turbo dev --log-order=stream --filter web --env-mode loose

image

espenja commented 3 months ago

While ctrl+L clears the terminal, and running the old stream experience is an ok workaround, I think the issue still stands with tui. Look at this example:

https://github.com/user-attachments/assets/9ab6e0a4-9186-419f-8e50-61626c5374cd

After having terminated the turbo process with ctrl+c the entire terminal is non-scrollable, even after clearing. The mouse scroll wheel acts like arrow up/down, the scrollbar for the terminal itself is gone, even the terminal cursor indicator is gone. This doesn't go away until you either completely close the terminal or start another app (in the screen cap I show how I can reset the terminal to the original state by starting vim and closing it again)

My (completely uneducated) guess after having looked inside the code for TUI (I don't know Rust at all) would be that the ctrl+c handler in TUI somehow either crashes before it can run the cleanup function, or doesn't run it at all: https://github.com/vercel/turbo/blob/496def6068938f517b19d004032a4a6e02aa6f32/crates/turborepo-ui/src/tui/app.rs#L454

Within this cleanup function, it looks like all the things described in my issue would have been resolved:

lucas-labs commented 3 months ago

In my case, after Ctrl+C, the terminal has no colors anymore.

image

espenja commented 3 months ago

I mentioned this in my original description as well, but another workaround is executing turbo from pnpm with pnpm exec turbo watch lint instead of global turbo turbo watch lint. Running with pnpm and pressing ctrl+c seems to exit the process just fine.

WillKirkmanM commented 3 months ago

What version is pnpm's turbo?

espenja commented 3 months ago

It's the same version as global. I've tested all 2.x major versions, same thing happens with all of them.