Search code examples
pythonxonsh

odd behavior changing dir from .xonshrc


I am using the xonsh shell and my .xonshrc file contains a call to os.chdir(), which works, but oddly. When I start xonsh, my prompt, which shows my current path, seems to indicate I am still in my home dir until after I execute a command or even simply press enter. e.g.

[etlsmart@msd-cld-etl-21p ~]$ export PROCESS=ibmc_reminder; /etlsmart/env/xonsh/bin/xonsh
etlsmart@msd-cld-etl-21p:~
$
etlsmart@msd-cld-etl-21p:/etlsmart/feeds/ibmc_reminder
$

If I execute a command such as ls, it lists the contents of the correct directory (the one I've changed to, not my home directory).

[etlsmart@msd-cld-etl-21p ~]$ export PROCESS=ibmc_reminder; /etlsmart/env/xonsh/bin/xonsh
etlsmart@msd-cld-etl-21p:~
$ ls
bin  block  data  docs  etc  lock  log  post_deployment  stage
etlsmart@msd-cld-etl-21p:/etlsmart/feeds/ibmc_reminder
$ ls
bin  block  data  docs  etc  lock  log  post_deployment  stage

So this appears to be a benign cosmetic problem. That is, unless my first command is cd, then it does not behave correctly. The cd neither fails nor works, which makes no sense to me.

etlsmart@msd-cld-etl-21p:~
$ cd log
etlsmart@msd-cld-etl-21p:/etlsmart/feeds/ibmc_reminder
$ pwd
/etlsmart/feeds/ibmc_reminder
etlsmart@msd-cld-etl-21p:/etlsmart/feeds/ibmc_reminder
$ cd log
etlsmart@msd-cld-etl-21p:/etlsmart/feeds/ibmc_reminder/log

I have tried adding various commands to my .xonshrc file to get past this, but so far without success. Can anyone offer an explanation and/or solution to this problem?


Solution

  • It will be good to do this after init. This works to me in ~/.xonshrc:

    @events.on_post_init
    def on_post_init(**_):
        cd /tmp
    

    But I see that xontrib-back2dir and xontrib-jump-to-dir use another approach:

    @events.on_post_init
    def on_post_init(**_):
      __xonsh__.subproc_captured_stdout(['cd', '/tmp'])