eyaltoledano/claude-task-master · error

projectRoot is required for getCurrentBranch

Error message

projectRoot is required for getCurrentBranch

What it means

getCurrentBranch executes 'git rev-parse --abbrev-ref HEAD' with cwd set to projectRoot; an empty projectRoot makes that impossible, so the guard throws this error before shelling out. It is a fail-fast argument check for the branch-lookup helper.

Source

Thrown at packages/tm-core/src/common/utils/git-utils.ts:62

	try {
		execSync('git rev-parse --git-dir', {
			cwd: projectRoot,
			stdio: 'ignore'
		});
		return true;
	} catch (error) {
		return false;
	}
}

/**
 * Get the current git branch name
 */
export async function getCurrentBranch(
	projectRoot: string
): Promise<string | null> {
	if (!projectRoot) {
		throw new Error('projectRoot is required for getCurrentBranch');
	}

	try {
		const { stdout } = await execAsync('git rev-parse --abbrev-ref HEAD', {
			cwd: projectRoot
		});
		return stdout.trim();
	} catch (error) {
		return null;
	}
}

/**
 * Synchronous get current git branch name
 */
export function getCurrentBranchSync(projectRoot: string): string | null {
	if (!projectRoot) {
		return null;

View on GitHub (pinned to c0c98d367c)

Solutions

  1. Supply the resolved project directory: getCurrentBranch(projectRoot || process.cwd())
  2. Ensure the caller resolved projectRoot before invoking branch helpers (resolve with path.resolve(...))
  3. Fix config loading so the project root key is always populated
  4. If callers cannot guarantee a root, guard: if (!projectRoot) return null; instead of calling

Example fix

// before
const branch = await getCurrentBranch(undefined as any);
// after
const root = projectRoot || process.cwd();
const branch = await getCurrentBranch(root);
Defensive patterns

Strategy: validation

Validate before calling

if (typeof projectRoot !== 'string' || !projectRoot.trim()) {
  throw new Error('getCurrentBranch needs a non-empty projectRoot');
}
// optionally verify it is a repo first:
if (!(await isGitRepository(projectRoot))) return null;

Type guard

function isNonEmptyString(v: unknown): v is string {
  return typeof v === 'string' && v.trim().length > 0;
}

Try / catch

try {
  const branch = await getCurrentBranch(projectRoot);
} catch (err) {
  if (err instanceof Error && err.message.includes('getCurrentBranch')) {
    console.error('Missing projectRoot; defaulting to process.cwd().');
    return getCurrentBranch(process.cwd());
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling getCurrentBranch(undefined) or '' — usually projectRoot was never resolved from config or CLI args; also triggered indirectly via the currentBranch/defaultBranch wrappers when they receive an empty root.

Common situations: Running git-dependent commands outside a configured project; config files missing the project root key; race where root detection failed silently earlier and '' propagated downstream.

Understand the failure class

Background: Missing required parameter errors: what 'X is required' and 'the required X param is missing' mean, and how to fix them — this error's family across 27 libraries.

Related errors


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