From 1a94117607316eb94f52675c820cf5fe9aff82b2 Mon Sep 17 00:00:00 2001 From: Zihao Xu Date: Sat, 3 Jan 2026 23:53:31 -0800 Subject: [PATCH] fix: cross-platform subprocess compatibility for npm dev server On macOS/Linux, using shell=True with a list argument doesn't work as expected. Only the first element is passed to the shell, causing npm to print help instead of running the dev command. Changes: - Use shell=False + list on macOS/Linux, shell=True + string on Windows. - Use terminate() instead of taskkill on macOS/Linux for cleanup. --- src/server/main.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/server/main.py b/src/server/main.py index d0e5dbe..dd0ed52 100644 --- a/src/server/main.py +++ b/src/server/main.py @@ -465,9 +465,13 @@ async def lifespan(app: FastAPI): web_dir = os.path.join(project_root, 'web') print(f"正在启动前端开发服务 (npm run dev) 于: {web_dir}") - # Windows 下使用 shell=True + # 跨平台兼容:Windows 用 shell=True + 字符串,macOS/Linux 用 shell=False + 列表。 try: - npm_process = subprocess.Popen(["npm", "run", "dev"], cwd=web_dir, shell=True) + import platform + if platform.system() == "Windows": + npm_process = subprocess.Popen("npm run dev", cwd=web_dir, shell=True) + else: + npm_process = subprocess.Popen(["npm", "run", "dev"], cwd=web_dir, shell=False) # 假设 Vite 默认端口是 5173 target_url = "http://localhost:5173" except Exception as e: @@ -489,9 +493,13 @@ async def lifespan(app: FastAPI): if npm_process: print("正在关闭前端开发服务...") try: - # 尝试终止进程 - # Windows 下 terminate 可能无法杀死 shell=True 的子进程树,这里简单处理 - subprocess.call(['taskkill', '/F', '/T', '/PID', str(npm_process.pid)]) + import platform + if platform.system() == "Windows": + # Windows 下 terminate 可能无法杀死 shell=True 的子进程树。 + subprocess.call(['taskkill', '/F', '/T', '/PID', str(npm_process.pid)]) + else: + # macOS/Linux 直接 terminate。 + npm_process.terminate() except Exception as e: print(f"关闭前端服务时出错: {e}")