{"record":{"id":"a0155814d16289bd","repo":"fish-shell/fish-shell","slug":"s","errorCode":null,"errorMessage":"%s","messagePattern":"%s","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/parse_execution.rs","lineNumber":1319,"sourceCode":"            return result;\n        }\n\n        trace_if_enabled_with_args(ctx.parser(), L!(\"function\"), &arguments);\n        let mut outs = OutputStream::Null;\n        let mut errs = OutputStream::String(StringOutputStream::new());\n        let io_chain = IoChain::new();\n        let mut streams = IoStreams::new(&mut outs, &mut errs, &io_chain);\n        let mut shim_arguments: Vec<&wstr> = arguments\n            .iter()\n            .map(|s| truncate_at_nul(s.as_ref()))\n            .collect();\n        let err_code = builtins::function::function(\n            ctx.parser(),\n            &mut streams,\n            &mut shim_arguments,\n            NodeRef::new(Arc::clone(self.pstree()), statement),\n        )\n        .err()\n        .unwrap_or(STATUS_CMD_OK);\n\n        ctx.parser().libdata_mut().status_count += 1;\n        ctx.parser().set_last_statuses(Statuses::just(err_code));\n\n        let errtext = errs.contents();\n        if !errtext.is_empty() {\n            report_error!(self, ctx, err_code, header, \"%s\", errtext);\n        }\n        result\n    }\n\n    fn run_begin_statement(\n        &mut self,\n        ctx: &mut OperationContext<'_>,\n        contents: &ast::JobList,\n    ) -> EndExecutionReason {\n        // Basic begin/end block. Push a scope block, run jobs, pop it","sourceCodeStart":1301,"sourceCodeEnd":1337,"githubUrl":"https://github.com/fish-shell/fish-shell/blob/edb719d76c8f76633f09dbbccedbda1823a7d9d5/src/parse_execution.rs#L1301-L1337","documentation":"When a `function` definition statement fails, run_function_statement runs the function builtin, captures its exit status and any text written to the error stream, and re-raises the builtin's message through report_error! using the format string '%s' with the captured error text. The '%s' is the error's format template, not the actual message; the real diagnostic (e.g. 'function: name should be one word') is in errtext.","triggerScenarios":"Executing a `function NAME ...; end` statement where the function builtin returns an error status: invalid function name (not a valid identifier / contains slashes), duplicate reserved names, missing name, or arguments failing expansion.","commonSituations":"Sourcing an old script whose function name collides with fish's reserved/invalid names, `function` with a variable-expanded name that expands to an empty string or multiple words, or copy-pasted zsh/bash function syntax invalid in fish.","solutions":["Read the accompanying diagnostic line printed with this error for the actual cause","Ensure the function name is a single valid word (letters/digits/underscore, not starting with a dash, no path separators)","If the name comes from a variable, quote and validate it before `function $name`, e.g. `function $name; ...` only when `string match -qr '^[A-Za-z_][A-Za-z0-9_]*$' -- $name`","Check that all arguments on the function header line expand correctly and don't contain wildcards that match nothing"],"exampleFix":"// before\nfunction $fname; echo hi; end  # fname empty -> error\n// after\nif set -q fname[1]; and string match -qr '^[A-Za-z_][A-Za-z0-9_]*$' -- $fname\n    function $fname; echo hi; end\nend","handlingStrategy":"try-catch","validationCode":"if not string match -qr '^[A-Za-z_][A-Za-z0-9_]*$' -- $fname\n    echo \"invalid function name: $fname\" >&2\n    exit 1\nend","typeGuard":"function is_valid_function_name\n    string match -qr '^[A-Za-z_][A-Za-z0-9_]*$' -- $argv[1]\nend","tryCatchPattern":"// fish has no try/catch for parse errors; capture and inspect status\nfunction define_fn --argument-names name\n    eval \"function $name; end\" 2>/tmp/fnerr\n    or begin\n        cat /tmp/fnerr >&2\n        return 1\n    end\nend","preventionTips":["Validate expanded function names before `function` statements","Avoid variable names in function headers sourced from user input","Don't name functions after reserved words or paths","Lint sourced scripts with `fish --no-execute` to catch definitions early"],"tags":["shell","function-definition","parse-error"],"backgroundTag":"invalid-function-definition","analyzedSha":"edb719d76c8f76633f09dbbccedbda1823a7d9d5","analyzedAt":"2026-09-03T01:23:54.744Z","contentChangedAt":"2026-09-03T01:23:54.744Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}