{"record":{"id":"6cbab969a3b6a78c","repo":"hashicorp/nomad","slug":"stdin-already-consumed","errorCode":null,"errorMessage":"stdin already consumed","messagePattern":"stdin already consumed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/var.go","lineNumber":213,"sourceCode":"\t}\n\n\t// Empty strings are fine, just ignored\n\tif raw == \"\" {\n\t\treturn nil\n\t}\n\n\t// Split into key/value\n\tparts := strings.SplitN(raw, \"=\", 2)\n\n\t// If the arg is exactly \"-\", then we need to read from stdin\n\t// and merge the results into the resulting structure.\n\tif len(parts) == 1 {\n\t\tif raw == \"-\" {\n\t\t\tif b.Stdin == nil {\n\t\t\t\treturn fmt.Errorf(\"stdin is not supported\")\n\t\t\t}\n\t\t\tif b.stdin {\n\t\t\t\treturn fmt.Errorf(\"stdin already consumed\")\n\t\t\t}\n\n\t\t\tb.stdin = true\n\t\t\treturn b.addReader(b.Stdin)\n\t\t}\n\n\t\t// If the arg begins with \"@\" then we need to read a file directly\n\t\tif raw[0] == '@' {\n\t\t\tf, err := os.Open(raw[1:])\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tdefer f.Close()\n\n\t\t\treturn b.addReader(f)\n\t\t}\n\t}\n","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/var.go#L195-L231","documentation":"KVBuilder allows reading from stdin at most once per invocation. Once '-' has been consumed (the unexported stdin flag is set), a second '-' argument triggers this error. It prevents ambiguous double-reads of a non-seekable stream.","triggerScenarios":"Passing '-' twice to KVBuilder.Add, e.g. Add(\"-\", \"-\") or two '-var -' flags in one command, after the first '-' successfully consumed Stdin.","commonSituations":"Shell scripts piping one JSON blob but specifying '-var -' multiple times; loops that append '-var -' per variable when the intent was 'key=-' per pair.","solutions":["Pass '-' only once per command invocation; combine remaining vars as explicit key=value pairs","Merge all JSON input into a single stdin payload read by the one '-'","If multiple stdin reads are genuinely needed, construct a new KVBuilder with a fresh Stdin reader"],"exampleFix":"// before\nb.Add(\"-\", \"-\")\n// after\nb.Add(\"-\")\nb.Add(\"otherkey=othervalue\")","handlingStrategy":"validation","validationCode":"if n := strings.Count(strings.Join(args, \" \"), \"\\\"-\\\"\") + countExactDashes(args); n > 1 {\n\t// reject: stdin can only be consumed once\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use '-' at most once per KVBuilder invocation","Supply additional variables as key=value or @file references","Review generated CLI args in loops for repeated '-var -'"],"tags":["cli","stdin","argument-parsing"],"backgroundTag":"stdin-already-consumed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}