plandex-ai/plandex · error

failed to check if %s exists: %s

Error message

failed to check if %s exists: %s

What it means

Inside ApplyFiles, each goroutine stats the destination file to determine existence and mode before writing. If os.Stat fails with an error other than NotExist (e.g. permission denied on a parent directory), the goroutine sends this error on errCh, aborting the whole apply.

Source

Thrown at app/cli/lib/apply.go:651

			errCh <- nil
			continue
		}
		go func(path, content string) {
			// Compute destination path
			dstPath := filepath.Join(fs.ProjectRoot, path)
			content = strings.ReplaceAll(content, "\\`\\`\\`", "```")
			// Check if the file exists
			var exists bool
			var mode os.FileMode
			info, err := os.Stat(dstPath)
			if err == nil {
				exists = true
				mode = info.Mode()
			} else {
				if os.IsNotExist(err) {
					exists = false
				} else {
					errCh <- fmt.Errorf("failed to check if %s exists: %s", dstPath, err.Error())
					return
				}
			}

			if exists {
				// read file content
				bytes, err := os.ReadFile(dstPath)
				if err != nil {
					errCh <- fmt.Errorf("failed to read %s: %s", dstPath, err.Error())
					return
				}
				// Check if the file has changed
				if string(bytes) == content {
					// log.Println("File is unchanged, skipping")
					errCh <- nil
					return
				} else {
					mu.Lock()

View on GitHub (pinned to e2d772072e)

Solutions

  1. Check permissions on the target directory (ls -la) and fix with chmod/chown
  2. Verify no file exists where a directory is expected in the path (e.g. 'pkg' is a file)
  3. Run the CLI with sufficient privileges, or from the correct project root
  4. Check disk/filesystem health if I/O errors appear

Example fix

// before
$ ls -la pkg   # pkg is a regular file, but plan writes pkg/x.go
// after
$ rm pkg && mkdir pkg
$ plandex apply
Defensive patterns

Strategy: validation

Validate before calling

// before apply: ensure each path component allows stat
for _, path := range paths {
  if _, err := os.Stat(filepath.Join(root, path)); err != nil && !os.IsNotExist(err) {
    return fmt.Errorf("cannot access %s: %w", path, err)
  }
}

Type guard

func statAccessible(path string) bool {
  _, err := os.Stat(path)
  return err == nil || os.IsNotExist(err) // NotExist is fine; anything else is a problem
}

Try / catch

if _, err := os.Stat(dstPath); err != nil && !os.IsNotExist(err) {
  return fmt.Errorf("stat %s: %w", dstPath, err)
}

Prevention

When it happens

Trigger: os.Stat(dstPath) returns an error that is not os.IsNotExist — permission denied on a parent directory, I/O error, or a component of dstPath is not a directory.

Common situations: Project subdirectory owned by another user or read-only; dstPath collides with a regular file (e.g. 'pkg' exists as a file but path is 'pkg/x.go'); filesystem/permission issues after running as different users.

Related errors


AI-assisted analysis of plandex-ai/plandex@e2d772072e (2026-09-05). Data as JSON: /api/errors/6c41ee83148a62db. Report an issue: GitHub.