Sublimerepl getenv error

I would like to use the SiblimeREPL package with Sublime Text. When I try to run REPL, I get

SublimeREPL: obtaining sane environment failed in getenv() Check console and 'getenv_command' setting WARN: Falling back to SublimeText environment 

This happens no matter which REPL I'm trying to run. (I tried Ruby, Python, and Clojure.) I tried Sublime Text 2 and Sublime Text 3 with the same results. This applies to Mac OS X, if that matters.

I looked in the package settings where I see

 "getenv_command": ["/bin/bash", "--login", "-c", "env"], 

If I run "/ bin / bash --login -c env" at the Terminal prompt, I can specify my environment.

What do I need to change to get a successful getenv_command command?

+7
clojure sublimetext2 sublimetext sublimetext3
source share
7 answers

I had the same problem as ssgam. The problematic line for me is in the getenv method. It calls subprocess.check_output (getenv_command) , which does not exist in python 2.6, which apparently uses ST2.

The trick is that it calls subprocess.check_output () calls if getenv_command is true and os.environ.copy () is used by default. To get the ssgam fix without changing the SublimeREPL package, in Preferences> Package Settings> SublimeREPL> Settings - User, follow these steps:

 { "getenv_command": false } 
+15
source share

I have explored this question a bit deeper and it seems that SublimeText 3 is also affected. In my case, the problem is related to the bash -completion function, in particular the COMP_WORDBREAKS environment variable.

Use the following command to display the contents of COMP_WORDBREAKS:

 $ echo "$COMP_WORDBREAKS" 

displays

 "'><=;|&(: 

You can also use:

 $ echo $COMP_WORDBREAKS 

but note that with the second command (without quotes) you will not see that the variable also contains a line feed character.

The problem here is the line symbol that breaks the parsing of the output in the getenv_command function. If you are extracting a portion of the source code for SublimeREPL, you may receive a real error message from the python interpreter.

 Traceback (most recent call last): File "main.py", line 71, in getenv env = dict(line.split('=', 1) for line in lines) ValueError: dictionary update sequence element #6 has length 1; 2 is required 

You can map item # 6 to position COMP_WORDBREAKS in the env list.

Solution (the first thing that occurred to me)

At the moment, I can’t say what really affects the bash -completion function after applying the following solution, and, of course, SublimeREPL should be fixed accordingly. Please comment on my answer to fill in the missing knowledge.

We can remove the disturbing characters to get rid of the error. First we define these characters

 $ echo -n "${COMP_WORDBREAKS}" | od -t x1c 

displays

 0000000 20 09 0a 22 27 3e 3c 3b 7c 26 28 3a \t \n " ' > < ; | & ( : 0000014 

so we have three to delete. The easiest way is to add the following line to your .bashrc :

 COMP_WORDBREAKS="${COMP_WORDBREAKS#???}" 

Voila! More error messages.

My last thought on the removed characters. I don’t quite understand how bash -completion works, and I know that changing COMP_WORDBREAKS can affect other scripts that use it. For now, you can always change it ad-hoc.

Hope this helps.

Greetings

+6
source share

Found. I fixed it. SublimeREPL assumes that running getenv_command will produce SOLELY output from running env, and each line will contain an equal sign. But my .bash_profile retells some things in stdout.

The solution was to complete my .bash_profile's output in

 if [[ $- == *i* ]] 

so as not to produce an additional result, except for the executed command.

+3
source share

TL; DR;

Replace:

 env = dict(line.split('=', 1) for line in lines) 

in ~/.config/sublime-text-3/Packages/SublimeREPL/repls/subprocess_repl.py with

 env = dict(line.split('=', 1) for line in lines if '=' in line) 

(Thanks @MichaelOhlrogge for the shorter syntax)


Why does it work

@Develucas's solution helped me solve my problem. I did not have the problem that he described, but his research helped.

In my case, there was a greeting in the login shell. So, bash --login -c env (the command specified in the SublimeREPL.sublime-settings file in the getenv_command parameter) printed something like this:

 Hello, parth! USER=parth SHELL=/bin/bash . . . 

It turns out that SublimeREPL uses the output of this command to load environment variables - as indicated in the comment above the getenv_command parameter:

 // On POSIX system SublimeText launched from GUI does not inherit // a proper environment. Often leading to problems with finding interpreters // or not using the ones affected by changes in ~/.profile / *rc files // This command is used as a workaround, it launched before any subprocess // repl starts and it output is parsed as an environment "getenv_command": ["/bin/bash", "--login", "-c", "env"], 

The code analyzing this output is similar to this (in the ~/.config/sublime-text-3/Packages/SublimeREPL/repls/subprocess_repl.py for ST3):

 def getenv(self, settings): """Tries to get most appropriate environent, on windows it os.environ.copy, but on other system we'll try get values from login shell""" getenv_command = settings.get("getenv_command") if getenv_command and POSIX: try: output = subprocess.check_output(getenv_command) lines = output.decode("utf-8", errors="replace").splitlines() env = dict(line.split('=', 1) for line in lines) return env except: import traceback traceback.print_exc() error_message( "SublimeREPL: obtaining sane environment failed in getenv()\n" "Check console and 'getenv_command' setting \n" "WARN: Falling back to SublimeText environment") # Fallback to environ.copy() if not on POSIX or sane getenv failed return os.environ.copy() 

The line env = dict(line.split('=', 1) for line in lines) causes a problem because the first line in the output of bash --login -c env does not have = . Therefore, I changed this line to ignore lines that are not = :

env = dict(line.split('=', 1) for line in lines if '=' in line)

And that solved the problem for me. Remember to restart Sublime Text after modifying this file.

+2
source share

changing COMP_WORDBREAKS does not work for me ... I am using ST2 and the exception was thrown in check_output ().

it also completes the name completion on the command line after changing COMP_WORDBREAKS.

in my case, I changed the subprocess_repl.py env () method:

 [wind]$ diff subprocess_repl.py.20151117.173317 subprocess_repl.py 160c160,161 < updated_env = env if env else self.getenv(settings) --- > # updated_env = env if env else self.getenv(settings) > updated_env = env if env else os.environ.copy() [wind]$ 

it would be interesting to know why the problem began to appear suddenly ...

Hth, cheers,

Sam

+1
source share

The answer from @develucas basically works for me with ST3 with OSX El Capitan, except that I had to put

export COMP_WORDBREAKS="${COMP_WORDBREAKS#???}"

Pay attention to export. However, if I do this, tab completion no longer works.

+1
source share

I had the same problem: my .bash_profile had some service outputs, such as a greeting message, etc.

These outputs are processed by SublimeREPL to try to extract environment variables from the output of the env command, and mixed mixed text strings provoked an error.

(I would like to do PR for SublimeREPL in order to try to make this phase more reliable, it should not depend on specific implementations of .bash_profile !)

+1
source share

All Articles