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

Output from Colorize Ruby gem doesn't appear colorized #5

Closed
toddmazierski opened this issue Mar 31, 2012 · 25 comments
Closed

Output from Colorize Ruby gem doesn't appear colorized #5

toddmazierski opened this issue Mar 31, 2012 · 25 comments

Comments

@toddmazierski
Copy link

Colored console output from the Colorize Ruby gem doesn't appear colorized in the Jenkins console.

In addition to the color code, the gem includes the mode and background color. Are these not supported?

"\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters( color_parameters )

Thanks!

@dblock
Copy link
Member

dblock commented Apr 1, 2012

Do you get other color output? For example from bundle install? Cause this looks like standard ANSI color scheme as far as I can see.

@toddmazierski
Copy link
Author

Hi @dblock,

Here's a way to reproduce the bug. If you have Ruby installed, please try this from the command line:

ruby -e "puts \"\033[31mthis appears red in Jenkins\033[0m\"" -e "puts \"\033[0;31;49mthis doesn't appear red in Jenkins\033[0m\""

Both lines should appear red in the console.

Next, try creating a fake Jenkins job and adding that command as a build step. You should observe that only the first line is red.

Thanks for your help!

@dblock
Copy link
Member

dblock commented Apr 4, 2012

The problem is in JANSI, I made a pull request - fusesource/jansi#7. Will be able to fix this after it gets merged.

@dblock
Copy link
Member

dblock commented Apr 4, 2012

Background colors are supported, the issue is around the default foreground (39) and background (49) values. If you don't have those, everything works.

@toddmazierski
Copy link
Author

Way cool, @dblock! Thanks for looking into this.

@dblock dblock closed this as completed Jun 4, 2012
@dblock
Copy link
Member

dblock commented Jun 4, 2012

Fixed in 0843782.

@toddmazierski
Copy link
Author

Awesome! Can't wait for the update in Jenkins. Thanks, @dblock!

@toddmazierski
Copy link
Author

Hi @dblock,

I just updated to 0.3.1 and unfortunately, I'm still not seeing color output from the Colorize gem. In addition, I can still reproduce the bug with the command I shared in an earlier comment. Here's a screenshot.

Thanks for your help, again!

@dblock
Copy link
Member

dblock commented Jun 8, 2012

I am going to guess the upgrade didn't work (maybe restart Jenkins?).
Here's what I see:

Check that the ANSI Color option has a colorize map option - that's how you know you got yourself a new version.

@toddmazierski
Copy link
Author

Hmm… I think the upgrade worked. Jenkins restarted after the plugin installation, but I tried again just to be sure.

Here's a screenshot of the "ANSI color map" function.

@dblock dblock reopened this Jun 8, 2012
@dblock
Copy link
Member

dblock commented Jun 8, 2012

Ok. I believe you. I tried on another Jenkins instance and got the same problem as you. And no problem on another. I think it has something to do with how update works - it's getting an older version of JANSI.

@dblock
Copy link
Member

dblock commented Jun 8, 2012

It works with an older version of Hudson (I am testing with 1.374 for backwards compatibility). Any Jenkins fails at this specific example. It looks like the bash commands are executed somewhat differently. I'll debug this soon.

Do you have any real colorize gem output in some rake task or something like that. Is that not working either?

@toddmazierski
Copy link
Author

You may be on to something — I discovered two JANSI jar files in my installation:

jenkins$ sudo find . -name jansi-1.*.jar
./Users/jenkins/.jenkins/plugins/ansicolor/WEB-INF/lib/jansi-1.9.jar
./Users/jenkins/.jenkins/war/WEB-INF/lib/jansi-1.6.jar

Any recommendations?

Yes, output from the colorize gem isn't working, either. Here's a little test that can be used:

ruby -r rubygems -e "require 'colorize'" -e "puts 'This doesn\'t appear red in Jenkins'.red"

Here's a screenshot of that command in my Jenkins (1.459).

@dblock
Copy link
Member

dblock commented Jun 11, 2012

Looking at Colorize code it checks whether STDOUT is a terminal or something else. Try this:

ruby -r rubygems -e "class IO; def isatty; true; end; end;" -e "require 'colorize'" -e "puts 'This doesn\'t appear red in Jenkins'.red"

Does this output red? (it does on mine)

@toddmazierski
Copy link
Author

It does not output red: http://cl.ly/161x2u2j2O331S0w2847

@toddmazierski
Copy link
Author

Great catch, though! Presumably, this is the line you're referring to.

I've confirmed that your overwriting of the IO#isatty method has the desired effect in Jenkins, though.

Started by user anonymous
Building in workspace /Users/jenkins/.jenkins/workspace/colortest
[colortest] $ /bin/sh -xe /var/folders/6t/bvst7hqd4zx8drn5c5jjymn00000gw/T/hudson6891404958282431780.sh
+ ruby -e 'puts STDOUT.isatty'
false
Finished: SUCCESS
Started by user anonymous
Building in workspace /Users/jenkins/.jenkins/workspace/colortest
[colortest] $ /bin/sh -xe /var/folders/6t/bvst7hqd4zx8drn5c5jjymn00000gw/T/hudson1353389529801506613.sh
+ ruby -e 'class IO; def isatty; true; end; end; puts STDOUT.isatty'
true
Finished: SUCCESS

@toddmazierski
Copy link
Author

I got color! Thanks for your help, @dblock. In summary, there appears to be two bugs happening at once:

Bug 1: the STDOUT.isatty conditional in the Colorize gem will prevent colorized output in Jenkins since STDOUT.isatty returns false:

Started by user anonymous
Building in workspace /Users/jenkins/.jenkins/workspace/colortest
[colortest] $ /bin/sh -xe /var/folders/6t/bvst7hqd4zx8drn5c5jjymn00000gw/T/hudson6891404958282431780.sh
+ ruby -e 'puts STDOUT.isatty'
false
Finished: SUCCESS

Workaround: overwrite the IO#isatty method to return true:

Started by user anonymous
Building in workspace /Users/jenkins/.jenkins/workspace/colortest
[colortest] $ /bin/sh -xe /var/folders/6t/bvst7hqd4zx8drn5c5jjymn00000gw/T/hudson1353389529801506613.sh
+ ruby -e 'class IO; def isatty; true; end; end; puts STDOUT.isatty'
true
Finished: SUCCESS

Permanent fix: Unsure — I'm assuming this was done so colorized output isn't mistakenly piped somewhere else where it would be disagreeable (ex. a log file on disk).

Bug 2: there are two versions of JANSI in my Jenkins installation (perhaps this is a Jenkins plugin upgrade/dependency management bug):

jenkins$ sudo find . -name jansi-1.*.jar
./Users/jenkins/.jenkins/plugins/ansicolor/WEB-INF/lib/jansi-1.9.jar
./Users/jenkins/.jenkins/war/WEB-INF/lib/jansi-1.6.jar

Workaround: remove/rename the older version and restart Jenkins:

jenkins$ mv -v .jenkins/war/WEB-INF/lib/jansi-1.6.jar .jenkins/war/WEB-INF/lib/jansi-1.6.jar.ignore
.jenkins/war/WEB-INF/lib/jansi-1.6.jar -> .jenkins/war/WEB-INF/lib/jansi-1.6.jar.ignore

Permanent fix: Unsure — any thoughts?

@dblock
Copy link
Member

dblock commented Jun 11, 2012

Alright, so the original problem is really that AnsiColor that uses the old JANSI 1.6 was not properly removed. Good we figured it out. I posted this in the Jenkins google group. We shall see ...

I think the Colorize gem needs a way to overwrite its behavior in a much cleaner way than a monkey patch. So I would fork and contribute something to it that lets me turn off isatty within a block, cleanly.

@toddmazierski
Copy link
Author

Just wanted to add a couple updates to this ticket:

  1. This fork of the Colorize gem has an overridable color_enabled attribute, here are the relevant changes and a new test
  2. With a fresh installation (as opposed to an upgrade) of AnsiColor 0.3.1, I still had to perform the renaming workaround I mentioned in my last comment. I wonder if Jenkins ships with JANSI 1.6?

Thanks again for your help!

@dblock
Copy link
Member

dblock commented Nov 29, 2012

Thanks @toddmazierski . I am not sure what the workitems out of this are - if you can find why you needed to do the renaming workaround, that'd be amazing - I never have to do this.

@toddmazierski
Copy link
Author

Good news, JANSI was just bumped up to 1.9 a couple days ago. I think it means sooner or later I won't have to apply the patch any longer.

@toddmazierski
Copy link
Author

Confirmed! This bug is now fixed in Jenkins 1.500.

@dblock
Copy link
Member

dblock commented Jan 28, 2013

Awesome!

@dothebart
Copy link

according to http://stackoverflow.com/questions/1401002/trick-an-application-into-thinking-its-stdin-is-interactive-not-a-pipe
the 'script' command can solve this for you. Imho it should be somewhere prominent on the howto ;-)

@dblock
Copy link
Member

dblock commented Mar 24, 2015

@dothebart Please contribute to the doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants