Skip to content

fix(cmd/start): use absolute executable path for child process - #2125

Merged
PIKACHUIM merged 2 commits into
OpenListTeam:mainfrom
abcqqoo:fix/windows-start-executable-path
Sep 1, 2026
Merged

PIKACHUIM merged 2 commits into
OpenListTeam:mainfrom
abcqqoo:fix/windows-start-executable-path

Conversation

@abcqqoo

@abcqqoo abcqqoo commented Feb 15, 2026

Copy link
Copy Markdown
Contributor

Description / 描述

  • 将 start 命令启动子进程时使用的路径从 os.Args[0] 改为 os.Executable()。
  • 透传 start 后的用户参数(os.Args[2:])给 server。
  • 当已传入 --force-bin-dir 时不再重复追加。

Motivation and Context / 背景

在 Windows 上,当添加了可执行文件目录环境变量后,从非可执行文件目录调用 openlist start 时,os.Args[0] 可能是相对路径(如 .\openlist),导致子进程启动失败:
exec: ".\\openlist": executable file not found in %PATH%。

How Has This Been Tested? / 测试

在非可执行文件目录执行 openlist start,确认不再出现 exec: ".\\openlist": executable file not found in %PATH%,确认 start 能成功拉起子进程(日志显示 success start pid)。

Checklist / 检查清单

  • I have read the CONTRIBUTING document.
    我已阅读 CONTRIBUTING 文档。
  • I have formatted my code with go fmt or prettier.
    我已使用 go fmt 或 prettier 格式化提交的代码。
  • I have added appropriate labels to this PR (or mentioned needed labels in the description if lacking permissions).
    我已为此 PR 添加了适当的标签(如无权限或需要的标签不存在,请在描述中说明,管理员将后续处理)。
  • I have requested review from relevant code authors using the "Request review" feature when applicable.
    我已在适当情况下使用"Request review"功能请求相关代码作者进行审查。
  • I have updated the repository accordingly (If it’s needed).
    我已相应更新了相关仓库(若适用)。

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a Windows-specific bug where the start command fails to spawn a child process when the executable is invoked via a relative path. The fix uses os.Executable() instead of os.Args[0] to obtain the absolute path to the executable, ensuring reliable subprocess spawning across all operating systems.

Changes:

  • Replace os.Args[0] with os.Executable() for obtaining the executable path
  • Pass user arguments from start command to server command via os.Args[2:]
  • Add duplicate detection to avoid appending --force-bin-dir when already present

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/start.go Resolved
@abcqqoo
abcqqoo force-pushed the fix/windows-start-executable-path branch from 568c25a to abe5737 Compare February 20, 2026 06:13
@abcqqoo
abcqqoo force-pushed the fix/windows-start-executable-path branch from abe5737 to ba8a0d8 Compare February 28, 2026 09:29
@Suyunmeng
Suyunmeng force-pushed the main branch 4 times, most recently from a31fd53 to 7bea29c Compare April 2, 2026 17:27

@pikachuren pikachuren left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏 感谢 @abcqqoo 提交!
🤖 AI 自动审核声明:本评审报告由 AI 自动生成,当前使用 Claude Opus 5 模型进行分析。
⚠️ AI 分析结果仅供参考,可能存在误判或遗漏。如您发现任何问题或有不同意见,欢迎随时提出讨论和纠正。
⚠️ 重要提醒:即使 AI 评审认为代码质量良好且建议合并,最终是否合并仍需由项目维护者进行人工判定。项目维护者会综合考虑代码质量、项目规划、技术方向、团队资源等多方面因素做出决策。

🎯 结论

✅ 建议 Approve(由维护者人工确认)— 修正了一处真实缺陷,实现比原逻辑更健壮

📖 概要

fix(cmd/start): use absolute executable path for child process · 修复以相对路径或通过 PATH 启动时,守护子进程拉起失败的问题。
核心改动:改用 os.Executable() 取绝对路径,重建子进程参数,并对 --force-bin-dir 做去重。

🧭 整体方案

原实现直接用 os.Args[0] 作为 exec.Cmd.Path,当程序通过 ./openlist 或 PATH 查找启动时,os.Args[0] 可能是相对路径或裸命令名,子进程会因工作目录变化而找不到可执行文件。改用 os.Executable() 是 Go 中处理这一问题的标准做法,方案正确。

📊 变更统计

1 个文件(+17 / -7 行) | 功能 ⭐⭐⭐⭐⭐ | 最小改动 ⭐⭐⭐⭐ | 前向兼容 ⭐⭐⭐⭐⭐ | 方案设计 ⭐⭐⭐⭐⭐

🚨 关键问题

P0(阻塞合并):无

P1(建议修复):无

P2(可选):

  • 💡 原实现用 args[1] = "server" 原地修改 os.Args,属于会污染全局状态的写法;新实现改为 append([]string{"server"}, os.Args[2:]...) 构造新切片,这个改进很好~ 顺带一提,新写法隐含了「os.Args 至少有 2 个元素」的前提,考虑到该函数由 start 子命令触发时必然满足,实际不会越界,仅作记录。
  • 💡 --force-bin-dir 去重同时匹配了 --force-bin-dir 与 --force-bin-dir= 两种形式,考虑周到。若想更简洁,也可以用 slices.ContainsFunc~

📂 逐文件分析

cmd/start.go

改动意图:确保守护模式下子进程能被可靠拉起。
代码逻辑:os.Executable() 解析绝对路径 → 构造 ["server", <原始参数>...] → 检查并按需追加 --force-bin-dir → exec.Command 启动。
问题分析:逻辑正确,且顺带修复了原代码重复追加 --force-bin-dir 的潜在问题。os.Executable() 失败时 log.Fatal 退出也是合理的——这种情况下无法安全继续。
详细建议:无需改动。

✅ 待处理清单

  • 在 Linux / Windows 下分别验证 openlist start 守护模式正常拉起
  • 确认通过符号链接启动时行为符合预期(os.Executable() 在部分平台会解析软链)

🎯 结论:✅ 建议 Approve — 标准做法修复真实问题,改动干净,无阻塞项。

@PIKACHUIM
PIKACHUIM merged commit ce87b45 into OpenListTeam:main Sep 1, 2026
cjd6568358 pushed a commit to cjd6568358/OpenList-zig that referenced this pull request Oct 18, 2026
…istTeam#2125)

* fix(cmd/start): use absolute executable path for child process

* fix(cmd/start): detect force-bin-dir flag variants

---------

Co-authored-by: Zoe Lee <zoelee@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants