fix(non-interactive-env): detect shell type for csh/tcsh env var syntax (fixes #2089)

This commit is contained in:
MoerAI
2026-03-23 19:33:54 +09:00
parent 331f7ec52b
commit 92509d8cfb
2 changed files with 17 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
import type { PluginInput } from "@opencode-ai/plugin"
import { HOOK_NAME, NON_INTERACTIVE_ENV, SHELL_COMMAND_PATTERNS } from "./constants"
import { log, buildEnvPrefix } from "../../shared"
import { log, buildEnvPrefix, detectShellType } from "../../shared"
export * from "./constants"
export * from "./detector"
@@ -52,9 +52,9 @@ export function createNonInteractiveEnvHook(_ctx: PluginInput) {
// The env vars (GIT_EDITOR=:, EDITOR=:, etc.) must ALWAYS be injected
// for git commands to prevent interactive prompts.
// The bash tool always runs in a Unix-like shell (bash/sh), even on Windows
// (via Git Bash, WSL, etc.), so always use unix export syntax.
const envPrefix = buildEnvPrefix(NON_INTERACTIVE_ENV, "unix")
// Detect the current shell type for proper env var syntax (export for bash/zsh, setenv for csh/tcsh, etc.)
const shellType = detectShellType()
const envPrefix = buildEnvPrefix(NON_INTERACTIVE_ENV, shellType)
// Check if the command already starts with the prefix to avoid stacking.
// This maintains the non-interactive behavior and makes the operation idempotent.

View File

@@ -1,4 +1,4 @@
export type ShellType = "unix" | "powershell" | "cmd"
export type ShellType = "unix" | "powershell" | "cmd" | "csh"
/**
* Detect the current shell type based on environment variables.
@@ -14,6 +14,10 @@ export function detectShellType(): ShellType {
}
if (process.env.SHELL) {
const shell = process.env.SHELL
if (shell.includes("csh") || shell.includes("tcsh")) {
return "csh"
}
return "unix"
}
@@ -34,6 +38,7 @@ export function shellEscape(value: string, shellType: ShellType): string {
switch (shellType) {
case "unix":
case "csh":
if (/[^a-zA-Z0-9_\-.:\/]/.test(value)) {
return `'${value.replace(/'/g, "'\\''")}'`
}
@@ -91,6 +96,13 @@ export function buildEnvPrefix(
return `export ${assignments};`
}
case "csh": {
const assignments = entries
.map(([key, value]) => `setenv ${key} ${shellEscape(value, shellType)}`)
.join("; ")
return `${assignments};`
}
case "powershell": {
const assignments = entries
.map(([key, value]) => `$env:${key}=${shellEscape(value, shellType)}`)