nushell: Extending Path not working

Describe the bug

My line

  let-env Path = ($env.Path | append 'C:\Program Files\Git\usr\bin')

in env.nu or config.nu doesn’t seem to work

How to reproduce

  1. On windows add the line above in env.nu
  2. Observe the path does not include this entry by evaluating $env.Path

Expected behavior

Add the entry to the path

Screenshots

Having this in env.nu

  $env.Path
  let-env Path = ($env.Path | prepend 'C:\Program Files\Git\usr\bin')
  $env.Path

yields

C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86.......................................
╭───┬─────────
│ 0 │ C:\Program Files\Git\usr\bin                                                                                                                                 
│ 1 │ C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86.................... 

Configuration

key value
version 0.61.0
branch master
tag
short_commit 3783c19d
commit_hash 3783c19d02f4a3b2f35cd26b79daabcb70b04880
commit_date 2022-04-12 20:38:15 +00:00
build_os windows-x86_64
rust_version rustc 1.60.0 (7737e0b5c 2022-04-04)
rust_channel stable-x86_64-pc-windows-msvc
cargo_version cargo 1.60.0 (d1fd9fe2c 2022-03-01)
pkg_version 0.61.0
build_time 2022-04-12 21:07:36 +00:00
build_rust_channel release
features dataframe, default, trash, which, zip
installed_plugins

Additional context

In the env.nu and config.nu files if I print $env.Path I don’t see a list but a long string separated by ; characters.

The odd thing is that it doesn’t matter if I append or prepend. Nu seems to pick the longest item in the list or something.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 23 (1 by maintainers)

Most upvoted comments

ya, good point @filaretov. i think the problem with the most recent tries from @chtenb may be having the let-env inside of an if. so, your solution avoids that.

@chtenb

A workaround would be to define a function that returns the additional paths on a condition and then always set the path. If the condition doesn’t hold, then you would be extending the path with an empty list, like this:

def additional-paths-windows [] {
    let paths = if (sys).host.name == "Windows" {
        ['C:\Program Files\Git\usr\bin']
    } else {
        []
    }
    $paths
}

let-env Path = ($env.Path | prepend (additional-paths-windows))

You can also confine it to a single def-env:

def-env extendPathsWindows [] {
  let new-paths = if (sys).host.name == "Windows" {
    ['C:\Program Files\Git\usr\bin']
  } else {
    []
  }
  let-env Path = ($env.Path | prepend $new-paths)
}

extendPathsWindows