{"record":{"id":"f40dfd54d4b7362a","repo":"sinelaw/fresh","slug":"empty-script-pass-a-file-or-pipe-the-source-on-stdin","errorCode":null,"errorMessage":"empty script: pass a file, or pipe the source on stdin","messagePattern":"empty script: pass a file, or pipe the source on stdin","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/fresh-editor/src/main.rs","lineNumber":4247,"sourceCode":"        .join(\"types\");\n    println!(\"{}\", dir.join(\"fresh.d.ts\").display());\n    println!(\"{}\", dir.join(\"plugins.d.ts\").display());\n    Ok(())\n}\n\n/// `fresh --cmd script run [FILE|-]` — evaluate a script in the editor.\n///\n/// The source comes from a file or stdin rather than argv: a script is\n/// multi-line and full of quotes, and threading that through a shell's argument\n/// vector mangles it. `fresh --cmd script run < s.ts`, a heredoc, or an explicit\n/// path all work.\n///\n/// Whatever the script returns is printed as JSON; a throw becomes a non-zero\n/// exit with the message on stderr.\nfn script_run(session: Option<&str>, from: &[&str]) -> AnyhowResult<()> {\n    let source = read_script_source(from)?;\n    if source.trim().is_empty() {\n        anyhow::bail!(\"empty script: pass a file, or pipe the source on stdin\");\n    }\n    submit_script(session, source, false)\n}\n\n/// Send `source` to a live editor's script channel and report the outcome:\n/// whatever the script returned on stdout, a throw on stderr with exit 1.\n///\n/// Every verb below is a thin wrapper over this. The script channel is\n/// already the authorized, window-scoped, capability-checked way into a\n/// running editor, so a new verb needs a new *script*, not a new socket\n/// message — and the verb inherits the token check for free.\n///\n/// `unwrap_string` decodes a JSON string result before printing it. `script\n/// run` leaves its output verbatim (a script's return value is data, and the\n/// caller asked for JSON); the convenience verbs return prose meant to be\n/// read, where the surrounding quotes and `\\n` escapes would be noise.\nfn submit_script(session: Option<&str>, source: String, unwrap_string: bool) -> AnyhowResult<()> {\n    use fresh::server::protocol::ClientControl;","sourceCodeStart":4229,"sourceCodeEnd":4265,"githubUrl":"https://github.com/sinelaw/fresh/blob/67894ca5463dbd7a89bb31add4627c27d6b79d83/crates/fresh-editor/src/main.rs#L4229-L4265","documentation":"script_run reads the script source from files/stdin via read_script_source and bails when the resulting source is empty or whitespace-only. An empty script has nothing to evaluate, so it is rejected before submission to the editor.","triggerScenarios":"`fresh --script-file` pointing at an empty (or whitespace/comment-only) file; piping empty stdin, e.g. running `fresh --script-file -` in a terminal with no piped input; an empty here-doc (`<<EOF\\nEOF`).","commonSituations":"Forgot to redirect a file into stdin (`fresh --script-file -` without `< script.js`); script file created but never saved; a build step that produced a zero-byte script; variable expansion that emptied the source.","solutions":["Pipe the script on stdin: `fresh --script-file - < myscript.js`, or use a tty-interactive editor.","Pass a non-empty file path with actual JS source in it.","Verify the file size / content before invoking (`wc -c script.js`).","Fix the upstream step that was supposed to generate the script."],"exampleFix":"# before\nfresh --script-file -          # empty stdin\n# after\nfresh --script-file - < script.js","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nconst src = process.argv[3] === '-' ? fs.readFileSync(0, 'utf8') : fs.readFileSync(process.argv[3], 'utf8');\nif (!src.trim()) { console.error('script source is empty: pass a file with content or pipe stdin'); process.exit(1); }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always redirect input when using stdin mode: `fresh --script-file - < script.js`.","Check `wc -c script.js` before invoking on generated scripts.","Don't rely on an interactive tty for stdin in scripts.","Verify the generator step actually wrote the script file."],"tags":["cli","scripting","empty-input","validation"],"backgroundTag":"empty-required-field","analyzedSha":"67894ca5463dbd7a89bb31add4627c27d6b79d83","analyzedAt":"2026-09-13T15:04:03.701Z","contentChangedAt":"2026-09-13T15:04:03.701Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}