{"record":{"id":"3f87ff3f21532db4","repo":"d2lang/d2","slug":"error-reading-from-stdin-w","errorCode":null,"errorMessage":"error reading from stdin: %w","messagePattern":"error reading from stdin: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"d2cli/play.go","lineNumber":56,"sourceCode":"\tif err != nil {\n\t\treturn err\n\t}\n\n\tencoded, err := urlenc.Encode(fileRaw)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\turl := fmt.Sprintf(\"https://play.d2lang.com/?script=%s&sketch=%d&theme=%d&\", encoded, sketchNumber, theme)\n\topenBrowser(ctx, ms, url)\n\treturn nil\n}\n\nfunc readInput(filepath string) (string, error) {\n\tif filepath == \"-\" {\n\t\tdata, err := io.ReadAll(os.Stdin)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"error reading from stdin: %w\", err)\n\t\t}\n\t\treturn string(data), nil\n\t}\n\n\tdata, err := os.ReadFile(filepath)\n\tif err != nil {\n\t\treturn \"\", xmain.UsageErrorf(\"%s\", err.Error())\n\t}\n\treturn string(data), nil\n}\n\nfunc openBrowser(ctx context.Context, ms *xmain.State, url string) {\n\tms.Log.Info.Printf(\"opening playground: %s\", url)\n\n\terr := xbrowser.Open(ctx, ms.Env, url)\n\tif err != nil {\n\t\tms.Log.Warn.Printf(\"failed to open browser to %v: %v\", url, err)\n\t}","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/d2cli/play.go#L38-L74","documentation":"readInput reads the sketch/pad input; when the path is \"-\" it reads from stdin via io.ReadAll(os.Stdin). If the stdin read fails, the error is wrapped as \"error reading from stdin\".","triggerScenarios":"Running the play command with \"-\" as the input path (e.g. `d2 play -`) while stdin is closed, not piped, or the pipe writer errored/closed mid-stream.","commonSituations":"Running `d2 play -` in an interactive terminal without piping input,CI jobs where stdin is /dev/null or closed, or upstream command in a pipe failing and closing the pipe early.","solutions":["Pipe content into the command: `cat sketch.d2 | d2 play -`","Or pass a real file path instead of \"-\"","Check that the upstream command in the pipeline succeeds"],"exampleFix":"// before\nd2 play -          # no stdin provided\n// after\ncat sketch.d2 | d2 play -","handlingStrategy":"try-catch","validationCode":"stat, _ := os.Stdin.Stat()\nif (stat.Mode() & os.ModeCharDevice) != 0 {\n\t// stdin is a terminal, not a pipe — provide a file or pipe input\n}","typeGuard":null,"tryCatchPattern":"input, err := readInput(path)\nif err != nil {\n\treturn fmt.Errorf(\"pipe input: cat sketch.d2 | d2 play - (%v)\", err)\n}","preventionTips":["Always pipe data when using \"-\" as input path","Prefer a real file path in CI","Ensure upstream pipeline commands succeed"],"tags":["cli","stdin","io"],"backgroundTag":"stdin-read-failed","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}