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)

Commits related to this issue

Most upvoted comments

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.

That totally makes sense, I just wasnโ€™t aware of the change in behavior from previous versions.

what was your previous version? ๐Ÿ˜‹

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 to run.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 the default_env.nu file.

cargo r -- --log-level warn -c "ls"
    Finished dev [unoptimized + debuginfo] target(s) in 0.20s
     Running `target/debug/nu --log-level warn -c ls`
run_commands
2023-07-29 12:03:56.678 PM [WARN ] nu::run: starting run_commands
2023-07-29 12:03:56.679 PM [WARN ] nu::run: parsed_nu_cli_args.no_config_file.is_none() == true
2023-07-29 12:03:56.679 PM [WARN ] nu::run: calling read_plugin_file
2023-07-29 12:03:56.718 PM [WARN ] nu::run: parsed_nu_cli_args.env_file.is_some() == false
2023-07-29 12:03:56.718 PM [WARN ] nu::run: calling read_default_env_file <-- This means it's loading the default_env.nu
2023-07-29 12:03:56.724 PM [WARN ] nu::run: just called set_startup_time
2023-07-29 12:03:56.724 PM [WARN ] nu::run: calling evaluate_commands
โ•ญโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚  # โ”‚        name         โ”‚ type โ”‚   size    โ”‚   modified   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  0 โ”‚ CODE_OF_CONDUCT.md  โ”‚ file โ”‚   3.4 KiB โ”‚ 4 months ago โ”‚
โ”‚  1 โ”‚ CONTRIBUTING.md     โ”‚ file โ”‚  10.4 KiB โ”‚ 3 weeks ago  โ”‚
โ”‚  2 โ”‚ Cargo.lock          โ”‚ file โ”‚ 139.1 KiB โ”‚ 2 days ago   โ”‚
โ”‚  3 โ”‚ Cargo.toml          โ”‚ file โ”‚   5.6 KiB โ”‚ 2 days ago   โ”‚
โ”‚  4 โ”‚ Cross.toml          โ”‚ file โ”‚     363 B โ”‚ 4 months ago โ”‚

Here I am passing the --config and --env-config parameters, and it seems to load them.

cargo r -- --log-level warn -c "ls" --config "/Users/username/Library/Application Support/nushell/config.nu" --env-config "/Users/username/Library/Application Support/nushell/env.nu"
   Compiling nu v0.83.1 (/Users/username/src/nushell)
    Finished dev [unoptimized + debuginfo] target(s) in 1.32s
     Running `target/debug/nu --log-level warn -c ls --config '/Users/username/Library/Application Support/nushell/config.nu' --env-config '/Users/username/Library/Application Support/nushell/env.nu'`
2023-07-29 12:07:03.560 PM [WARN ] nu::run: starting run_commands
2023-07-29 12:07:03.560 PM [WARN ] nu::run: parsed_nu_cli_args.no_config_file.is_none() == true
2023-07-29 12:07:03.560 PM [WARN ] nu::run: calling read_plugin_file
2023-07-29 12:07:03.598 PM [WARN ] nu::run: parsed_nu_cli_args.env_file.is_some() == true
2023-07-29 12:07:03.598 PM [WARN ] nu::run: calling read_config_file with: Some(Spanned { item: "/Users/username/Library/Application Support/nushell/env.nu", span: Span { start: 113, end: 172 } })
2023-07-29 12:07:03.607 PM [WARN ] nu::run: parsed_nu_cli_args.config_file.is_some() == true
2023-07-29 12:07:03.607 PM [WARN ] nu::run: calling read_config_file with: Some(Spanned { item: "/Users/username/Library/Application Support/nushell/config.nu", span: Span { start: 37, end: 99 } })
2023-07-29 12:07:03.934 PM [WARN ] nu::run: just called set_startup_time
2023-07-29 12:07:03.934 PM [WARN ] nu::run: calling evaluate_commands
โ•ญโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚  # โ”‚        name         โ”‚ type โ”‚   size   โ”‚   modified   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  0 โ”‚ CODE_OF_CONDUCT.md  โ”‚ file โ”‚   3.4 KB โ”‚ 4 months ago โ”‚
โ”‚  1 โ”‚ CONTRIBUTING.md     โ”‚ file โ”‚  10.6 KB โ”‚ 3 weeks ago  โ”‚
โ”‚  2 โ”‚ Cargo.lock          โ”‚ file โ”‚ 142.5 KB โ”‚ 2 days ago   โ”‚
โ”‚  3 โ”‚ Cargo.toml          โ”‚ file โ”‚   5.7 KB โ”‚ 2 days ago   โ”‚
โ”‚  4 โ”‚ Cross.toml          โ”‚ file โ”‚    363 B โ”‚ 4 months ago โ”‚

So, the moral of the story is, if you want to load your personal env.nu file, I believe you have to list it as a command line parameter with --env-config. Otherwise, it will load the default_env.nu file. I think nushell is working as designed.