{"record":{"id":"53221dbab99ceec1","repo":"mastra-ai/mastra","slug":"could-not-find-mastracode-package-root-from-star","errorCode":null,"errorMessage":"Could not find mastracode package root from ${startDir}","messagePattern":"Could not find mastracode package root from (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/plugins/package-link.ts","lineNumber":56,"sourceCode":"      return currentDir;\n    }\n    if (packageName === '@mastra/code-sdk') {\n      // The `mastracode` package sits next to the sdk: mastracode/sdk ↔\n      // mastracode/tui in the source tree, node_modules/@mastra/code-sdk ↔\n      // node_modules/mastracode when installed.\n      for (const candidate of [\n        path.resolve(currentDir, '..', 'tui'),\n        path.resolve(currentDir, '..', '..', 'mastracode'),\n      ]) {\n        if (readPackageName(candidate) === 'mastracode') {\n          return candidate;\n        }\n      }\n    }\n\n    const parentDir = path.dirname(currentDir);\n    if (parentDir === currentDir) {\n      throw new Error(`Could not find mastracode package root from ${startDir}`);\n    }\n    currentDir = parentDir;\n  }\n}\n\nexport function ensureMastraCodePackageLink(pluginDir: string): void {\n  if (declaresInstallableMastraCodeDependency(pluginDir)) {\n    return;\n  }\n\n  const packageRoot = mastraCodePackageRoot();\n  const nodeModulesDir = path.join(pluginDir, 'node_modules');\n  const linkPath = path.join(nodeModulesDir, 'mastracode');\n  try {\n    if (fs.realpathSync(linkPath) === fs.realpathSync(packageRoot)) {\n      return;\n    }\n    fs.rmSync(linkPath, { recursive: true, force: true });","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/plugins/package-link.ts#L38-L74","documentation":"findMastraCodePackageRoot walks upward from a start directory looking for the mastracode package root (identified by a marker such as package.json containing the package name). If it reaches the filesystem root without finding the marker, it throws this error, because plugin linking cannot proceed without knowing where the mastracode package lives.","triggerScenarios":"Calling mastraCodePackageRoot/mastracodePackageRoot (via ensureMastraCodePackageLink) from a plugin directory that is not inside a workspace containing the mastracode package — e.g. a plugin installed in a global node_modules or an isolated temp dir.","commonSituations":"Running the plugin from outside the monorepo/project that contains mastracode; using a globally installed CLI whose plugin dir is not nested under the package; an incomplete install where mastracode's package.json is missing; symlinked plugin dirs that resolve to a path outside the package tree.","solutions":["Run the command from within the project/monorepo that contains the mastracode package so the upward search finds its root.","Install mastracode as a dependency of the project so its package root exists under node_modules and is discoverable.","If using symlinks, ensure they resolve into a directory inside the mastracode package tree.","Reinstall/repair the mastracode package if its root package.json is missing (e.g. delete node_modules/mastracode and reinstall)."],"exampleFix":"// before — running from an isolated dir\n$ cd /tmp/my-plugin && mastracode link\n// Error: Could not find mastracode package root from /tmp/my-plugin\n// after — run inside the project that depends on mastracode\n$ cd ~/my-project/plugins/my-plugin && mastracode link","handlingStrategy":"validation","validationCode":"import { createRequire } from 'module';\nconst require = createRequire(import.meta.url);\ntry {\n  const pkgPath = require.resolve('mastracode/package.json');\n  console.log('mastracode root:', path.dirname(pkgPath));\n} catch {\n  throw new Error('mastracode is not resolvable from this project; install it first (pnpm/npm install mastracode)');\n}","typeGuard":null,"tryCatchPattern":"try {\n  ensureMastraCodePackageLink(pluginDir);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('Could not find mastracode package root')) {\n    console.error(`Run this inside a project that has mastracode installed: ${err.message}`);\n    process.exitCode = 1;\n  } else throw err;\n}","preventionTips":["Always run plugin link/scaffold commands from within the project that depends on mastracode.","Install mastracode as a local dependency instead of relying on global installs.","Check `node -e \"require.resolve('mastracode/package.json')\"` succeeds before automating the link step.","Avoid symlinking plugin dirs to paths outside the package tree."],"tags":["filesystem","package-resolution","plugins"],"backgroundTag":"package-root-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}