fix: 修复.promptx目录定位bug,防止定位到用户主目录

🐛 修复的问题:
- 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 <noreply@anthropic.com>
This commit is contained in:
sean
2025-06-14 22:10:47 +08:00
parent 63ac467611
commit 2d90a7089e

View File

@ -44,33 +44,75 @@ function getMCPWorkingDirectory() {
}
}
// 策略2PROMPTX_WORKSPACEPromptX专用环境变量
const promptxWorkspace = normalizePath(expandHome(process.env.PROMPTX_WORKSPACE || ''));
if (promptxWorkspace && isValidDirectory(promptxWorkspace)) {
console.error(`[执行上下文] 使用PROMPTX_WORKSPACE: ${promptxWorkspace}`);
return promptxWorkspace;
// 策略2PROMPTX_WORKSPACEPromptX专用环境变量,仅当明确配置且非空时使用
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;
}
}
// 策略3PWD环境变量某些情况下可用
// 策略3向上查找现有.promptx目录复用现有项目配置
const existingPrompxRoot = findExistingPromptxDirectory(process.cwd());
if (existingPrompxRoot) {
console.error(`[执行上下文] 发现现有.promptx目录: ${existingPrompxRoot}`);
return existingPrompxRoot;
}
// 策略4PWD环境变量某些情况下可用
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
};