-1

I'm working on a project that requires the use of a test framework called Appium in Java. The Appium library is attempting to run the following script on my machine to find out where my NodeJS install is located (not important that it is node):

#!bin/sh
OUTPUT="$(npm root -g)"
echo "${OUTPUT}"
exit

This script is executed during my JUnit unit tests in Java explicitly with bash. See this link to see the code that Appium uses to run the script. On my system, if I execute this from a shell (using zsh, bash, or sh), I get the following output:

/home/me/node-v8.9.0-linux-x64/lib/node_modules

Great. For some reason, when that same script is running in the environment that is set up by my IDE (IntelliJ), this is the output:

/home/me/node-v8.9.0-linux-x64/lib/node_modules

[H[2J

Regardless of whether or not this was a good idea on the part of Appium, its breaking because of the extra \r\r\n[H[2J. I usually launch IntelliJ through a program called GnomeDO on Ubuntu MATE. After seeing that my $PATH in IntelliJ was different at run time than what I expected, I closed it out and launched it form my shell instead. The $PATH was right and the trailing output stopped.

What can be different about launching an application from my desktop environment in Linux and launching it from my shell that would lead to trailing non-readable characters?

  • 2
    Those characters look like the control character sequence for moving cursor to top left of a terminal window and clearing the screen. Try echo -e '\e[H\e[2J' from a shell and you'll see what I mean. Don't know if that will help identify the cause but... – B Layer Nov 05 '17 at 03:09
  • 1
    That should probably read "escape sequence" rather than "control character sequence" but you get my drift. https://en.wikipedia.org/wiki/Escape_sequence – B Layer Nov 05 '17 at 03:20
  • Where is that displaying? Intellij is opening a terminal window of some kind? Is it a new window or embedded in the IDE? I see a few mentions about some Gnome terminals not handling some escape sequences correctly so perhaps when launching with DO you get one of those terminals instead of one that handles them correctly. – B Layer Nov 05 '17 at 03:33
  • Based on the new information in your edit Gnome DO is not relevant at all. But parts of my answer still have relevance. I'd like to hear whether a different terminal is used when running the script directly vs when Intellij launches it. If they are the same then what happens when you execute the echo command I specified in that terminal? – B Layer Nov 05 '17 at 23:26
  • @BLayer As for "where is that displaying", all of this information came from debugging unit tests in IntelliJ's debugger. I had to actually step through line by line, see the output of the Appium shell execution saved as a Java variable, see .trim() do nothing because it didn't end in whitespace, etc. – Anthony Naddeo Nov 06 '17 at 02:10
  • But from within Intellij when that Appium routine runs it's not in a normal terminal. It's the Intellij console. Of course escape sequences won't work there. I guess Appium/Selenium is emitting the (normally perfectly legal) escape sequence but it's meaningless in Intellij console so it's just echoed literally (minus Escape which is non-printing). My first theory was too complex because I didn't fully understand the execution contexts/environments. – B Layer Nov 06 '17 at 03:24
  • Well, calling it a console might be generous. Its really using Java to execute bash and its inheriting the environment that IntelliJ was launched from. – Anthony Naddeo Nov 06 '17 at 06:34
  • Is there a .logout or .bash_logout file that’s being executed along the line? (And that contains a clear command) – Jeff Schaller Nov 06 '17 at 11:08
  • No there isn't. – Anthony Naddeo Nov 15 '17 at 21:35
  • Related: https://unix.stackexchange.com/questions/124762/how-does-clear-command-work – Kusalananda Feb 07 '18 at 19:48

1 Answers1

1

Note: I've been theorizing in the comments and this captures those thoughts and a HIGHLY speculative conclusion.

The funky characters look suspiciously like the escape sequence that moves the cursor to the top (top-left?) of a terminal and then clears the screen. You can see this in action by doing this in a shell: echo -e '\e[H\e[2J'.

I did a little searching and found a few references to Gnome terminals having incomplete support for escape sequences and for the problem sequences characters looking just like ours (e.g. [H) are displayed

Combining those two facts leads to a theory:

  1. Intellij emits some escape sequences upon opening a terminal window to clean the window up including \e[H\e[2J.
  2. Gnome DO sets up an environment that points to a particular terminal type and this is different from the terminal you use normally.
  3. Intellij launches this terminal when it is launched from DO, emits escape sequences, then runs your script.

Ah but why does the escape sequence come after the output of the script. That does not necessarily mean that it was printed after the script output was printed. There could be other escape sequences emitted by Intellij that worked and they could have moved the cursor in such a way to make the lines appear seemingly out of sequence.

Again this is extreme speculation made without having all the facts...but see if you can find a Gnome terminal on your system that doesn't handle the echo command above or otherwise check my guess.

B Layer
  • 5,171
  • The world wants you to clean your screen. Also, read that script carefully. (-: – JdeBP Nov 05 '17 at 09:58
  • @JdeBP Afraid I'm not catching your meaning...either with the linked answer or your comment about the script. The script isn't really important for my theory/guess. What's important is that some escape chars are being processed correctly in one case and not in the other case. If you've identified some important data points please clarify. – B Layer Nov 05 '17 at 13:16
  • I just updated the second paragraph with a link to the source code that Appium uses to execute the script. It happens each time I run a unit test so it isn't an IDE startup thing. They also explicitly execute the script with bash (despite having put the sh shebang...). I do find that escape sequence very interesting though, I didn't know that. That's the kind of thing I was hoping to learn from this post too so thank you. – Anthony Naddeo Nov 05 '17 at 16:55