eyaltoledano/claude-task-master · error

INVALID_TARGET_DIRECTORY

INVALID_TARGET_DIRECTORY

Error message

Cannot initialize project: Invalid target directory '${targetDirectory}' received. Please ensure a valid workspace/folder is open or specified.

What it means

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.

Source

Thrown at mcp-server/src/core/direct-functions/initialize-project.js:43

	// --- Determine Target Directory ---
	// TRUST the projectRoot passed from the tool layer via args
	// The HOF in the tool layer already normalized and validated it came from a reliable source (args or session)
	const targetDirectory = args.projectRoot;

	// --- Validate the targetDirectory (basic sanity checks) ---
	if (
		!targetDirectory ||
		typeof targetDirectory !== 'string' || // Ensure it's a string
		targetDirectory === '/' ||
		targetDirectory === homeDir
	) {
		log.error(
			`Invalid target directory received from tool layer: '${targetDirectory}'`
		);
		return {
			success: false,
			error: {
				code: 'INVALID_TARGET_DIRECTORY',
				message: `Cannot initialize project: Invalid target directory '${targetDirectory}' received. Please ensure a valid workspace/folder is open or specified.`,
				details: `Received args.projectRoot: ${args.projectRoot}` // Show what was received
			}
		};
	}

	// --- Proceed with validated targetDirectory ---
	log.info(`Validated target directory for initialization: ${targetDirectory}`);

	const originalCwd = process.cwd();
	let resultData;
	let success = false;
	let errorResult = null;

	log.info(
		`Temporarily changing CWD to ${targetDirectory} for initialization.`
	);
	process.chdir(targetDirectory); // Change CWD to the HOF-provided root

View on GitHub (pinned to c0c98d367c)

Solutions

  1. Pass a valid absolute directory path as projectRoot in the tool arguments
  2. In IDE integrations, open the workspace folder or configure the default project root in the MCP server config
  3. Check the error's details field — it echoes the exact args.projectRoot received — and correct that value
  4. Ensure the directory exists and is readable before initializing

Example fix

// before
await mcp.call('initialize_project', {});
// after
await mcp.call('initialize_project', { projectRoot: '/home/me/my-project' });
Defensive patterns

Strategy: validation

Validate before calling

import fs from 'fs';
function assertValidProjectRoot(projectRoot) {
  if (typeof projectRoot !== 'string' || projectRoot.trim() === '') {
    throw new Error('projectRoot is required: open a workspace or pass an absolute directory path');
  }
  fs.accessSync(projectRoot, fs.constants.R_OK | fs.constants.W_OK);
}

Type guard

function isValidProjectRoot(p) {
  return typeof p === 'string' && p.trim().length > 0 &&
    (() => { try { return fs.statSync(p).isDirectory(); } catch { return false; } })();
}

Try / catch

const res = await callTool('initialize_project', { projectRoot });
if (!res.success && res.error?.code === 'INVALID_TARGET_DIRECTORY') {
  console.error(res.error.message, res.error.details); // details echoes received args.projectRoot
}

Prevention

When it happens

Trigger: Calling initialize_project without projectRoot, with an empty string, or with a value the direct function's path resolution (getProjectRoot / path checks) rejects.

Common situations: 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.

Related errors


AI-assisted analysis of eyaltoledano/claude-task-master@c0c98d367c (2026-08-29). Data as JSON: /api/errors/6e7d8d4f3907c7c5. Report an issue: GitHub.