Skip to content

Bad error msg with variable span covering large code block #15160

@godalming123

Description

@godalming123

Describe the bug

I'm getting an error message that covers 30 lines of my script, and is not clear about what went wrong.

How to reproduce

Put the following code in a nu script file

#!/bin/nu

def get_window_type [container] {
  match $container.app_id {
    "com.mitchellh.ghostty"|"Alacritty"|"org.gnome.Terminal"|"foot" => {return "terminal"}
    "org.qutebrowser.qutebrowser"|"zen"|"org.mozilla.firefox"|"org.gnome.Epiphany"|"chromium-browser" => {return "web_browser"}
  }
  match $container.window_properties?.class? {
    "Chromium-browser" => {return "web_browser"}
  }
  match $container.name {
    "gf2" => "debugger"
    _ => "other"
  }
}

^echo -m -t subscribe '["window"]' | lines | reduce --fold [] {
  |line recently_used_windows|
  let event = $line | from json

  # Set $out (the recently used windows for the next iteration of reduce)
  let window_details = {
    id: $event.container.id,
    type: (get_window_type $event.container),
  }
  let out = $recently_used_windows | match $event.change {
    # See the window section of the events section of the `man 7 sway-ipc`
    # command output for a full list of events.
    "new"   => ($in | prepend $window_details)
    "close" => ($in | filter {|e| $e.id != $event.contaier.id})
    "focus" => ($in | filter {|e| $e.id != $event.contaier.id} | prepend $window_details)
    _       => {return $in}
  }

  # Set the _prev flags based on $out
  $out | skip 1 | (reduce --fold [] {|window, handled_types|
    if $window.type in $handled_types { $handled_types } else {
      swaymsg $"[con_id=($window.id)] mark --add _prev_($window.type)"
      $handled_types | append $window.type
    }
  })

  # Return $out (the recently used windows for the next iteration of reduce)
  return $out
}

Try to run the file

nu FILE_NAME.nu

See an error that covers from lines 17 to 45

Error: nu::shell::variable_not_found

  × Variable not found
    ╭─[/home/jg/.config/sway/update-prev-focus.nu:17:65]
 16 │
 17 │ ╭─▶ swaymsg -m -t subscribe '["window"]' | lines | reduce --fold [] {
 18 │ │     |line recently_used_windows|
 19 │ │     let event = $line | from json
 20 │ │
 21 │ │     # Set $out (the recently used windows for the next iteration of reduce)
 22 │ │     let window_details = {
 23 │ │       id: $event.container.id,
 24 │ │       type: (get_window_type $event.container),
 25 │ │     }
 26 │ │     let out = $recently_used_windows | match $event.change {
 27 │ │       # See the window section of the events section of the `man 7 sway-ipc`
 28 │ │       # command output for a full list of events.
 29 │ │       "new"   => ($in | prepend $window_details)
 30 │ │       "close" => ($in | filter {|e| $e.id != $event.contaier.id})
 31 │ │       "focus" => ($in | filter {|e| $e.id != $event.contaier.id} | prepend $window_details)
 32 │ │       _       => {return $in}
 33 │ │     }
 34 │ │
 35 │ │     # Set the _prev flags based on $out
 36 │ │     $out | skip 1 | (reduce --fold [] {|window, handled_types|
 37 │ │       if $window.type in $handled_types { $handled_types } else {
 38 │ │         swaymsg $"[con_id=($window.id)] mark --add _prev_($window.type)"
 39 │ │         $handled_types | append $window.type
 40 │ │       }
 41 │ │     })
 42 │ │
 43 │ │     # Return $out (the recently used windows for the next iteration of reduce)
 44 │ │     return $out
 45 │ ├─▶ }
    · ╰──── variable not found
    ╰────

Expected behavior

Nu shell should either run the script successfully, or give a more precise error.

Configuration

key value
version 0.102.0
major 0
minor 102
patch 0
branch
commit_hash
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.84.1 (e71f9a9a9 2025-01-27) (Homebrew)
cargo_version cargo 1.84.1
build_time 2025-02-04 15:49:35 +00:00
build_rust_channel release
allocator mimalloc
features default, sqlite, trash
installed_plugins

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:error-unhelpfulThe error message you observe is not helpful to identify the problemA:scoping/name-resolutionHow Nu finds which variables/functions are in scope and to what they are bound

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions