Skip to content

Commit acff004

Browse files
committed
feat: add cleanup_env util to avoid opening new projects within script venv
1 parent b701bf6 commit acff004

1 file changed

Lines changed: 29 additions & 9 deletions

File tree

open/open.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@
3434
SIMILARITY_THRESHOLD = 4
3535
PATHS_DIR = os.path.join(os.path.dirname(__file__), ".env")
3636

37+
OPEN_FILE_MANAGER = False
38+
39+
40+
def cleaned_env():
41+
env = os.environ.copy()
42+
43+
env.pop("VIRTUAL_ENV", None)
44+
45+
if "PATH" in env:
46+
paths = env["PATH"].split(":")
47+
cleaned_paths = [
48+
p
49+
for p in paths
50+
if not p.endswith("/.venv/bin") and not p.endswith("/venv/bin")
51+
]
52+
env["PATH"] = ":".join(cleaned_paths)
53+
54+
return env
55+
3756

3857
class Command(Protocol):
3958
def execute(self) -> int: ...
@@ -93,22 +112,23 @@ def execute(self):
93112
if self.relative_path:
94113
path_project = Path(path_project, self.relative_path)
95114

96-
# Open file manager
97-
try:
98-
subprocess.run(["xdg-open", path_project], check=True)
99-
logger.info("Opened file manager for: %s", path_project)
100-
except subprocess.SubprocessError as e:
101-
logger.error("Failed to open file manager: %s", e)
102-
return 1
103-
104115
# Open VS Code
105116
try:
106-
subprocess.run(["code", path_project], check=True)
117+
subprocess.run(["code", path_project], env=cleaned_env(), check=True)
107118
logger.info("Opened VS Code for: %s", path_project)
108119
except subprocess.SubprocessError as e:
109120
logger.error("Failed to open VS Code: %s", e)
110121
return 1
111122

123+
if OPEN_FILE_MANAGER:
124+
# Open file manager
125+
try:
126+
subprocess.run(["xdg-open", path_project], check=True)
127+
logger.info("Opened file manager for: %s", path_project)
128+
except subprocess.SubprocessError as e:
129+
logger.error("Failed to open file manager: %s", e)
130+
return 1
131+
112132
# Close calling terminal if requested
113133
if not self.keep_terminal:
114134
try:

0 commit comments

Comments
 (0)