plandex-ai/plandex · error

failed to stat stdin: %v

Error message

failed to stat stdin: %v

What it means

MustLoadContext calls os.Stdin.Stat() to detect whether data is piped into the CLI (named pipe check); this error means the stat syscall on stdin failed. It exits via onErr with 'Failed to load context'. Without knowing stdin's mode the command cannot tell if piped data should be captured as context.

Source

Thrown at app/cli/lib/context_load.go:49

		}

		term.LongSpinnerWithWarning("🗺️  Building project map...", "🗺️  This can take a while in larger projects...")
	} else if params.NamesOnly {
		term.LongSpinnerWithWarning("🌳 Loading directory tree...", "🌳 This can take a while in larger projects...")
	} else {
		term.StartSpinner("📥 Loading context...")
	}

	onErr := func(err error) {
		term.StopSpinner()
		term.OutputErrorAndExit("Failed to load context: %v", err)
	}

	var loadContextReq shared.LoadContextRequest

	fileInfo, err := os.Stdin.Stat()
	if err != nil {
		onErr(fmt.Errorf("failed to stat stdin: %v", err))
	}

	var authVars map[string]string
	var openAIBase string

	if params.Note != "" || fileInfo.Mode()&os.ModeNamedPipe != 0 {
		authVars = MustVerifyAuthVarsSilent(auth.Current.IntegratedModelsMode)
	}

	if params.Note != "" {
		loadContextReq = append(loadContextReq, &shared.LoadContextParams{
			ContextType: shared.ContextNoteType,
			Body:        params.Note,
			ApiKeys:     authVars,
			OpenAIBase:  openAIBase,
			OpenAIOrgId: os.Getenv("OPENAI_ORG_ID"),
			SessionId:   params.SessionId,
			AutoLoaded:  params.AutoLoaded,

View on GitHub (pinned to e2d772072e)

Solutions

  1. Ensure stdin is attached when running the command (run interactively or redirect from a source)
  2. If scripting, redirect input explicitly: `plandex ... < /dev/null`
  3. Check the sandbox/container for fd restrictions (ulimit, security profile)
  4. Upgrade or fix the launcher/daemon that invokes the CLI if it closes fds

Example fix

// before
fileInfo, err := os.Stdin.Stat()
if err != nil {
	onErr(fmt.Errorf("failed to stat stdin: %v", err))
}
// after
fileInfo, err := os.Stdin.Stat()
if err != nil {
	if errors.Is(err, os.ErrInvalid) { // stdin closed — treat as no piped data
		fileInfo = &os.FileInfo(nil)
		// proceed without piped input instead of exiting
	} else {
		onErr(fmt.Errorf("failed to stat stdin: %w", err))
	}
}
Defensive patterns

Strategy: fallback

Validate before calling

// check stdin is usable before the load command
if fi, err := os.Stdin.Stat(); err != nil {
	log.Printf("stdin unavailable (%v); proceeding without piped input", err)
}

Type guard

// Go: classify stdin mode safely
func stdinIsPipe() bool {
	fi, err := os.Stdin.Stat()
	return err == nil && fi.Mode()&os.ModeNamedPipe != 0
}

Try / catch

// Go: treat unstatable stdin as no-piped-input rather than fatal
fileInfo, err := os.Stdin.Stat()
hasPipe := err == nil && fileInfo.Mode()&os.ModeNamedPipe != 0
if err != nil { log.Printf("stdin stat failed, assuming no piped data: %v", err) }

Prevention

When it happens

Trigger: os.Stdin.Stat() returns an error — stdin fd is closed, invalid, or the OS denies stat (e.g. detached process, restricted sandbox/container without a stdin fd).

Common situations: Running the CLI with stdin explicitly closed (`plandex ... 0<&-`); daemonized/scheduled invocation with no stdin attached; restricted container runtimes that don't provide standard fds.

Related errors


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