Repository: xonsh/xonsh

Issue:
Cannot set PROMPT in config file on Windows
Steps to repeat:
Be on Windows platform
Generate a config.json, e.g. through xonfig wizard. Set the PROMPT to be anything other than default.
Close and relaunch xonch

Expectation:
That the new prompt takes effect

Actual result:
Default prompt always shows.

This turns out to be the result of a little function called "windows_env_fixes()" in environ.py.
This is it:

```
def windows_env_fixes(ctx):
    """Environment fixes for Windows. Operates in-place."""
    # Windows default prompt doesn't work.
    ctx['PROMPT'] = DEFAULT_PROMPT
    # remove these bash variables which only cause problems.
    for ev in ['HOME', 'OLDPWD']:
        if ev in ctx:
            del ctx[ev]
    # Override path-related bash variables; on Windows bash uses
    # /c/Windows/System32 syntax instead of C:\\Windows\\System32
    # which messes up these environment variables for xonsh.
    for ev in ['PATH', 'TEMP', 'TMP']:
        if ev in os.environ:
            ctx[ev] = os.environ[ev]
        elif ev in ctx:
            del ctx[ev]
    ctx['PWD'] = _get_cwd()
```

This also explained why my 'HOME' environment variable vanished - this is a useful variable to make some Linux/Cygwin derived applications play ball (when running through normal command prompt not bash). I'm not quite sure what problem removing it fixes.

My suggestion:
Re-order default_env() so that windows_env_fixes() comes before load_static_config(). That way anything in config.json will take effect on the now fixed imported environment.

The rest of the changes all appear to relate to bash. Would it not be more appropriate to filter out those imports from load_foreign_env() (if on Windows), rather than unilaterally removing them from the inherited environment?

If I have some time later today I'll submit a pull request.



Discussion:
A PR would be very welcome :)


Resolving patch:
diff --git a/xonsh/environ.py b/xonsh/environ.py
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -1261,10 +1261,8 @@ def xonshrc_context(rcfiles=None, execer=None, ctx=None):
     return ctx
 
 
-def windows_env_fixes(ctx):
+def windows_foreign_env_fixes(ctx):
     """Environment fixes for Windows. Operates in-place."""
-    # Windows default prompt doesn't work.
-    ctx['PROMPT'] = DEFAULT_PROMPT
     # remove these bash variables which only cause problems.
     for ev in ['HOME', 'OLDPWD']:
         if ev in ctx:
@@ -1285,13 +1283,27 @@ def default_env(env=None, config=None, login=True):
     # in order of increasing precedence
     ctx = dict(BASE_ENV)
     ctx.update(os.environ)
+    if ON_WINDOWS:
+        # Windows style PROMPT definitions don't work in XONSH:
+        try:
+            del ctx['PROMPT']
+        except KeyError:
+            pass
+
     if login:
         conf = load_static_config(ctx, config=config)
+
+        foreign_env = load_foreign_envs(shells=conf.get('foreign_shells', DEFAULT_SHELLS),
+                                        issue_warning=False)
+        if ON_WINDOWS:
+            windows_foreign_env_fixes(foreign_env)
+
+        ctx.update(foreign_env)
+
+        # Do static config environment last, to allow user to override any of
+        # our environment choices
         ctx.update(conf.get('env', ()))
-        ctx.update(load_foreign_envs(shells=conf.get('foreign_shells', DEFAULT_SHELLS),
-                                     issue_warning=False))
-    if ON_WINDOWS:
-        windows_env_fixes(ctx)
+
     # finalize env
     if env is not None:
         ctx.update(env)


Test patch: