fix(plugins): use cmd.exe on Windows for shell hooks#468
fix(plugins): use cmd.exe on Windows for shell hooks#468jmikedupont2 wants to merge 1 commit intomoltis-org:mainfrom
Conversation
Shell hooks hardcoded Command::new("sh") which fails on Windows
with 'program not found'. Now uses cmd /C on Windows, sh -c elsewhere.
Greptile SummaryThis PR adds Windows support to However, the test suite was not updated to reflect the new Windows code path:
Confidence Score: 3/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[ShellHookHandler::handle called] --> B[Serialize HookPayload to JSON]
B --> C{cfg! target_os = windows?}
C -- Windows --> D[Command::new cmd.exe]
D --> E[arg /C, arg command]
C -- Unix/Other --> F[Command::new sh]
F --> G[arg -c, arg command]
E --> H[Set envs, stdin/stdout/stderr piped]
G --> H
H --> I{working_dir set?}
I -- Yes --> J[cmd.current_dir]
I -- No --> K[Spawn child process]
J --> K
K --> L[Write JSON payload to stdin]
L --> M[Wait with timeout]
M -- Timeout --> N[Return Err timeout]
M -- exit 1 --> O[Return Ok Block reason]
M -- exit != 0 --> P[Return Err non-zero exit]
M -- exit 0 + empty stdout --> Q[Return Ok Continue]
M -- exit 0 + JSON stdout --> R{Parse ShellHookResponse}
R -- action = modify --> S[Return Ok ModifyPayload]
R -- other / parse error --> Q
|
| let mut cmd = if cfg!(target_os = "windows") { | ||
| let mut c = Command::new("cmd"); | ||
| c.arg("/C").arg(&self.command); | ||
| c | ||
| } else { | ||
| let mut c = Command::new("sh"); | ||
| c.arg("-c").arg(&self.command); | ||
| c | ||
| }; |
There was a problem hiding this comment.
cmd.exe invoked without full path — prefer explicit binary name
Command::new("cmd") relies on cmd being resolvable via PATH. While cmd.exe is normally on the PATH on all Windows installations, using "cmd.exe" (with the .exe extension) is the Windows convention and avoids any edge-case ambiguity where a user might have a cmd script or binary earlier in their PATH.
| let mut cmd = if cfg!(target_os = "windows") { | |
| let mut c = Command::new("cmd"); | |
| c.arg("/C").arg(&self.command); | |
| c | |
| } else { | |
| let mut c = Command::new("sh"); | |
| c.arg("-c").arg(&self.command); | |
| c | |
| }; | |
| let mut cmd = if cfg!(target_os = "windows") { | |
| let mut c = Command::new("cmd.exe"); | |
| c.arg("/C").arg(&self.command); | |
| c | |
| } else { | |
| let mut c = Command::new("sh"); | |
| c.arg("-c").arg(&self.command); | |
| c | |
| }; |
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Shell hooks fail on Windows because
sh -cisn't available. This usescmd.exe /Con Windows targets.Changes
cmd.exe /Cinstead ofsh -cfor shell hook executionTesting