cross-platform-cli-launching
Use when shell finds a CLI but child process cannot.
- License 许可
- MIT
- 最近更新
- 2026-10-20
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- cross-platform-cli-launching
- author
- Hermes Agent
- license
- MIT
- version
- 1.1.0
- metadata
- { "hermes": { "tags": [ "subprocess", "windows", "cli", "npm", "pathext", "portability", "debugging" ], "related_skills": [ "systematic-debugging", "node-inspect-debugger" ] } }
- platforms
- [ "linux", "macos", "windows" ]
- description
- Use when shell finds a CLI but child process cannot.
02 资源文件 2
- 参考文档references/windows-npm-shims.md5.1 KB
- 脚本scripts/probe_cli_resolution.py2.8 KB
03 SKILL.md 正文
Cross-Platform CLI Launching
Overview
Use this skill when an application launches another CLI and reports “not found,” even though the same command works in an interactive shell. Diagnose command resolution in the actual parent runtime, then normalize the executable before launch. Do not treat an install hint as proof that the binary is absent.
This class includes Python subprocess, Node child_process, service/gateway processes, npm-generated shims, PATH/PATHEXT differences, and shell-vs-direct process creation.
Diagnostic Workflow
- Reproduce both layers separately:
- interactive shell:
<tool> --version; - exact application primitive: the same
subprocess.run,Popen,spawn, orexecFilecall used by the code.
- interactive shell:
- Inspect resolution in the parent runtime, not only the shell:
- Python:
shutil.which(name),PATH, andPATHEXT; - Node:
process.env.PATH,process.env.PATHEXT, and the selected spawn API.
- Python:
- Identify the actual artifact: native executable,
.cmd,.bat,.ps1, shell script, symlink, or package-manager shim. - Compare direct launch of the bare name with direct launch of the resolved absolute path.
- Trace every launch site. A preflight check and the real long-lived subprocess must use the same resolver.
- Separate environment defects from launch-semantics defects:
- resolver returns nothing: PATH/service environment/install state;
- resolver returns a wrapper but bare direct launch fails: platform command-resolution mismatch;
- resolved absolute launch fails: wrapper runtime, quoting, permissions, or architecture.
- Add one regression test that represents the platform wrapper shape, then implement the smallest shared resolver.
Preferred Implementation Pattern
Resolve once, preserve argument boundaries, and pass an argv list:
from pathlib import Path
import shutil
def resolve_cli(command: str) -> str:
resolved = shutil.which(command)
if resolved:
return resolved
if Path(command).is_file():
return str(Path(command))
raise FileNotFoundError(f"CLI executable not found: {command}")
Use the returned path for both version/preflight checks and the production process launch. If configuration allows an explicit executable path, resolve only bare names and preserve explicit paths.
Treat the launch environment's PATH as part of the executable-selection contract. If an API accepts env=..., do not resolve with an unqualified shutil.which(name) and then launch with a different child environment: that searches the parent PATH, can bypass the caller's overridden or hardened env["PATH"], and changes normal POSIX Popen(..., env=env) behavior. Either resolve against the effective child path (shutil.which(name, path=spawn_env.get("PATH"))) or keep native lookup on platforms where pre-resolution is unnecessary. Add a regression test where parent and child PATH values select different fixtures.
On Windows, package managers commonly expose .cmd/.bat wrappers. Prefer launching the resolved wrapper directly when the runtime supports it. If the language runtime cannot execute that wrapper directly, invoke the platform command processor deliberately and safely rather than setting a broad shell=True default.
Tight Reproduction
For a reusable bounded probe, run python scripts/probe_cli_resolution.py <tool> -- --version from the skill directory. It compares the bare launch with the shutil.which result without dumping the full PATH; pass --path before <tool> to test a child-specific PATH.
Python:
import os, shutil, subprocess
print("PATH:", os.environ.get("PATH"))
print("PATHEXT:", os.environ.get("PATHEXT"))
print("resolved:", shutil.which("tool"))
subprocess.run(["tool", "--version"], check=True)
Then replace "tool" with the exact value returned by shutil.which. The differential result distinguishes PATH lookup from process-creation semantics.
Verification
- Shell invocation succeeds or fails as recorded.
- The exact application launch primitive is reproduced.
- Resolver output is shown without exposing secrets.
- Preflight and production launch use the same resolver policy.
- Write separate RED tests for the preflight/version path and the real long-lived spawn; either path can remain broken independently.
- In portable unit tests, mock resolver output to a Windows
.cmdpath and capture the argv passed torun/Popen. Reserve executing a real.cmdwrapper for native-Windows integration tests. - If an unrelated existing test asserted the literal bare executable, narrow it to the behavior it owns (subcommand, sandbox flags, environment) while keeping a dedicated resolver test strict about the absolute wrapper path.
- Real smoke tests must enter through the default bare command and confirm one meaningful protocol response/version; injecting an already-resolved path bypasses the resolver under test.
Pitfalls
- Reinstalling a package before proving it is absent.
- Checking only
command -v,where, orwhichand assuming the application launches commands identically. - Using
shell=Truemerely to inherit shell lookup; this changes quoting and injection boundaries. - Fixing only the version check while leaving the actual app-server/daemon launch on the broken bare name.
- Hard-coding one machine’s global package path instead of resolving through the parent process environment.
- Calling every
FileNotFoundErrora PATH problem; direct process creation may not apply shell aliases, functions, or wrapper-extension rules. - Duplicating install hints at multiple error-wrapping layers; preserve one root diagnostic plus one actionable remedy.
Reference
See references/windows-npm-shims.md for the Windows npm .cmd differential probe and regression guidance.
04 同仓库更多 Skills
web-retrieval-resilience
Web tools fail or search rules break: repair or fall back.
blocked-page-recovery
Use when a fetch fails: 403/429, paywall, WAF, bot wall.
verification-loop
Run a six-phase verification of a Claude Code session's work — build, type check, lint, tests with coverage, security grep, and diff review — then produce a PASS/FAIL verification report. Use when verifying work after completing a feature or refactor, before creating a PR, or when quality gates must pass.
unreal-world-builder
开放世界与环境专家——精通 UE5 World Partition、Landscape、程序化植被、HLOD 和大规模关卡流式加载,打造无缝开放世界体验
unreal-technical-artist
Unreal Engine 视觉管线专家——精通材质编辑器、Niagara 特效、程序化内容生成和 UE5 项目的美术到引擎管线
unreal-systems-engineer
性能与混合架构专家——精通 C++/Blueprint 边界、Nanite 几何体、Lumen GI 和 Gameplay Ability System,面向 AAA 级 Unreal Engine 项目