{"record":{"id":"fd5c9eeb3c5eb83e","repo":"facebook/docusaurus","slug":"couldn-t-check-if-this-directory-is-within-a-git-w","errorCode":null,"errorMessage":"Couldn't check if this directory is within a Git worktree: ${cwd}","messagePattern":"Couldn't check if this directory is within a Git worktree: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/docusaurus-utils/src/vcs/gitUtils.ts","lineNumber":271,"sourceCode":"): Promise<GitCommitInfo | null> {\n  return getGitCommitInfo(filePath, 'newest');\n}\n\nexport async function getGitCreation(\n  filePath: string,\n): Promise<GitCommitInfo | null> {\n  return getGitCommitInfo(filePath, 'oldest');\n}\n\nexport async function isGitInsideWorktree(cwd: string): Promise<boolean> {\n  try {\n    const result = await execa('git', ['rev-parse', '--is-inside-work-tree'], {\n      cwd,\n      reject: false,\n    });\n    return result.exitCode === 0;\n  } catch (error) {\n    throw new Error(\n      `Couldn't check if this directory is within a Git worktree: ${cwd}`,\n      {cause: error},\n    );\n  }\n}\n\nexport async function getGitRepoRoot(cwd: string): Promise<string> {\n  const createErrorMessageBase = () => {\n    return `Couldn't find the git repository root directory\nFailure while running ${logger.code(\n      'git rev-parse --show-toplevel',\n    )} from cwd=${logger.path(cwd)}`;\n  };\n\n  const result = await execa('git', ['rev-parse', '--show-toplevel'], {\n    cwd,\n  }).catch((error) => {\n    // We enter this rejection when cwd is not a dir for example","sourceCodeStart":253,"sourceCodeEnd":289,"githubUrl":"https://github.com/facebook/docusaurus/blob/3f483e80e326cc646b54b83d564b3f0c4881b9a6/packages/docusaurus-utils/src/vcs/gitUtils.ts#L253-L289","documentation":"Thrown by isGitInsideWorktree() when the underlying execa('git', ['rev-parse', '--is-inside-work-tree']) call itself rejects (note: reject:false is set, so a normal non-zero exit is captured and returns false; this catch is only entered when execa cannot spawn the process at all). The cause is the original spawn error. The function's purpose is to detect whether cwd is inside a git worktree, so a failure to even run git is wrapped with the offending cwd.","triggerScenarios":"Calling isGitInsideWorktree(cwd) in an environment where git is not installed (spawn ENOENT), or where cwd does not exist / is not a directory / cannot be entered, causing execa to throw before producing a result. Because reject:false only converts non-zero git exits into return values, only spawn-level failures reach this catch.","commonSituations":"git is not installed (same root cause as error 93 but surfaced from a different code path). The cwd argument points at a deleted or inaccessible directory. Running in a constrained sandbox that blocks process spawning. Permissions prevent entering the directory passed as cwd.","solutions":["Install git and verify `git --version` works (see error 93 fixes).","Confirm the cwd passed to isGitInsideWorktree exists, is a directory, and the current process can enter it.","If the call site cannot guarantee a usable cwd, guard with fs.pathExists(cwd) and skip the check when the directory is invalid.","Wrap the call in try/catch and treat the failure as 'not in a worktree' if that is a safe degradation for your use case."],"exampleFix":"// before\nconst inside = await isGitInsideWorktree(cwd);\n\n// after — guard cwd and tolerate spawn failures\nif (!(await fs.pathExists(cwd))) return false;\nlet inside = false;\ntry {\n  inside = await isGitInsideWorktree(cwd);\n} catch (err) {\n  console.warn(`Could not determine git worktree status for ${cwd}: ${err.cause ?? err}`);\n}\nreturn inside;","handlingStrategy":"validation","validationCode":"import fs from 'fs-extra';\nimport { execaSync } from 'execa';\n\nfunction gitBinaryAvailable(): boolean {\n  try { execaSync('git', ['--version']); return true; } catch { return false; }\n}\n\nasync function canCheckWorktree(cwd: string): Promise<boolean> {\n  return gitBinaryAvailable() && (await fs.stat(cwd)).isDirectory();\n}\n\nif (!(await canCheckWorktree(cwd))) {\n  throw new Error(`Cannot check git worktree: git missing or cwd invalid: ${cwd}`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await isGitInsideWorktree(cwd);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith(\"Couldn't check if this directory is within a Git worktree\")) {\n    // err.cause is the spawn error (often ENOENT — git not installed, or cwd invalid)\n    // treat as 'not in a worktree' if that is a safe degradation\n  }\n  throw err;\n}","preventionTips":["Install git in every environment that runs the build.","Validate the cwd argument (exists and is a directory) before calling isGitInsideWorktree.","When the check is non-critical, wrap it in try/catch and default to false on spawn failure."],"tags":["git","environment","worktree","spawn","dependency"],"backgroundTag":null,"analyzedSha":"3f483e80e326cc646b54b83d564b3f0c4881b9a6","analyzedAt":"2026-08-12T13:25:04.382Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}