nushell / nushell/nushell

cp command gives an error when running from a script file but not from command line

Open
#7,386 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A:file-system A:scoping/name-resolution category:inconsistent-behavior usage:script-file
Dominant language
Rust
Stars
40.5k
Forks
2.3k
Avg merge
1d 19h
Merged PRs (30d)
85

Description

Describe the bug

I'm solving de Advent of Code 2022 and I have a short script that let's me initialize my template for solving the problems of a day. It goes like this:

def main [day_number: string] {
     let new_dir = $"day($day_number)/"
    cp -r day00 $new_dir
    code $new_dir
}

The cp command returns an error but when I run the same lines, one by one on the terminal, it just works!
Here's the error:

Error: nu::shell::directory_not_found (link)

  × Directory not found
   ╭─[initialize_day.nu:2:1]
 2 │     let new_dir = $"day($day_number)/"
 3 │     cp -r day00 $new_dir
   ·                 ────┬───
   ·                     ╰── directory not found
 4 │     code $new_dir 
   ╰────
  help: destination directory does not exist

This script worked fine in the previous version of nu I had installed, unfortunetly I don't remember which one it was, if I managed to get the version from the cache of pacman I'll update the issue with the version I had previously.

According to the pacman cache, the version I was using before was 0.71.0.

How to reproduce
  1. Save the script above to a .nu file.
  2. Have a dir named day00 in the same directory where you'll run the script.
  3. Run the command using nu {filename}.nu 02.
Expected behavior

I expected nu to work in the same way that it did when I inputted the same lines one by one.

Screenshots

No response

Configuration
key value
version 0.72.0
branch makepkg
commit_hash 8fa965118c2e5a0beaa528d07eaf9b33d96779cb
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.65.0 (Arch Linux rust 1:1.65.0-1)
cargo_version cargo 1.65.0
pkg_version 0.72.0
build_time 2022-11-30 06:57:53 +00:00
build_rust_channel release
features database, default, trash, which, zip
installed_plugins
Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the command from initialize_day.nu with Nushell 0.72.0, then compare it with running the same lines interactively using the provided day00 and day02 setup. Trace the cp invocation and its destination handling; done means the script and interactive command behave consistently, with a regression test covering the case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.