Skip to content

Span issue of $in and $it inside a block #4460

Closed

Description

@kubouch

Describe the bug

When creating an error using the spans of $in or $it inside a block, the arrow points at the whole block. Below is a collection of examples that show the wrong spans:

How to reproduce

Example 1

> def shl [] {
   let inp = $in
   if true {
     let span = (metadata $inp).span  # Defining the span here gives wrong output
     error make {
       msg: "Error",
       label: {
         text: "error",
         start: $span.start,
         end: $span.end
       }
     }
   }
 }

> 2 | shl
Error:
  × Error
    ╭─[entry #89:2:1]
  2 │       let inp = $in
  3 │ ╭─▶   if $true {
  4 │ │       let span = (metadata $inp).span 
  5 │ │       error make {
  6 │ │         msg: "Error",
  7 │ │         label: {
  8 │ │           text: "error",
  9 │ │           start: $span.start,
 10 │ │           end: $span.end
 11 │ │       }
 12 │ │     }
 13 │ ├─▶ }
    · ╰──── error
 14 │     }
    ╰────

> def shl [] {
   let inp = $in
   let span = (metadata $inp).span  # Defining the span here results in correct span
   if true {
     error make {
       msg: "Error",
       label: {
         text: "error",
         start: $span.start,
         end: $span.end
       }
     }
   }
 }

> 2 | shl
Error:
  × Error
   ╭─[entry #91:1:1]
 1 │ def shl [] {
 2 │   let inp = $in
   ·             ─┬─
   ·              ╰── error
 3 │   let span = (metadata $inp).span
   ╰────

Example 2

> def "test bits" [] {
    0..4 | each {|it|
        if true {
            let span = (metadata $it).span  # This will show the wrong span
            error make {
                msg: "Test mismatch",
                label: {
                    text: $"error"
                    start: $span.start
                    end: $span.end
                }
            }
        }
    }
}

> test bits
Error:
  × Test mismatch
    ╭─[entry #4:2:1]
  2 │         0..4 | each {
  3 │ ╭─▶         if $true {
  4 │ │               let span = (metadata $it).span  # This will show the wrong span
  5 │ │               error make {
  6 │ │                   msg: "Test mismatch",
  7 │ │                   label: {
  8 │ │                       text: $"error"
  9 │ │                       start: $span.start
 10 │ │                       end: $span.end
 11 │ │                   }
 12 │ │               }
 13 │ ├─▶         }
    · ╰──── error
 14 │         }
    ╰────

> def "test bits" [] {
     0..4 | each {|it|
         let span = (metadata $it).span  # This will show a better span
         if true {
             error make {
                 msg: "Test mismatch",
                 label: {
                     text: $"error"
                     start: $span.start
                     end: $span.end
                 }
             }
         }
     }
 }

> test bits
Error:
  × Test mismatch
   ╭─[entry #6:1:1]
 1 │ def "test bits" [] {
 2 │     0..4 | each {
   ·     ┬
   ·     ╰── error
 3 │         let span = (metadata $it).span  
   ╰────

Expected behavior

Ideally, the true span of $it, or $inp would be shown and would not depend on where the $span variable is defined.

Screenshots

No response

Configuration

key value
version 0.59.0
branch main
short_commit 1fd7b9a
commit_hash 1fd7b9a
commit_date 2022-02-12 11:11:54 +00:00
build_os linux-x86_64
rust_version rustc 1.58.1 (db9d1b20b 2022-01-20)
rust_channel stable-x86_64-unknown-linux-gnu
cargo_version cargo 1.58.0 (f01b232bc 2022-01-19)
pkg_version 0.59.0
build_time 2022-02-12 14:03:38 +02:00
build_rust_channel release
features dataframe, default, trash, which, zip
installed_plugins gstat

Additional context

No response

Activity

  1. added
    category:bug
    on Feb 13, 2022
  2. kubouch commented on May 20, 2022

    @kubouch
    ContributorAuthor

    The issue appeared again in both examples.

    key value
    version 0.62.1
    branch
    commit_hash 0d06b62
    build_os linux-x86_64
    rust_version rustc 1.60.0 (7737e0b5c 2022-04-04)
    rust_channel stable-x86_64-unknown-linux-gnu
    cargo_version cargo 1.60.0 (d1fd9fe 2022-03-01)
    pkg_version 0.62.1
    build_time 2022-05-20 10:27:34 +03:00
    build_rust_channel release
    features database, dataframe, default, trash, which, zip
    installed_plugins gstat
  3. sophiajt commented on Aug 10, 2023

    @sophiajt
    Contributor

    It's a little better now, but not great:

    ~/Disks> 2 | shl                                                 08/11/2023 03:45:38 AM
    Error:   × Error
       ╭─[entry #200:1:1]
     1 │ def shl [] {
     2 │    let inp = $in
       ·    ─┬─
       ·     ╰── error
     3 │    if true {
       ╰────
    
  4. added
    A:error-unhelpful
    A:error-handling
    on Aug 15, 2024
  5. Bahex commented on Jul 26, 2026

    @Bahex
    Member

    This seems to be partially solved? error make's labels' structure is now:

    record<text: string, span: record<start: int, end: int>
    

    Rather than:

    record<text: string, start: int, end: int>
    

    And it works as expected when the span is in its own record:

    def shl [] {
        let inp = $in
        if true {
            let span = (metadata $inp).span
            error make {
                msg: "Error",
                label: {
                    text: "error",
                    span: $span
                }
            }
        }
    }
    #=> Error: nu::shell::error
    #=> 
    #=>   × Error
    #=>    ╭─[repl_entry #71:2:15]
    #=>  1 │ def shl [] {
    #=>  2 │     let inp = $in
    #=>    ·               ─┬─
    #=>    ·                ╰── error
    #=>  3 │     if true {
    #=>  4 │         let span = (metadata $inp).span
    #=>    ╰────

    But make error seemingly accepts the older structure as well, but it always points to the label field because that label record is invalid and should raise an error. However it doesn't appear as its own distinct error and leads to confusion.

  6. added this to the v0.115.0 milestone on Jul 29, 2026
  7. added a commit that references this issue on Jul 29, 2026
    cc7f044
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:error-handlingA:error-unhelpfulcategory:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions