{"record":{"id":"048365114a467894","repo":"nushell/nushell","slug":"invalid-xml-document","errorCode":null,"errorMessage":"invalid xml document","messagePattern":"invalid xml document","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"crates/nu_plugin_query/src/query_xml.rs","lineNumber":139,"sourceCode":"            return Err(\n                LabeledError::new(\"problem with input data\").with_label(\"query missing\", call.head)\n            );\n        }\n    };\n\n    let node_output_options = NodeOutputOptions::from_call(call);\n\n    let xpath = build_xpath(query_string, span)?;\n    let input_string = input.coerce_str()?;\n    let package = parser::parse(&input_string);\n\n    if let Err(err) = package {\n        return Err(\n            LabeledError::new(\"Invalid XML document\").with_label(err.to_string(), input.span())\n        );\n    }\n\n    let package = package.expect(\"invalid xml document\");\n\n    let document = package.as_document();\n    let mut context = Context::new();\n\n    let mut namespaces = namespaces.unwrap_or_default();\n\n    if namespaces.get(\"xml\").is_none() {\n        // XML namespace is always present, so we add it explicitly\n        // it's used in attributes like `xml:lang`, `xml:base`, etc.\n        namespaces.insert(\n            \"xml\",\n            Value::string(\"http://www.w3.org/XML/1998/namespace\", call.head),\n        );\n    }\n\n    // NB: `xmlns:whatever=` or `xmlns=` may look like an attribute, but XPath doesn't treat it as such.\n    // Those are namespaces, and they are available through a separate axis (`namespace::`)\n    // Thus we don't need to register a namespace for `xmlns` prefix","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/nushell/nushell/blob/8e03210652f3c48c4521cec982d96e4cb6c67181/crates/nu_plugin_query/src/query_xml.rs#L121-L157","documentation":"The 'invalid xml document' expect in query xml: the code first checks `if let Err(err) = package` and returns a labeled 'Invalid XML document' error for any parse failure, then calls package.expect(\"invalid xml document\") on the value that just passed that check. The expect is therefore dead/unreachable — by the time it runs, the Result is guaranteed Ok. Users never see this panic; malformed XML input produces the proper labeled error with the parser message instead.","triggerScenarios":"None for the panic itself: any malformed XML input (unbalanced tags, bad encoding, wrong root structure) exits at the earlier `if let Err` block with LabeledError 'Invalid XML document'. The expect could only fire if the early-return check were removed or reordered.","commonSituations":"Running 'query xml' with a --query XPath against non-XML input — you get the graceful labeled error, not this panic. Relevant only to maintainers refactoring query_xml.rs who might break the check-then-expect ordering.","solutions":["No fix needed for users; malformed XML already yields a labeled 'Invalid XML document' error","Maintainer cleanup: bind the success value in the check (`let package = parser::parse(&input_string).map_err(...)?.as_document()` style) to remove the unreachable expect","Keep the error return before the unwrap when refactoring"],"exampleFix":"// before\nlet package = parser::parse(&input_string);\nif let Err(err) = package {\n    return Err(LabeledError::new(\"Invalid XML document\").with_label(err.to_string(), input.span()));\n}\nlet package = package.expect(\"invalid xml document\");\n\n// after: no unreachable expect\nlet package = parser::parse(&input_string).map_err(|err| {\n    LabeledError::new(\"Invalid XML document\").with_label(err.to_string(), input.span())\n})?;","handlingStrategy":"try-catch","validationCode":"// users cannot reach this panic; to fail gracefully on bad input the command already:\n// 1) parses the XML, 2) returns LabeledError \"Invalid XML document\" on failure.\n// You can pre-validate before invoking:\nlet looks_like_xml = input_str.trim_start().starts_with('<');","typeGuard":"fn is_parseable_xml(s: &str) -> bool {\n    roxmltree::Document::parse(s).is_ok() // independent pre-check\n}","tryCatchPattern":"// in plugin command run(): the error is already a labeled error, so just bubble it up\nmatch execute_xpath(...) {\n    Ok(v) => Ok(v),\n    Err(e) => Err(e), // LabeledError carries \"Invalid XML document\" + parser detail\n}","preventionTips":["Feed well-formed XML to 'query xml' (balanced tags, single root, correct declaration)","Strip leading whitespace/BOM before parsing pasted XML","For maintainers: bind the Ok value via map_err + ? so no expect remains"],"tags":["nushell","query-xml","rust","expect","xml","dead-code"],"backgroundTag":"invalid-xml-parse","analyzedSha":"8e03210652f3c48c4521cec982d96e4cb6c67181","analyzedAt":"2026-08-17T16:24:07.527Z","contentChangedAt":"2026-08-17T16:24:07.527Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}