74th/xonsh-direnv

Recent change in xonsh seems to have broken direnv

con-f-use opened this issue · 3 comments

One of the recent changes to xonsh seems to have broken xonsh-direnv

$ cd atdiff                                                                           
direnv: loading .envrc
direnv: export +VIRTUAL_ENV ~PATH
{
  "DIRENV_DIFF": "eJzkkt1umzAYhu_FxwE30CSDMygwSFOWEeePEwuw-UnBEAwBUuXepy1bM63TNE0765mf198jWa-_F1AB9QUsNWQDFcC0LCg8BAyKeRkFOQwzpt7CP9FvBTEqWZwlcAhIQcomznLKryMtr-HV4L_wK94uvh2u-JolQUH5z973gLOg-n8PBZcRYF8LMhzPdDfYcDygAuEmEnqiOYxaEsCgIVkcg9GP2a2GHmxzBVRA5z0_c-uxN7TPeqvLQ8h4YSNUUEZKQSdLbUBLafos3Tnyttv1cigvTtywFbxHu-M47tJzyfOxzmb-qtt_tLA1farnZPs4kTq7bm0n7HkVRnWXdmfvLmcIDeZEUKTpLOoULH2SBVOny3lTU8lYNW1vF2jBmeQ5fFt5qWVNkNP7Y9u9x8mzXx216N6ZFIa7L62ndbIWWsl_qFvleDj7BxySGdYSTcNYKdz1zgGjt8vzphQokqym7ASroUlLJsjiVPzwN__yDjduBDaOh9baApvu5h9qBZfLlwAAAP__lzpRYQ==",
  "DIRENV_DIR": "-/home/confus/atdiff",
  "DIRENV_WATCHES": "eJxszsFKxDAQBuB3ybnsmHTTmendo-BdPAyTP26k20I3WwXx3b3LvsDH9_YTXq1fwhzosl1Bn7ZSwYGF_F6MrJdWK52wHruHIbxspbcrwhwzR0lnTTyE5-9267cw9_2O3-EBePJtre2DStuxHmTLsn2RIsWpRhFF5TIxZ1HN4_gkZpqAc4I5mDNYoFMUgU-u2ZCru9qjzZj_bd7_AgAA__9mNUXI",
  "PATH": "/home/confus/atdiff/.direnv/python-3.6.8/bin:/home/confus/.local/bin:/home/confus/bin:/home/confus/bin:/home/confus/.local/bin:/home/confus/.config/yadmdotfiles/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/confus/bin:/home/confus/.local/bin:/home/confus/.config/yadmdotfiles/bin",
  "VIRTUAL_ENV": "/home/confus/atdiff/.direnv/python-3.6.8"

$ which python                                                                        
/usr/bin/python      # so no env loaded!

$ xonfig                                                                              
+------------------+----------------------+
| xonsh            | 0.9.13               |
| Git SHA          | 550d70d2             |
| Commit Date      | May 22 01:42:07 2019 |
| Python           | 3.6.8                |
| PLY              | 3.11                 |
| have readline    | True                 |
| prompt toolkit   | 2.0.9                |
| shell type       | prompt_toolkit2      |
| pygments         | 2.2.0                |
| on posix         | True                 |
| on linux         | True                 |
| distro           | ubuntu               |
| on darwin        | False                |
| on windows       | False                |
| on cygwin        | False                |
| on msys2         | False                |
| is superuser     | False                |
| default encoding | utf-8                |
| xonsh encoding   | utf-8                |
| encoding errors  | surrogateescape      |
+------------------+----------------------+

This seems to effect both newly created environments and preexisting ones. Bash and other shells are not effected. See xonsh/xonsh#3369

Seems to be related to to this: xonsh/xonsh#3369
I have made #3 to fix it in xonsh-direnv. I think this will make xonsh-direnv more robust, even once the problem is fixed upstream in xonsh.

Welp, might be necessary to set $UPDATE_OS_ENVIRON=True in xonsh or do this (or a similar) fix, depending on xonsh and direnv version:

def __direnv():
    p = subprocess.Popen(
        'direnv export json'.split(),
        stdout=subprocess.PIPE,
        env=__xonsh__.env.detype()
    )
    r, _ = p.communicate()
    if len(r) > 0:
        for k, v in json.loads(r).items():
            if v is None:
                del (__xonsh__.env[k])
            else:
                __xonsh__.env[k] = v

or set $UPDATE_OS_ENVIRON = True in xonsh.

See: xonsh/xonsh#3371

@con-f-use: definitely.

I'm making do with $UPDATE_OS_ENVIRON in the meantime, but we probably still need do changes with respect to the type conversion.

I think this one warrants a reopening.