{"record":{"id":"fb44cad4ccabafd1","repo":"mastra-ai/mastra","slug":"copyfile-src-dest-failed-exit-result-e","errorCode":null,"errorMessage":"copyFile ${src} -> ${dest} failed (exit ${result.exitCode}): ${result.stderr.trim()}","messagePattern":"copyFile (.+?) -> (.+?) failed \\(exit (.+?)\\): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/agents/sandbox-filesystem.ts","lineNumber":349,"sourceCode":"          `src=${shellQuote(srcAbs)}`,\n          `dest=${shellQuote(destAbs)}`,\n          `if [ ! -e \"$src\" ] && [ ! -L \"$src\" ]; then exit ${EXIT_NOT_FOUND}; fi`,\n          `mkdir -p ${shellQuote(posixPath.dirname(destAbs))} || exit 1`,\n          `if [ -d \"$src\" ]; then`,\n          `  mkdir \"$dest\" 2>/dev/null || exit ${EXIT_EXISTS}`,\n          `  cp -R \"$src\"/. \"$dest\"/`,\n          `else`,\n          `  tmp=\"$dest.__cptmp$$\"`,\n          `  cp \"$src\" \"$tmp\" || exit 1`,\n          `  ln \"$tmp\" \"$dest\" 2>/dev/null || { rm -f \"$tmp\"; [ -e \"$dest\" ] && exit ${EXIT_EXISTS} || exit 1; }`,\n          `  rm -f \"$tmp\"`,\n          `fi`,\n        ].join('\\n'),\n      );\n      if (result.exitCode === EXIT_NOT_FOUND) throw new FileNotFoundError(src);\n      if (result.exitCode === EXIT_EXISTS) throw new FileExistsError(dest);\n      if (result.exitCode !== 0) {\n        throw new Error(`copyFile ${src} -> ${dest} failed (exit ${result.exitCode}): ${result.stderr.trim()}`);\n      }\n      return;\n    }\n    const result = await this.exec(\n      `if [ ! -e ${shellQuote(srcAbs)} ]; then exit ${EXIT_NOT_FOUND}; fi; mkdir -p ${shellQuote(posixPath.dirname(destAbs))} && cp ${recursive}${shellQuote(srcAbs)} ${shellQuote(destAbs)}`,\n    );\n    if (result.exitCode === EXIT_NOT_FOUND) throw new FileNotFoundError(src);\n    if (result.exitCode !== 0) {\n      throw new Error(`copyFile ${src} -> ${dest} failed (exit ${result.exitCode}): ${result.stderr.trim()}`);\n    }\n  }\n\n  async moveFile(src: string, dest: string, options?: CopyOptions): Promise<void> {\n    const srcAbs = await this.resolveAsync(src);\n    const destAbs = await this.resolveAsync(dest);\n    await this.assertContainedRealpath(srcAbs, src);\n    await this.assertContainedDest(destAbs, dest);\n    if (options?.overwrite === false) {","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/agents/sandbox-filesystem.ts#L331-L367","documentation":"This generic Error comes from SandboxFilesystem.copyFile in its atomic/structured branch: a multi-line shell probe ran and exited with a code that is neither EXIT_NOT_FOUND nor EXIT_EXISTS. The library reserves those two codes for FileNotFoundError and FileExistsError; anything else (permission denied, cross-device copy failure, cp syntax/behavior issues, sandbox instability) is rethrown with the raw exit code and stderr. The probe script typically checks existence/overlap before invoking cp, so this indicates the cp step or an intermediate check failed unexpectedly.","triggerScenarios":"Calling sandbox.files.copyFile(src, dest, options) where the probe-and-copy shell block fails: dest parent cannot be created, cp lacks read permission on src or write permission on dest dir, src and dest resolve oddly (device/overlay boundaries), or the shell itself errors mid-script.","commonSituations":"Copying into a read-only or full sandbox mount; copying a file the current sandbox user cannot read; a malformed or relative dest that resolves outside an allowed root; sandbox snapshot/restore leaving stale permissions.","solutions":["Inspect the stderr embedded in the message for the exact shell failure.","Verify src is readable and dest's parent directory is writable by the sandbox user.","Ensure dest is not a directory path that would cause cp semantics conflicts; pick an explicit file name for dest.","Retry with simpler options (no noOverwrite/atomic flag) to isolate whether the probe block or plain cp path is failing.","Recreate or restart the sandbox if filesystem state appears corrupted."],"exampleFix":"// before — dest is an existing directory, cp behaves unexpectedly\nawait sandbox.fs.copyFile('/app/report.pdf', '/out');\n\n// after — explicit destination file path\nawait sandbox.fs.copyFile('/app/report.pdf', '/out/report.pdf');","handlingStrategy":"try-catch","validationCode":"// verify src readable and dest parent usable before copying\nawait sandbox.fs.stat(src);\nconst destDir = destPath.dirname(dest);\nif (!(await sandbox.fs.isDirectory(destDir))) {\n  await sandbox.fs.mkdir(destDir, { recursive: true });\n}","typeGuard":"function isFileExistsError(e: unknown): e is FileExistsError {\n  return e instanceof FileExistsError;\n}","tryCatchPattern":"try {\n  await sandbox.fs.copyFile(src, dest, { noOverwrite: true });\n} catch (e) {\n  if (isFileExistsError(e)) return; // dest already present\n  if (e instanceof FileNotFoundError) throw new Error(`src missing: ${src}`);\n  throw e; // unexpected shell failure: surface stderr from message\n}","preventionTips":["Use explicit file paths for dest, never bare directory paths.","Pre-create the dest parent directory with mkdir recursive.","Catch the typed FileNotFoundError/FileExistsError separately from the generic failure.","Check sandbox disk space and mount permissions before bulk copies."],"tags":["sandbox","filesystem","shell-command","copy"],"backgroundTag":"sandbox-command-failed","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}