diff --git a/crates/nu-command/src/experimental/view_source.rs b/crates/nu-command/src/experimental/view_source.rs index 8958cb2f55..2226ddf7e4 100644 --- a/crates/nu-command/src/experimental/view_source.rs +++ b/crates/nu-command/src/experimental/view_source.rs @@ -35,12 +35,16 @@ impl Command for ViewSource { let arg_span = arg.span()?; match arg { - Value::Block { span, .. } => { - let contents = engine_state.get_span_contents(&span); - Ok( - Value::string(String::from_utf8_lossy(contents), call.head) - .into_pipeline_data(), - ) + Value::Block { val: block_id, .. } => { + let block = engine_state.get_block(block_id); + + if let Some(span) = block.span { + let contents = engine_state.get_span_contents(&span); + Ok(Value::string(String::from_utf8_lossy(contents), call.head) + .into_pipeline_data()) + } else { + Ok(Value::string("", call.head).into_pipeline_data()) + } } Value::String { val, .. } => { if let Some(decl_id) = engine_state.find_decl(val.as_bytes()) {