{"record":{"id":"6e7d8d4f3907c7c5","repo":"eyaltoledano/claude-task-master","slug":"invalid-target-directory","errorCode":"INVALID_TARGET_DIRECTORY","errorMessage":"Cannot initialize project: Invalid target directory '${targetDirectory}' received. Please ensure a valid workspace/folder is open or specified.","messagePattern":"Cannot initialize project: Invalid target directory '(.+?)' received\\. Please ensure a valid workspace/folder is open or specified\\.","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mcp-server/src/core/direct-functions/initialize-project.js","lineNumber":43,"sourceCode":"\t// --- Determine Target Directory ---\n\t// TRUST the projectRoot passed from the tool layer via args\n\t// The HOF in the tool layer already normalized and validated it came from a reliable source (args or session)\n\tconst targetDirectory = args.projectRoot;\n\n\t// --- Validate the targetDirectory (basic sanity checks) ---\n\tif (\n\t\t!targetDirectory ||\n\t\ttypeof targetDirectory !== 'string' || // Ensure it's a string\n\t\ttargetDirectory === '/' ||\n\t\ttargetDirectory === homeDir\n\t) {\n\t\tlog.error(\n\t\t\t`Invalid target directory received from tool layer: '${targetDirectory}'`\n\t\t);\n\t\treturn {\n\t\t\tsuccess: false,\n\t\t\terror: {\n\t\t\t\tcode: 'INVALID_TARGET_DIRECTORY',\n\t\t\t\tmessage: `Cannot initialize project: Invalid target directory '${targetDirectory}' received. Please ensure a valid workspace/folder is open or specified.`,\n\t\t\t\tdetails: `Received args.projectRoot: ${args.projectRoot}` // Show what was received\n\t\t\t}\n\t\t};\n\t}\n\n\t// --- Proceed with validated targetDirectory ---\n\tlog.info(`Validated target directory for initialization: ${targetDirectory}`);\n\n\tconst originalCwd = process.cwd();\n\tlet resultData;\n\tlet success = false;\n\tlet errorResult = null;\n\n\tlog.info(\n\t\t`Temporarily changing CWD to ${targetDirectory} for initialization.`\n\t);\n\tprocess.chdir(targetDirectory); // Change CWD to the HOF-provided root","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/eyaltoledano/claude-task-master/blob/c0c98d367c55296bfe69e65680625b6db437af02/mcp-server/src/core/direct-functions/initialize-project.js#L25-L61","documentation":"initializeProjectDirect validates the targetDirectory handed down from the tool layer before initializing a Task Master project. If it is empty, null, or otherwise invalid, the tool returns INVALID_TARGET_DIRECTORY with a details field showing the raw args.projectRoot that was received, instructing the user to open or specify a valid workspace folder.","triggerScenarios":"Calling initialize_project without projectRoot, with an empty string, or with a value the direct function's path resolution (getProjectRoot / path checks) rejects.","commonSituations":"Standalone MCP clients (e.g. Claude Desktop) with no workspace folder open; config where projectRoot was never set; template configs leaving the placeholder '{{projectRoot}}' unfilled; IDE extension not forwarding the workspace path.","solutions":["Pass a valid absolute directory path as projectRoot in the tool arguments","In IDE integrations, open the workspace folder or configure the default project root in the MCP server config","Check the error's details field — it echoes the exact args.projectRoot received — and correct that value","Ensure the directory exists and is readable before initializing"],"exampleFix":"// before\nawait mcp.call('initialize_project', {});\n// after\nawait mcp.call('initialize_project', { projectRoot: '/home/me/my-project' });","handlingStrategy":"validation","validationCode":"import fs from 'fs';\nfunction assertValidProjectRoot(projectRoot) {\n  if (typeof projectRoot !== 'string' || projectRoot.trim() === '') {\n    throw new Error('projectRoot is required: open a workspace or pass an absolute directory path');\n  }\n  fs.accessSync(projectRoot, fs.constants.R_OK | fs.constants.W_OK);\n}","typeGuard":"function isValidProjectRoot(p) {\n  return typeof p === 'string' && p.trim().length > 0 &&\n    (() => { try { return fs.statSync(p).isDirectory(); } catch { return false; } })();\n}","tryCatchPattern":"const res = await callTool('initialize_project', { projectRoot });\nif (!res.success && res.error?.code === 'INVALID_TARGET_DIRECTORY') {\n  console.error(res.error.message, res.error.details); // details echoes received args.projectRoot\n}","preventionTips":["Always pass an absolute, existing directory as projectRoot","For IDE integrations, verify a workspace folder is open before calling","Fill in MCP config placeholders ({{projectRoot}}) rather than sending them literally","Log outgoing arguments so bad values are visible before the call"],"tags":["mcp","validation","configuration"],"backgroundTag":"missing-project-root","analyzedSha":"c0c98d367c55296bfe69e65680625b6db437af02","analyzedAt":"2026-08-29T02:56:26.071Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}