nushell: nu -c doesn't use env.nu in 0.83.0
Describe the bug
Attempting to use a module in the scripts directory, even after updating new default env.nu to include the scripts directory, results in a module not found error.
nu -c "use module.nu; ..."
When running use module.nu interactively, there is no error.
How to reproduce
Edit env.nu to incorporate the scripts directory.
$env.NU_LIB_DIRS = [
($nu.default-config-dir | path join 'scripts') # add <nushell-config-dir>/scripts
]
Add a module to the scripts directory.
Attempt to import the module when using the nu -c command, i.e. nu -c 'module.nu; ...'.
Expected behavior
I expect the env.nu file to be loaded when using nu -c to run commands, since I use modules with these commands. There is no indication this should not be the case and this is how it worked prior to version 0.83.0.
Screenshots
No response
Configuration
| key | value |
|---|---|
| version | 0.83.0 |
| branch | |
| commit_hash | a33b5fe6ce97b5e9fe8a774c13e783ed65c1b591 |
| build_os | linux-x86_64 |
| build_target | x86_64-unknown-linux-gnu |
| rust_version | rustc 1.69.0 (84c898d65 2023-04-16) |
| rust_channel | 1.69.0-x86_64-unknown-linux-gnu |
| cargo_version | cargo 1.69.0 (6e9a83356 2023-04-12) |
| build_time | 2023-07-25 19:48:46 +00:00 |
| build_rust_channel | release |
| allocator | standard |
| features | default, sqlite, static-link-openssl, trash, which, zip |
| installed_plugins |
Additional context
cat ~/.config/nushell/env.nu
# Nushell Environment Config File
#
# version = 0.82.1
def create_left_prompt [] {
mut home = ""
try {
if $nu.os-info.name == "windows" {
$home = $env.USERPROFILE
} else {
$home = $env.HOME
}
}
let dir = ([
($env.PWD | str substring 0..($home | str length) | str replace --string $home "~"),
($env.PWD | str substring ($home | str length)..)
] | str join)
let path_color = (if (is-admin) { ansi red_bold } else { ansi green_bold })
let separator_color = (if (is-admin) { ansi light_red_bold } else { ansi light_green_bold })
let path_segment = $"($path_color)($dir)"
$path_segment | str replace --all --string (char path_sep) $"($separator_color)/($path_color)"
}
def create_right_prompt [] {
# create a right prompt in magenta with green separators and am/pm underlined
let time_segment = ([
(ansi reset)
(ansi magenta)
(date now | date format '%Y/%m/%d %r')
] | str join | str replace --all "([/:])" $"(ansi green)${1}(ansi magenta)" |
str replace --all "([AP]M)" $"(ansi magenta_underline)${1}")
let last_exit_code = if ($env.LAST_EXIT_CODE != 0) {([
(ansi rb)
($env.LAST_EXIT_CODE)
] | str join)
} else { "" }
([$last_exit_code, (char space), $time_segment] | str join)
}
# Use nushell functions to define your right and left prompt
$env.PROMPT_COMMAND = {|| create_left_prompt }
# $env.PROMPT_COMMAND_RIGHT = {|| create_right_prompt }
# The prompt indicators are environmental variables that represent
# the state of the prompt
$env.PROMPT_INDICATOR = {|| " > " }
$env.PROMPT_INDICATOR_VI_INSERT = {|| " : " }
$env.PROMPT_INDICATOR_VI_NORMAL = {|| " > " }
$env.PROMPT_MULTILINE_INDICATOR = {|| "::: " }
# Specifies how environment variables are:
# - converted from a string to a value on Nushell startup (from_string)
# - converted from a value back to a string when running external commands (to_string)
# Note: The conversions happen *after* config.nu is loaded
$env.ENV_CONVERSIONS = {
"PATH": {
from_string: { |s| $s | split row (char esep) | path expand --no-symlink }
to_string: { |v| $v | path expand --no-symlink | str join (char esep) }
}
"Path": {
from_string: { |s| $s | split row (char esep) | path expand --no-symlink }
to_string: { |v| $v | path expand --no-symlink | str join (char esep) }
}
}
# Directories to search for scripts when calling source or use
$env.NU_LIB_DIRS = [
($nu.default-config-dir | path join 'scripts') # add <nushell-config-dir>/scripts
]
# Directories to search for plugin binaries when calling register
$env.NU_PLUGIN_DIRS = [
# ($nu.default-config-dir | path join 'plugins') # add <nushell-config-dir>/plugins
]
# To add entries to PATH (on Windows you might use Path), you can use the following pattern:
# $env.PATH = ($env.PATH | split row (char esep) | prepend '/some/path')
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 16 (5 by maintainers)
This is why I said itโs a bug and we need to fix it in the rust code.
After reading this issue https://github.com/nushell/nushell/issues/9863 it occurred to me that this probably is a bug because we changed the default_env.nu which previously had a path in it for NU_LIB_DIRS. I think we need to revert the line in the NU_LIB_DIRS so we have something people can count on for a known path for scripts. We may also need to add it to the rust code.
Iโve been using this feature since at least 0.79.0 and each new version since as part of our CI process.
I did some testing this morning by adding some log events with the
warn!()macro torun.rs. From what I can tell, it seems like the env.nu file is being loaded.Hereโs a couple results.
This first one shows what happens when you do
nu -c "ls". It loads thedefault_env.nufile.Here I am passing the
--configand--env-configparameters, and it seems to load them.So, the moral of the story is, if you want to load your personal
env.nufile, I believe you have to list it as a command line parameter with--env-config. Otherwise, it will load thedefault_env.nufile. I think nushell is working as designed.