From 41c57ede0253a0d69f6ec8afc7193d2a5998dee9 Mon Sep 17 00:00:00 2001 From: xuli500177 Date: Thu, 28 May 2026 19:42:21 +0800 Subject: [PATCH] fix: open HTML report in Windows browser when running in WSL WSL users currently see a broken popup when TrendRadar tries to open the HTML report, because Python's webbrowser.open() launches a Linux browser that has no display. This fix detects the WSL environment and uses PowerShell's Start-Process to open the report in the user's Windows default browser instead. Changes: - Add _is_wsl() to detect WSL via /proc/version - Add _open_in_windows_browser() using wslpath + powershell.exe - Non-WSL environments are unaffected --- trendradar/__main__.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/trendradar/__main__.py b/trendradar/__main__.py index d50a0b8612314..865cd1b287aff 100644 --- a/trendradar/__main__.py +++ b/trendradar/__main__.py @@ -267,6 +267,35 @@ def _should_open_browser(self) -> bool: """判断是否应该打开浏览器""" return not self.is_github_actions and not self.is_docker_container + @staticmethod + def _is_wsl() -> bool: + """检测是否运行在 WSL 中""" + try: + with open("/proc/version", "r") as f: + return "microsoft" in f.read().lower() + except Exception: + return False + + @staticmethod + def _open_in_windows_browser(html_file: str) -> None: + """在 WSL 中用 Windows 默认浏览器打开文件""" + import subprocess + try: + result = subprocess.run( + ["wslpath", "-w", html_file], + capture_output=True, text=True + ) + win_path = result.stdout.strip() + if win_path: + subprocess.Popen( + ["powershell.exe", "-Command", f'Start-Process "{win_path}"'], + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) + else: + print(f"路径转换失败,手动打开: {html_file}") + except Exception as e: + print(f"打开浏览器失败: {e},手动打开: {html_file}") + def _setup_proxy(self) -> None: """设置代理配置""" if not self.is_github_actions and self.ctx.config["USE_PROXY"]: @@ -1725,7 +1754,10 @@ def _execute_mode_strategy( if self._should_open_browser() and html_file: file_url = "file://" + str(Path(html_file).resolve()) print(f"正在打开HTML报告: {file_url}") - webbrowser.open(file_url) + if self._is_wsl(): + self._open_in_windows_browser(html_file) + else: + webbrowser.open(file_url) elif self.is_docker_container and html_file: print(f"HTML报告已生成(Docker环境): {html_file}")