{"record":{"id":"8cfbb2d8a0a7fdd4","repo":"can1357/oh-my-pi","slug":"file-not-found-path","errorCode":null,"errorMessage":"File not found: ${path}","messagePattern":"File not found: (.+?)","errorType":"validation","errorClass":"ApplyPatchError","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/edit/modes/patch.ts","lineNumber":1044,"sourceCode":"\t// Apply the replacement\n\tconst before = normalizedContent.substring(0, matchOutcome.match.startIndex);\n\tconst after = normalizedContent.substring(matchOutcome.match.startIndex + matchOutcome.match.actualText.length);\n\treturn { content: before + adjustedNewText + after, warnings };\n}\n\nfunction applyTrailingNewlinePolicy(content: string, hadFinalNewline: boolean): string {\n\tif (hadFinalNewline) {\n\t\treturn content.endsWith(\"\\n\") ? content : `${content}\\n`;\n\t}\n\treturn content.replace(/\\n+$/u, \"\");\n}\n\nasync function readExistingPatchFile(fileSystem: FileSystem, absolutePath: string, path: string): Promise<string> {\n\ttry {\n\t\treturn await fileSystem.read(absolutePath);\n\t} catch (error) {\n\t\tif (isEnoent(error) || (error instanceof Error && error.message.startsWith(\"File not found:\"))) {\n\t\t\tthrow new ApplyPatchError(`File not found: ${path}`);\n\t\t}\n\t\tthrow error;\n\t}\n}\n\n/**\n * A prefix/substring strategy matched pattern lines that cover only part of\n * the corresponding file lines; replacing whole lines would silently drop the\n * uncovered text the model never saw. Allow the replacement only when every\n * discarded piece (normalized) survives somewhere in the hunk's new lines.\n */\nfunction assertPartialMatchPreservesDiscardedText(\n\tpath: string,\n\tpattern: string[],\n\tmatchedLines: string[],\n\tnewLines: string[],\n\tmatchStartIndex: number,\n): void {","sourceCodeStart":1026,"sourceCodeEnd":1062,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/edit/modes/patch.ts#L1026-L1062","documentation":"The patch machinery tried to read the existing file that the patch targets (readExistingPatchFile) and got an ENOENT (or a wrapped 'File not found') from the FileSystem layer. It rethrows as an ApplyPatchError so callers can treat it as a patch-argument problem rather than an unexpected crash. This means the diff cannot be applied because the target file does not exist on disk at the resolved absolute path.","triggerScenarios":"Calling the apply-patch/edit tool with a patch whose *** Update File header names a file that is not present in the workspace, or whose relative path resolves against the wrong working directory; also when the FileSystem.read implementation wraps ENOENT in a generic Error with a message starting 'File not found:'.","commonSituations":"LLM-generated patches that hallucinate a file path or use a path from a different repo layout; the file was deleted or renamed before the patch ran; the patch references an absolute path from another machine; a patch meant to *** Add File was emitted as *** Update File.","solutions":["Verify the file exists at the given path (ls / read the file) and correct the path in the patch header.","If the file should be new, change the patch header to *** Add File (or *** Delete File semantics as appropriate) instead of Update.","Re-read the file to get its current path, then regenerate the patch.","Check the working directory root the patch is applied against; relative paths resolve from there."],"exampleFix":"// before\n*** Begin Patch\n*** Update File: src/servcie.ts\n@@\n-old\n+new\n*** End Patch\n\n// after\n*** Begin Patch\n*** Update File: src/service.ts\n@@\n-old\n+new\n*** End Patch","handlingStrategy":"validation","validationCode":"import * as fs from 'node:fs/promises';\nconst target = resolve(workspaceRoot, patchPath);\ntry {\n  await fs.access(target);\n} catch {\n  throw new Error(`Patch target does not exist: ${patchPath}. Use Add File for new files.`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  applyPatch(patch);\n} catch (err) {\n  if (err instanceof ApplyPatchError && err.message.startsWith('File not found:')) {\n    // prompt user to verify path or switch to Add File\n  } else throw err;\n}","preventionTips":["Always read/list the target file immediately before generating a patch.","Use paths relative to the workspace root actually passed to the tool.","Use *** Add File for files that do not exist yet.","Check for typos in the Update File header (e.g. swapped letters)."],"tags":["filesystem","patch","enoent","path"],"backgroundTag":"patch-target-file-not-found","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}