Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

windows app paths missing from $PATH #41

Open
Timost opened this issue May 25, 2023 · 1 comment
Open

windows app paths missing from $PATH #41

Timost opened this issue May 25, 2023 · 1 comment

Comments

@Timost
Copy link

Timost commented May 25, 2023

Hi,
Thank you for bottle-imp. 🙏

When using bottle-imp, I have a small issue with my PATH.

Inside a session started with imp -s my PATH envvar is missing the "windows apps paths" (eg: /mnt/c/windows/system32 etc.).

When I run a session without imp, my PATH envvar contains the paths.

The symptoms are code . and explorer.exe crashing with "command not found" for exemple.

I'm using an Ubuntu 22.04 WSL2 instance.

My current work-around is to start a imp session aside and then work inside a "normal" WSL session.

@enndubyu
Copy link

enndubyu commented Oct 4, 2023

Here's a patch that you can apply to preserve PATH and any environment variables listed in WSLENV (such as WT_SESSION and WT_PROFILE_ID used by vscode):

From d2b00226372922d2fe3e09b21be9f1719dd4419d Mon Sep 17 00:00:00 2001
From: Nick Winterer <nicholas.winterer@spectranetix.com>
Date: Tue, 3 Oct 2023 17:36:49 -0700
Subject: [PATCH] Preserve WSLENV variables and PATH

Changes:
 * Add PATH to machinectl environment
 * Add WSLENV entries to machinectl environment
 * Remove special handling for WT_PROFILE_ID and WT_SESSION, since
   these are just a special case of WSLENV
---
 binsrc/imp/__main__.py | 29 ++++++++++-------------------
 binsrc/imp/helpers.py  | 15 +++++++++++++++
 debian/changelog       |  7 +++++++
 3 files changed, 32 insertions(+), 19 deletions(-)

diff --git a/binsrc/imp/__main__.py b/binsrc/imp/__main__.py
index 1de6da6..4693d82 100644
--- a/binsrc/imp/__main__.py
+++ b/binsrc/imp/__main__.py
@@ -11,7 +11,7 @@ import configuration
 import helpers
 
 # Global variables
-version = "1.00"
+version = "1.0-nw"
 
 verbose = False
 login = None
@@ -158,14 +158,11 @@ def do_shell():
     if verbose:
         print("imp: starting shell")
 
-    if helpers.get_in_windows_terminal():
-        os.execv ('/usr/bin/machinectl', ['machinectl',
-            '-E', 'WT_SESSION=' + os.environ['WT_SESSION'],
-            '-E', 'WT_PROFILE_ID=' + os.environ['WT_PROFILE_ID'],
-            'shell', '-q', login + '@.host'])
-    else:
-        os.execv ('/usr/bin/machinectl', ['machinectl',
-            'shell', '-q', login + '@.host'])
+    argv = ['machinectl']
+    argv.extend(helpers.get_env_args())
+    argv.extend([ 'shell', '-q', login + '@.host'])
+
+    os.execv('/usr/bin/machinectl', argv)
     
     # never get here
 
@@ -183,17 +180,11 @@ def do_command(commandline):
     if len(commandline) == 0:
         sys.exit("imp: no command specified")
 
-    if helpers.get_in_windows_terminal():
-        command = ['machinectl',
-            '-E', 'WT_SESSION=' + os.environ['WT_SESSION'],
-            '-E', 'WT_PROFILE_ID=' + os.environ['WT_PROFILE_ID'],
-            'shell', '-q', login + '@.host', '/usr/bin/env', '-C', os.getcwd()] + commandline;
-    else:
-        command = ['machinectl',
-            'shell', '-q', login + '@.host', '/usr/bin/env', '-C', os.getcwd()] + commandline;
-
-    os.execv ('/usr/bin/machinectl', command)
+    command = ['machinectl']
+    command.extend(helpers.get_env_args())
+    command.extend(['shell', '-q', login + '@.host', '/usr/bin/env', '-C', os.getcwd()])
 
+    os.execv ('/usr/bin/machinectl', command + commandline)
 
 def do_shutdown():
     """Shut down systemd and the WSL instance."""
diff --git a/binsrc/imp/helpers.py b/binsrc/imp/helpers.py
index 2f1cc10..c43ea68 100644
--- a/binsrc/imp/helpers.py
+++ b/binsrc/imp/helpers.py
@@ -14,6 +14,21 @@ def get_in_windows_terminal():
     else:
         return False
 
+def get_wsl_env():
+    if os.environ['WSLENV']:
+        e = list(map(lambda x: str(x).split("/")[0], os.environ['WSLENV'].split(":")))
+        e.remove("")
+        return e
+    else:
+        return []
+
+def get_env_args():
+    args = ['-E', 'PATH=' + os.environ['PATH'], '-E', 'WSLENV=' + os.environ['WSLENV']]
+    env = get_wsl_env()
+    for e in env:
+        args.append("-E")
+        args.append(e + "=" + os.environ[e])
+    return args
 
 def get_login_session_user():
     """Get the user logged into the current session, pre-setuid."""
diff --git a/debian/changelog b/debian/changelog
index afacd9f..a1db64c 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
+bottle-imp (1.0-nw) buster bullseye bookworm sid focal jammy; urgency=medium
+
+        * Preserve WSLENV variables for shells and commands. 
+        * Preserve PATH variable for shells and commands.
+
+ -- nw-wsl-pengwin <nick@nw-wsl-pengwin.spx.local>  Tue, 03 Oct 2023 12:16:28 -0700
+
 bottle-imp (1.0) buster bullseye bookworm sid focal jammy; urgency=medium
 
         * Change over to generator-based model.
-- 
2.39.2


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants