Giter Club home page Giter Club logo

xontrib-langenv's Introduction

xontrib-langenv

xonsh integration with:

This xontrib replaces the slow langenv initialization with a faster python version (and skips the rehash step), which could save up to ~0.5s for each lang

The only two exceptions are:

so if you rewrite that goenv env var setting and pyenv virtualenv init logic in python and xontribute to this xontrib, you could eliminate the last sources of xonsh langenv startup delay!

Install

Install using pip

pip install xontrib-langenv

Usage

Add to your .xonshrc as follows:

Pyenv

xontrib load pyenv

This xontrib initializes pyenv when xonsh is started. After initialization pyenv commands works as they would do in any classic shell.

Also supports pyenv-virtualenv.

Nodenv

xontrib load nodenv

This xontrib initializes nodenv when xonsh is started. After initialization nodenv commands works as they would do in any classic shell.

Goenv

xontrib load goenv

This xontrib initializes goenv when xonsh is started. After initialization goenv commands works as they would do in any classic shell.

Rbenv

xontrib load rbenv

This xontrib initializes rbenv when xonsh is started. After initialization rbenv commands works as they would do in any classic shell.

Compatibility notes

If you are using xonsh v0.11 (or older) and you have issues with the latest version of this xontrib, try to downgrade it to version 1.0.6.

xontrib-langenv's People

Contributors

drmikecrowe avatar dyuri avatar eugenesvk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

xontrib-langenv's Issues

Improve startup speed by skipping all bash calls (set environment/alias directly)

(check this fork https://github.com/eugenesvk/xontrib-langenv/tree/repy_skip_bash
it further improves the branch mentioned in #13)

Given that all the langenv tools are the same and only need two special 'rehash','shell' alias commands it's better to just set the alias directly in xonsh instead of calling those slow bash shells to get this information

Also, the two /shims path and PYENV_SHELL can be set directly as we know what the output for our shell will be and the shims are at the PYENV_ROOT or a known default

So I've adjusted the base/pyenv functions to completely avoid calling bash in the init stage with a good startup speedup due to this. Now bash is only called when you invoke the two sh- commands

Error when pyenv xontrib loaded

The error when starting shell:

usage: source-bash [-h] [-n] [-l] [--envcmd ENVCMD] [--aliascmd ALIASCMD]
                   [--extra-args EXTRA_ARGS] [-u] [-p PREVCMD]
                   [--postcmd POSTCMD] [--funcscmd FUNCSCMD]
                   [--sourcer SOURCER] [--use-tmpfile]
                   [--seterrprevcmd SETERRPREVCMD]
                   [--seterrpostcmd SETERRPOSTCMD] [--overwrite-aliases]
                   [--suppress-skip-message] [--show] [-d]
                   files_or_code [files_or_code ...]

source-bash: error: argument -l/--login: ignored explicit argument 'false'

Long form traceback

Traceback (most recent call last):
  File "/home/mike/.xonshrc", line 101, in <module>
    xontrib load coreutils distributed output_search docker_tabcomplete autovox pyenv # avox_poetry
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/built_ins.py", line 196, in subproc_captured_hiddenobject
    return xonsh.procs.specs.run_subproc(cmds, captured="hiddenobject", envs=envs)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 3579, in run_subproc
    command.end()
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 928, in end
    self._end(tee_output=tee_output)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 936, in _end
    for _ in self.tee_stdout():
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 838, in tee_stdout
    for line in self.iterraw():
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 725, in iterraw
    proc.wait()
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 2626, in wait
    r = self.f(self.args, stdin, stdout, stderr, spec, spec.stack)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/__amalgam__.py", line 767, in __call__
    result = dispatch(
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/__amalgam__.py", line 516, in dispatch
    return _dispatch_func(func, ns)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/__amalgam__.py", line 489, in _dispatch_func
    return func(**kwargs)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/__amalgam__.py", line 12521, in xontribs_load
    update_context(name, ctx=ctx)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/__amalgam__.py", line 12475, in update_context
    modctx = xontrib_context(name)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/__amalgam__.py", line 12440, in xontrib_context
    m = importlib.import_module(spec.name)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xontrib/pyenv.xsh", line 39, in <module>
    source-bash --login=false --interactive=false --suppress-skip-message @(PYENV_PATH) e>/dev/null
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/built_ins.py", line 196, in subproc_captured_hiddenobject
    return xonsh.procs.specs.run_subproc(cmds, captured="hiddenobject", envs=envs)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 3579, in run_subproc
    command.end()
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 928, in end
    self._end(tee_output=tee_output)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 936, in _end
    for _ in self.tee_stdout():
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 838, in tee_stdout
    for line in self.iterraw():
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 725, in iterraw
    proc.wait()
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/procs/__amalgam__.py", line 2626, in wait
    r = self.f(self.args, stdin, stdout, stderr, spec, spec.stack)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/__amalgam__.py", line 767, in __call__
    result = dispatch(
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/site-packages/xonsh/__amalgam__.py", line 511, in dispatch
    parsed = parser.parse_args(args)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/argparse.py", line 1818, in parse_args
    args, argv = self.parse_known_args(args, namespace)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/argparse.py", line 1854, in parse_known_args
    self.error(str(err))
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/argparse.py", line 2575, in error
    self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/argparse.py", line 2562, in exit
    _sys.exit(status)
SystemExit: 2
Traceback (most recent call last):
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/argparse.py", line 1851, in parse_known_args
    namespace, args = self._parse_known_args(args, namespace)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/argparse.py", line 2060, in _parse_known_args
    start_index = consume_optional(start_index)
  File "/home/mike/.pyenv/versions/3.9.6/lib/python3.9/argparse.py", line 1982, in consume_optional
    raise ArgumentError(action, msg % explicit_arg)
argparse.ArgumentError: argument -l/--login: ignored explicit argument 'false'

My xonshrc:
https://github.com/meramsey/yadm-dotfiles/blob/main/.xonshrc

@ xonsh --version
xonsh/0.12.1
~ at 08:33:50
 
@

I was on xonsh v11 before with same file. I also uninstalled the legacy one and reinstalled the new one as outlined in
https://github.com/dyuri/xontrib-pyenv

Thanks in advance for your time.

Remove pyenv duplicate command and add no-rehash

While checking why pyenv init is so slow, I've realized that your xontrib runs it twice for no reason since the full init already provides PATH

PYENV_PATH = $(@(PYENV) init --path)  # this is not needed since โ†“ already includes `export PATH`
PYENV_ENV = $(@(PYENV) init -)

Also there is another trick to speed it up via pyenv init - --no-rehash instead of the regular init
pyenv/pyenv#1970

Error if pyenv-virtualenv not installed

If pyenv-virtualenv is not installed, I get:

xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
subprocess.CalledProcessError: Command '['bash', '-c', 'source "/home/pierre/.bashrc"\npyenv virtualenv-init -\n']' returned non-zero exit status 1.

With Ubuntu 22.04 I can't use this if I load pyenv

I tried installing this in my latest ubuntu 22.04 and I added it in .xonshrc file.

Now if I open the terminal it immediately closes within a blink of an eye.

However, I managed to get the output:
image

(Actually in order to make it work I added source-bash ~/.bashrc & .profile on top of the .xonshrc) but without them I was getting source-foreign error

Error while loading pyenv xontrib

See xonsh/xonsh#3610

pierre@kth ~ $ source-bash ~/Dev/env_xonsh/bin/activate                         
(env_xonsh) pierre@kth ~ $ $XONSH_DEBUG=1                                       
(env_xonsh) pierre@kth ~ $ $XONSH_SHOW_TRACEBACK = True                         
(env_xonsh) pierre@kth ~ $ xonsh                                                
Hello Pierre!
^Z
(env_xonsh) pierre@kth ~ $ xontrib load pyenv                                   
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 22033, in _run_binary
    p = self.cls(self.cmd, bufsize=bufsize, **kwargs)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 14147, in __init__
    self.proc = proc = subprocess.Popen(
  File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'pyenv\n'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 15421, in __init__
    proc = spec.run(pipeline_group=pipeline_group)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 22022, in run
    p = self._run_binary(kwargs)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 22046, in _run_binary
    sug = suggest_commands(cmd0, env, builtins.aliases)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 5293, in suggest_commands
    c = command_not_found(cmd)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 5241, in command_not_found
    s = subprocess.check_output(
  File "/usr/lib/python3.8/subprocess.py", line 411, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/usr/lib/python3.8/subprocess.py", line 512, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '/usr/lib/command-not-found pyenv
; exit 0' returned non-zero exit status 2.
xonsh: To log full traceback to a file set: $XONSH_TRACEBACK_LOGFILE = <filename>
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 22033, in _run_binary
    p = self.cls(self.cmd, bufsize=bufsize, **kwargs)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 14147, in __init__
    self.proc = proc = subprocess.Popen(
  File "/usr/lib/python3.8/subprocess.py", line 854, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "/usr/lib/python3.8/subprocess.py", line 1702, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'pyenv\n'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 15421, in __init__
    proc = spec.run(pipeline_group=pipeline_group)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 22022, in run
    p = self._run_binary(kwargs)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 22046, in _run_binary
    sug = suggest_commands(cmd0, env, builtins.aliases)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 5293, in suggest_commands
    c = command_not_found(cmd)
  File "/usr/lib/python3/dist-packages/xonsh/__amalgam__.py", line 5241, in command_not_found
    s = subprocess.check_output(
  File "/usr/lib/python3.8/subprocess.py", line 411, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/usr/lib/python3.8/subprocess.py", line 512, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '/usr/lib/command-not-found pyenv
; exit 0' returned non-zero exit status 2.

shims path is no longer added to PATH

Recent changes in pyenv require additional configuration besides sourcing pyenv init -. See pyenv/pyenv#1906.

My workaround was to add the following to .xonshrc before xontrib load pyenv:

source-bash --login=false --interactive=false $(pyenv init --path)

'shell' subcommand doesn't print current version

pyenv shell is supposed to echo "$PYENV_VERSION" for bash to print the actual version (if the version is defined), but in this plugin nothing is printed

It might be xonsh-related, not specific to this plugin since

  • source-bash --suppress-skip-message -n "echo 1" doesn't print 1 (asked this question in xonsh discussions xonsh/xonsh#5019)

Deprecation warning on install

DEPRECATION: xontrib-langenv is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at pypa/pip#8559

Getting command not found `ForeignShellFunctionAlias(extra_args=(), funcname='pyenv', shell='bash')`

Thanks for resolving the last issue, I was facing. After the latest version and the new system I am getting mentioned error:

xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
xonsh: subprocess mode: command not found: ForeignShellFunctionAlias(extra_args=(), funcname='pyenv', shell='bash')
foreignshellfunctionalias(extra_args=(), funcname='pyenv', shell='bash'): command not found
xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
xonsh: subprocess mode: command not found: ForeignShellFunctionAlias(extra_args=(), funcname='pyenv', shell='bash')
foreignshellfunctionalias(extra_args=(), funcname='pyenv', shell='bash'): command not found
xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
xonsh: subprocess mode: command not found: ForeignShellFunctionAlias(extra_args=(), funcname='pyenv', shell='bash')
foreignshellfunctionalias(extra_args=(), funcname='pyenv', shell='bash'): command not found

image

Any guesses?

Thanks :)

Maybe convert xsh to py for faster startup?

Since xonsh xontribs are not compiled unlike the .py ones, so take slower to start xonsh/xonsh#3953, it might make sense to rewrite this xontrib in Python?

I've taken a stab at it by changing the pyenv version (and the langenv_common) in this fork https://github.com/eugenesvk/xontrib-langenv/tree/repy, besides conversion I've also didn't understand why there is a need to search for tools themselves (not their shims) that are supposed to be in PATH already if they're properly installed, so just replaced it ;) with a lazy search of known xonsh binaries

upd: seems to be working, also #14 is fixed
However, I don't get how source-bash works, it doesn't print anything even in the regular subprocess, e.g. source-bash --suppress-skip-message -n "echo 1" doesn't print 1, so in the create_alias function when you do pyenv shell which ultimately goes to source-bash to source echo $PYTHON_VERSION, nothing is printed even if said variable exists

asked this question in xonsh discussions xonsh/xonsh#5019

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.