fvm icon indicating copy to clipboard operation
fvm copied to clipboard

[Enhancement] No colors or formatting when using fvm flutter proxy

Open Albert221 opened this issue 3 years ago • 12 comments

Describe the bug

When running commands through fvm (like flutter test or flutter analyze) it cuts off the formatting like color and caret manipulation what makes the output much harder to read than without fvm.

This is especially annoying when I have big output or many tests and I can't see what tests failed because it's already way beyond my terminal scroll buffer.

fvm-error

➜  ~/Projects  dart --version
Dart SDK version: 2.12.0-133.2.beta (beta) (Tue Dec 15 09:55:09 2020 +0100) on "macos_x64"
➜  ~/Projects  fvm version
1.3.6

Additional context I love fvm!

Albert221 avatar Dec 30 '20 14:12 Albert221

@Albert221 Thanks, this is something that is actually on my backlog, however, I did not find an option to pass the formatting even though the standard stdout. I will do a bit more research on this.

leoafarias avatar Jan 04 '21 17:01 leoafarias

Do you think it can be due to this line? Specifically the last condition. I'm just guessing though. I may debug this sometime in the future if it's not this

https://github.com/leoafarias/fvm/blob/3effc16134f8c6459d21b38562928f51df684094/packages/cli/lib/src/flutter_tools/flutter_tools.dart#L46

Albert221 avatar Jan 08 '21 09:01 Albert221

I don't think so. This line is to solve the issue of having to press R and ENTER to reload Flutter for example. This is needed to have the same behavior as the Flutter CLI. Also, this piece is important because the order in which you change the properties actually causes issues.

leoafarias avatar Jan 08 '21 14:01 leoafarias

@Albert221 Not sure if you are using a mac or not, but one of the ways to work around this is to call the cached FVM directly.

For example I added the following to my .zshrc

alias f=".fvm/flutter_sdk/bin/flutter"

This will call the relative flutter for all projects you have fvm setup

The only downside is that it doesn't do look-up directories and also other fallbacks. But you have the same flutter experience.

leoafarias avatar Mar 28 '21 14:03 leoafarias

@Albert221 I believe I will be closing this issue. I had some time to dig a bit deeper and found the same behavior within flutter tools when calling pub commands.

Left: flutter pub get Right: pub get CleanShot 2021-04-23 at 10 01 21

leoafarias avatar Apr 23 '21 14:04 leoafarias

So I found something out today while running some fvm flutter pub outdated commands. There is a --color flag. As soon as I add this ( fvm flutter pub outdated --color) the output is colored.

Flutter adds this flag by default when running in a terminal but when running through fvm it can not automatically determine that it is running in a terminal.

    --[no-]color           Whether to color the output.
                                   Defaults to color when connected to a
                                   terminal, and no-color otherwise.

So I suggest that fvm should detect correctly if it is running in a terminal and add this flag to each command (not sure if all support this) unless --no-color is explicitly specified.

kuhnroyal avatar May 06 '21 11:05 kuhnroyal

@kuhnroyal very helpful info, will dig in to see what they are doing behind the scenes

leoafarias avatar May 06 '21 11:05 leoafarias

@kuhnroyal just ran a quick test. It seems the outdated command works for pub items. But passing the flag --color to fvm flutter test --color for example does not have the color output. However, I think this is enough to dig into what is happening.

leoafarias avatar May 06 '21 12:05 leoafarias

Maybe flutter pub get and flutter test print warnings/errors to stdErr, dunno. I can confirm that they accept the --color flag but no changes, to the output.

kuhnroyal avatar May 06 '21 13:05 kuhnroyal

Is this still in the works?

acoutts avatar Aug 05 '21 18:08 acoutts

@acoutts it's not in the works.. per se.. but I am keeping it open if we can gain a bit more insight on how to make this happen, but since this is happening on official tooling, I am not sure if it can be done correctly

leoafarias avatar Aug 05 '21 18:08 leoafarias

I did some digging and it looks like all coloring done by flutter (ie. not pub, since it is external) passes through this method: https://github.com/flutter/flutter/blob/abecef6ed36565d5d756da3d406555a4e6e70387/packages/flutter_tools/lib/src/base/terminal.dart#L249

Which decides whether to color or not based on this:

https://github.com/flutter/flutter/blob/abecef6ed36565d5d756da3d406555a4e6e70387/packages/flutter_tools/lib/src/base/terminal.dart#L190-L191

However _platform does not seem to have a way to be overridden. To overcome it you can just replace the getter to always return true and recompile flutter locally:

image

This works, but can have very nasty consequences if your terminal does not actually support colors. To fix this, the getter could be reading the NO_COLORS env var instead (https://no-color.org/). This of course seems too hacky to be implemented by fvm itself (however it would be easy to implement, just a simple search and replace), but if someone really wants to have colors you can follow the steps above. This has to be done "only" once per flutter version.

shilangyu avatar Oct 27 '21 07:10 shilangyu

I came across this issue because I have a problem where colors don't show up for me in tmux. I've traced it back to the dart sdk:

https://github.com/dart-lang/sdk/blob/7fa17936a686652081337d9e1a5209f69c368eec/sdk/lib/io/stdio.dart#L157-L177

So if, as they mention, you run the command with TERM set to xterm it should trigger color output. It works for me, I hope it's the same for you!

rorystephenson avatar Jan 04 '23 12:01 rorystephenson

I think the issue is that the dart / flutter tools try to find out if stdout is a terminal and supports ANSI colors. For this they use the property supportsAnsiEscapes here: https://github.com/dart-lang/sdk/blob/78a9f52544269816877a945c1ddea6e0bcb4b72f/sdk/lib/io/stdio.dart#L177 From the comment it seems that passing the TERM environment variable (which should already be present in the parent env) should do the trick. (like @rorystephenson said)

One place where the dart tools use the mentioned property: https://github.com/dart-lang/sdk/blob/d9482e35ce75b0b03e78bc1ce6fed39018e50c00/pkg/_fe_analyzer_shared/lib/src/util/colors.dart#L110

I stumbled across a "problem" with fvm similar to this because checks for hasTerminal in my console application work if executed directly with dart but don't work if executed with fvm dart

devmil avatar Feb 15 '23 20:02 devmil