Skip to content

Commit 6cc521a

Browse files
Micro66qdaxb
authored andcommitted
refactor: Optimize the Zsh script injection logic to avoid conflicts with JetBrains.
1 parent 0ff159e commit 6cc521a

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

jetbrains_plugin/src/main/kotlin/com/sina/weibo/agent/terminal/WeCoderTerminalCustomizer.kt

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,25 +161,40 @@ class WeCoderTerminalCustomizer : LocalTerminalCustomizer() {
161161
}
162162

163163
/**
164-
* Inject VSCode integration script for Zsh
164+
* Inject VSCode integration script for Zsh (safe with JetBrains shell integration)
165165
*/
166-
private fun injectZshScript(command: Array<String>, envs: MutableMap<String, String>, scriptPath: String): Array<String> {
167-
// Save user's original ZDOTDIR environment variable
168-
val userZdotdir = envs["ZDOTDIR"]
169-
?: System.getenv("ZDOTDIR")
170-
?: System.getProperty("user.home")
171-
172-
// 🔧 Protect the JETBRAINS_INTELLIJ_ZSH_DIR environment variable to prevent it from being cleared.
166+
private fun injectZshScript(
167+
command: Array<String>,
168+
envs: MutableMap<String, String>,
169+
scriptPath: String
170+
): Array<String> {
171+
// 1) 如果 JetBrains 自带的 zsh shell integration 已经在场,就不要去改 ZDOTDIR,避免冲突
173172
val jetbrainsZshDir = envs["JETBRAINS_INTELLIJ_ZSH_DIR"] ?: System.getenv("JETBRAINS_INTELLIJ_ZSH_DIR")
174-
if (jetbrainsZshDir != null) {
175-
envs["JETBRAINS_INTELLIJ_ZSH_DIR"] = jetbrainsZshDir
176-
logger.info("🔧 Preserved JETBRAINS_INTELLIJ_ZSH_DIR: $jetbrainsZshDir")
173+
val shellExeName = File(command[0]).name
174+
val looksLikeJbZsh = command[0].contains("/plugins/terminal/shell-integrations/zsh")
175+
176+
if (jetbrainsZshDir != null || looksLikeJbZsh) {
177+
logger.info("🔒 Detected JetBrains Zsh integration (JETBRAINS_INTELLIJ_ZSH_DIR=$jetbrainsZshDir, looksLikeJbZsh=$looksLikeJbZsh). Skip overriding ZDOTDIR.")
178+
// 仍然保留用户原始 ZDOTDIR 到环境,便于脚本内按需使用
179+
val userZdotdir = envs["ZDOTDIR"] ?: System.getenv("ZDOTDIR") ?: System.getProperty("user.home")
180+
envs["USER_ZDOTDIR"] = userZdotdir
181+
return command
177182
}
178183

184+
// 2) 只有当 scriptPath 看起来是一个有效的 ZDOTDIR(至少包含 .zshrc)时才注入
185+
val dir = File(scriptPath)
186+
val hasZshrc = File(dir, ".zshrc").exists()
187+
if (!dir.isDirectory || !hasZshrc) {
188+
logger.warn("🚫 Zsh script dir '$scriptPath' is invalid (dir=$dir, hasZshrc=$hasZshrc). Skip overriding ZDOTDIR.")
189+
return command
190+
}
191+
192+
// 3) 记录并安全覆写
193+
val userZdotdir = envs["ZDOTDIR"] ?: System.getenv("ZDOTDIR") ?: System.getProperty("user.home")
179194
envs["USER_ZDOTDIR"] = userZdotdir
180195
envs["ZDOTDIR"] = scriptPath
181-
182-
logger.info("🔧 Saved original ZDOTDIR: $userZdotdir, set new ZDOTDIR: $scriptPath")
196+
197+
logger.info("🔧 Set ZDOTDIR to '$scriptPath' (saved original as USER_ZDOTDIR='$userZdotdir'), shell=$shellExeName")
183198
return command
184199
}
185200

0 commit comments

Comments
 (0)