From 2d90a7089e29e910a0ee826cf34ca7840d06bae3 Mon Sep 17 00:00:00 2001 From: sean Date: Sat, 14 Jun 2025 22:10:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D.promptx=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E5=AE=9A=E4=BD=8Dbug=EF=BC=8C=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E5=88=B0=E7=94=A8=E6=88=B7=E4=B8=BB=E7=9B=AE?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 修复的问题: - PROMPTX_WORKSPACE为空时错误定位到~/目录 - 缺少对现有.promptx目录的智能发现 - 目录定位策略优先级不够合理 ✨ 改进后的策略优先级: 1. WORKSPACE_FOLDER_PATHS (IDE标准环境变量) 2. PROMPTX_WORKSPACE (仅当明确配置且非空时) 3. 向上查找现有.promptx目录 (复用现有项目配置) 4. PWD环境变量 5. 项目根目录智能推测 (package.json, .git等) 6. process.cwd()回退 🔧 技术改进: - 新增findExistingPromptxDirectory()函数专门处理现有.promptx目录发现 - 优化findProjectRoot()不再优先查找.promptx,避免重复逻辑 - 改进PROMPTX_WORKSPACE空值处理,避免expandHome('')返回主目录 - 增强目录定位的智能性和可靠性 ✅ 测试验证: - 空PROMPTX_WORKSPACE不再错误定位 - 子目录中正确向上查找项目根目录 - 现有.promptx目录优先被发现和复用 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/lib/utils/executionContext.js | 63 ++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/src/lib/utils/executionContext.js b/src/lib/utils/executionContext.js index 2031c28..2e04022 100644 --- a/src/lib/utils/executionContext.js +++ b/src/lib/utils/executionContext.js @@ -44,33 +44,75 @@ function getMCPWorkingDirectory() { } } - // 策略2:PROMPTX_WORKSPACE(PromptX专用环境变量) - const promptxWorkspace = normalizePath(expandHome(process.env.PROMPTX_WORKSPACE || '')); - if (promptxWorkspace && isValidDirectory(promptxWorkspace)) { - console.error(`[执行上下文] 使用PROMPTX_WORKSPACE: ${promptxWorkspace}`); - return promptxWorkspace; + // 策略2:PROMPTX_WORKSPACE(PromptX专用环境变量,仅当明确配置且非空时使用) + const promptxWorkspaceEnv = process.env.PROMPTX_WORKSPACE; + if (promptxWorkspaceEnv && promptxWorkspaceEnv.trim() !== '') { + const promptxWorkspace = normalizePath(expandHome(promptxWorkspaceEnv)); + if (isValidDirectory(promptxWorkspace)) { + console.error(`[执行上下文] 使用PROMPTX_WORKSPACE: ${promptxWorkspace}`); + return promptxWorkspace; + } } - // 策略3:PWD环境变量(某些情况下可用) + // 策略3:向上查找现有.promptx目录(复用现有项目配置) + const existingPrompxRoot = findExistingPromptxDirectory(process.cwd()); + if (existingPrompxRoot) { + console.error(`[执行上下文] 发现现有.promptx目录: ${existingPrompxRoot}`); + return existingPrompxRoot; + } + + // 策略4:PWD环境变量(某些情况下可用) const pwd = process.env.PWD; if (pwd && isValidDirectory(pwd) && pwd !== process.cwd()) { console.error(`[执行上下文] 使用PWD环境变量: ${pwd}`); return pwd; } - // 策略4:项目根目录智能推测(向上查找项目标识) + // 策略5:项目根目录智能推测(向上查找项目标识) const projectRoot = findProjectRoot(process.cwd()); if (projectRoot && projectRoot !== process.cwd()) { console.error(`[执行上下文] 智能推测项目根目录: ${projectRoot}`); return projectRoot; } - // 策略5:回退到process.cwd() + // 策略6:回退到process.cwd() console.error(`[执行上下文] 回退到process.cwd(): ${process.cwd()}`); console.error(`[执行上下文] 提示:建议在MCP配置中添加 "env": {"PROMPTX_WORKSPACE": "你的项目目录"}`) return process.cwd(); } +/** + * 向上查找现有的.promptx目录 + * @param {string} startDir 开始查找的目录 + * @returns {string|null} 包含.promptx目录的父目录路径或null + */ +function findExistingPromptxDirectory(startDir) { + let currentDir = path.resolve(startDir); + const root = path.parse(currentDir).root; + + while (currentDir !== root) { + // 检查当前目录是否包含.promptx目录 + const promptxPath = path.join(currentDir, '.promptx'); + if (fs.existsSync(promptxPath)) { + try { + const stat = fs.statSync(promptxPath); + if (stat.isDirectory()) { + return currentDir; + } + } catch { + // 忽略权限错误等,继续查找 + } + } + + // 向上一级目录 + const parentDir = path.dirname(currentDir); + if (parentDir === currentDir) break; // 防止无限循环 + currentDir = parentDir; + } + + return null; +} + /** * 向上查找项目根目录 * @param {string} startDir 开始查找的目录 @@ -78,7 +120,6 @@ function getMCPWorkingDirectory() { */ function findProjectRoot(startDir) { const projectMarkers = [ - '.promptx', 'package.json', '.git', 'pyproject.toml', @@ -167,5 +208,7 @@ function expandHome(filepath) { module.exports = { getExecutionContext, isValidDirectory, - getDebugInfo + getDebugInfo, + findExistingPromptxDirectory, + findProjectRoot }; \ No newline at end of file