{"record":{"id":"3fbce8ad65611280","repo":"hashicorp/terraform","slug":"error-reading-script-s","errorCode":null,"errorMessage":"Error reading script: %s","messagePattern":"Error reading script: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/communicator/ssh/communicator.go","lineNumber":451,"sourceCode":"\t}\n\n\tscpFunc := func(w io.Writer, stdoutR *bufio.Reader) error {\n\t\treturn scpUploadFile(targetFile, input, w, stdoutR, size)\n\t}\n\n\tcmd, err := quoteScpCommand([]string{\"scp\", \"-vt\", targetDir}, c.connInfo.TargetPlatform)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn c.scpSession(cmd, scpFunc)\n}\n\n// UploadScript implementation of communicator.Communicator interface\nfunc (c *Communicator) UploadScript(path string, input io.Reader) error {\n\treader := bufio.NewReader(input)\n\tprefix, err := reader.Peek(2)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"Error reading script: %s\", err)\n\t}\n\tvar script bytes.Buffer\n\n\tif string(prefix) != \"#!\" && c.connInfo.TargetPlatform != TargetPlatformWindows {\n\t\tscript.WriteString(DefaultShebang)\n\t}\n\tscript.ReadFrom(reader)\n\n\tif err := c.Upload(path, &script); err != nil {\n\t\treturn err\n\t}\n\tif c.connInfo.TargetPlatform != TargetPlatformWindows {\n\t\tvar stdout, stderr bytes.Buffer\n\t\tcmd := &remote.Cmd{\n\t\t\tCommand: fmt.Sprintf(\"chmod 0777 %s\", path),\n\t\t\tStdout:  &stdout,\n\t\t\tStderr:  &stderr,\n\t\t}","sourceCodeStart":433,"sourceCodeEnd":469,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/communicator/ssh/communicator.go#L433-L469","documentation":"Raised in UploadScript when bufio.Reader.Peek(2) fails to read the first two bytes of the script content. Peek fails when the underlying reader returns an error before any data (e.g. an empty/closed reader, a pipe whose writer end closed, or an upstream provisioner producing an I/O error). The shebang check needs those bytes to decide whether to prepend DefaultShebang.","triggerScenarios":"An inline script passed via a broken variable reference, a file() call on a missing path, a template that evaluated to nothing, or a producer that errored/closed before yielding data. Most commonly the script source is malformed or empty beyond what Peek tolerates.","commonSituations":"file(\"${path.module}/script.sh\") pointing at a non-existent file; templatefile producing an I/O error; a custom data source returning an error value used directly as the script; an empty inline script set with an expression that resolved to no bytes.","solutions":["Verify the script source is readable and non-empty before the provisioner runs (cat the file locally, or print the variable).","For file-based scripts, confirm the path resolves under the module (use path.module / path.root).","confirm `templatefile()` inputs exist and the template itself is valid.","If using a data source for the script, ensure it returns content without error."],"exampleFix":"// before\nprovisioner \"remote-exec\" {\n  inline = file(\"${path.module}/scripts/setup.sh\") // file missing\n}\n\n// after\nprovisioner \"remote-exec\" {\n  inline = file(\"${path.module}/files/setup.sh\") // correct path\n}","handlingStrategy":"validation","validationCode":"# Confirm the script source is readable and non-empty before apply:\n#   test -s scripts/setup.sh && echo ok || echo 'missing or empty'\n# In HCL, guard file() with a check variable if needed:\n#   variable \"script_path\" { type = string; validation {...} }","typeGuard":null,"tryCatchPattern":"// In Go feeding the script reader, peek ahead and fail with a clearer msg:\nhead := make([]byte, 2)\nn, err := io.ReadFull(reader, head)\nif err != nil && n == 0 {\n    return fmt.Errorf(\"script source produced no data; check path/template: %w\", err)\n}","preventionTips":["Reference scripts via path.module to keep them portable.","Validate file existence with terraform console: fileexists(path.module.'/x.sh').","Avoid piping from data sources that can return errors; check first."],"tags":["terraform","ssh","upload","script","io","provisioner","remote-exec"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}