Skip to content

Commit d54acd3

Browse files
fix: set working directory if current is invalid (#261)
1 parent 6b5df12 commit d54acd3

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/mcpm/cli.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
from mcpm.migration import V1ConfigDetector, V1ToV2Migrator
3232
from mcpm.utils.logging_config import setup_logging
3333
from mcpm.utils.rich_click_config import click, get_header_text
34+
import os
35+
from pathlib import Path
3436

3537
console = Console() # stdout for regular CLI output
3638
err_console = Console(stderr=True) # stderr for errors/tracebacks
@@ -86,6 +88,21 @@ def wrapper(*args, **kwargs):
8688
@handle_exceptions
8789
def main(ctx, version, help_flag):
8890
"""Main entry point for MCPM CLI."""
91+
92+
try:
93+
# Check if the current working directory is valid.
94+
os.getcwd()
95+
except OSError:
96+
# If getcwd() fails, it means the directory doesn't exist.
97+
# This can happen when mcpm is called from certain environments
98+
# like some Electron apps that don't set a valid cwd.
99+
home_dir = str(Path.home())
100+
err_console.print(
101+
f"Current working directory is invalid. Changing to home directory: {home_dir}",
102+
style="bold yellow"
103+
)
104+
os.chdir(home_dir)
105+
89106
if version:
90107
print_logo()
91108
return

0 commit comments

Comments
 (0)