Skip to content

nu::shell::io_error when trying to run file on windows #13020

Closed

Description

@Araxeus

Describe the bug

When trying to run a powershell .ps1 script any file (except .bat) on windows, we get an error:

Error: nu::shell::io_error

  × I/O error
  help: %1 is not a valid Win32 application. (os error 193)

How to reproduce

  1. create a powershell .ps1 file containing anything, for example (Get-ChildItem).Count
  2. try to run it via nushell by calling the file name, for example ./childCount.ps1
  3. get error

Expected behavior

Run the powershell script and print the output

Screenshots

image

Configuration

key value
version 0.94.1
major 0
minor 94
patch 1
branch
commit_hash
build_os windows-x86_64
build_target x86_64-pc-windows-msvc
rust_version rustc 1.78.0 (9b00956e5 2024-04-29)
rust_channel stable-x86_64-pc-windows-msvc
cargo_version cargo 1.78.0 (54d8815d0 2024-03-26)
build_time 2024-05-31 15:27:30 +03:00
build_rust_channel release
allocator mimalloc
features default, sqlite, system-clipboard, trash, which
installed_plugins

Additional context

I remember it used to work in some previous nushell version, but I might be wrong

  • pwsh -f ./childCount.ps1 obviously works
  • ./batscript.bat works

Activity

  1. added
    status:needs-triage
    on May 31, 2024
  2. changed the title [-]`nu::shell::io_error` when trying to run powershell .ps1 script [windows][/-] [+]`nu::shell::io_error` when trying to run file on windows[/+] on May 31, 2024
  3. Araxeus commented on May 31, 2024

    @Araxeus
    Author

    After further inspection this has nothing to do with powershell, I can replicate the bug by creating an empty test.txt file and running in nushell ./test.txt

    image

  4. fdncred commented on May 31, 2024

    @fdncred
    Contributor

    Did those work before 0.94.0? Seems like ps1 files would need pwsh file.ps1 and .txt files are not executable.

  5. Araxeus commented on May 31, 2024

    @Araxeus
    Author

    @fdncred I confirmed it used to work on v0.93.0, and broke on v0.94.0

    seems like #12921 broke it?

  6. kubouch commented on May 31, 2024

    @kubouch
    Contributor

    Duplicate of #13016.

  7. Araxeus commented on May 31, 2024

    @Araxeus
    Author

    Duplicate of #13016.

    doesn't look like it? neither description nor how to reproduce?

    did u debug why this is happening and saw it is the same reason or what?

    also it broke on v0.94.0 not v0.94.1?

  8. kubouch commented on May 31, 2024

    @kubouch
    Contributor

    Ah, oops, sorry, too many tabs open :-D.

  9. devyn commented on Jun 1, 2024

    @devyn
    Contributor

    I can't really figure out why this would have worked before. Maybe because I don't have this configured, but it seems like you also have to set PATHEXT and have the registry file association to open it?

    But ultimately we're still passing the ps1 file to std::process::Command::new(), which is what was happening before too as far as I can tell. I don't think this was triggering the behaviour where it was getting passed to cmd.exe because that was special cased for .cmd and .bat files.

  10. added
    category:bug
    status:investigate
    and removed
    status:needs-triage
    on Jun 1, 2024
  11. Araxeus commented on Jun 1, 2024

    @Araxeus
    Author

    I can't really figure out why this would have worked before.

    But ultimately we're still passing the ps1 file to std::process::Command::new(), which is what was happening before too as far as I can tell.

    dont know much about nu architecture but after some debugging I figured out alot.

    the current error in v0.94.0 is thrown here in command.spawn():

    let mut child = ForegroundChild::spawn(command)?;

    impl ForegroundChild {
    #[cfg(not(unix))]
    pub fn spawn(mut command: Command) -> io::Result<Self> {
    command.spawn().map(|child| Self { inner: child })
    }

    but here is what was happening in previous versions (<v0.93.0):

    input = ./typos.toml

    the code gets to here:

    #[cfg(windows)]
    let child = match ForegroundChild::spawn(cmd) {
    Ok(child) => Ok(child),
    Err(err) => {

    and fails with { code: 193, kind: Uncategorized, message: "%1 is not a valid Win32 application." }

    then we get to:

    if let Ok(which_path) =
    which::which_in(&self.name.item, Some(path_with_cwd), cwd)
    {

    which_path = "C:\\Git\\rust\\nushell\\typos.toml"

    if let Some(file_name) = which_path.file_name() {
    if !file_name.to_string_lossy().eq_ignore_case(command_name)
    {

    command_name = "./typos.toml" does not equal file name = "typos.toml"

    // which-rs found an executable file with a slightly different name
    // than the one the user tried. Let's try running it
    let mut new_command = self.clone();
    new_command.name = Spanned {
    item: file_name.to_string_lossy().to_string(),
    span: self.name.span,
    };
    let (cmd, new_reader) =
    new_command.create_process(&input, true, head)?;

    cmd = "cmd" "/D" "/c" "typos.toml"

    and then the child spawn successfully

    input = typos.toml

    now this one is kinda funny, it fails because ./ is missing which means the command name is equal to the file name, and we don't spawn another process

    we get to this if and stop:

    if let Some(file_name) = which_path.file_name() {
    if !file_name.to_string_lossy().eq_ignore_case(command_name)
    {

    command_name = "typos.toml" equal file name = "typos.toml"

  12. 19 remaining items

  13. fdncred commented on Nov 12, 2024

    @fdncred
    Contributor

    Hopefully this fixes the problem. It does for me.
    #14318

  14. youk commented on Nov 12, 2024

    @youk

    Hey, that was quick! Thank you.

  15. added a commit that references this issue on Nov 15, 2024
    f7832c0
  16. added this to the v0.101.0 milestone on Nov 15, 2024
  17. Araxeus commented on Nov 15, 2024

    @Araxeus
    Author

    This wasn't actually fixed and is an undocumented regression.

  18. fdncred commented on Nov 15, 2024

    @fdncred
    Contributor

    Executing nushell scripts should be fixed. Executing bat and cmd seems to work fine. Executing ps1 may not be something nushell needs to do without pwsh.exe. Executing text files is definitely not something nushell should do, that relies on windows and the file extension association system. That's what the start command is about.

  19. youk commented on Nov 15, 2024

    @youk

    Executing ps1 may not be something nushell needs to do without pwsh.exe. Executing text files is definitely not something nushell should do, that relies on windows and the file extension association system.

    I think the problem here is the apparent lack of design. Thus all these maybes and shoulds. When design is unclear the observable behavior becomes the design. As such, I can only agree that this is a regression. The issue shouldn't be closed as if it was resolved. Either it should be reopened, or closed as "won't fix".

  20. fdncred commented on Nov 15, 2024

    @fdncred
    Contributor

    We welcome people to come in and design things. However, we can't design everything since we're staffed by a team of volunteers, there are a million little details, and the team mostly does what they're interested in, like any open-source project.

    I can ask the core-team if they think ps1 files should be launch-able without specifying pwsh.exe and see what they think, so it's not only me making decisions. We also have weekly meetings that are open to the public that people can join and discuss items important to them.

    Side note, if we wanted to execute ps1 files we'd also have to know if the user wants to run powershell.exe or pwsh.exe. I'm not sure how we'd tell that by doing just "./test.ps1".

  21. fdncred commented on Nov 15, 2024

    @fdncred
    Contributor

    I'll re-open the issue so we can continue discussing how .ps1 should be handled.

  22. fdncred commented on Nov 15, 2024

    @fdncred
    Contributor

    If we do allow .ps1 to be easily executed, it opens up the possibility of consuming powershell output pretty easily. I mean the same thing could happen with powershell -Command 'Get-ChildItem | ConvertTo-Json' but it's a tad bit easier to have a script like below.

    ❯ open test.ps1
    Get-ChildItem | ConvertTo-Json

    image

  23. fdncred commented on Nov 20, 2024

    @fdncred
    Contributor

    Closing due to #14379

    Let me know if I should reopen for some reason again.

  24. zhruith commented on Dec 14, 2024

    @zhruith

    How about .py or .js ?

    U:\python> cmd
    U:\python>pi
    Ellapsed time: 0.07150912284851074 s
    number:99495000
    U:\python>exit

    U:\python> pwsh
    PS U:\python> ./pi
    Ellapsed time: 0.0743412971496582 s
    number:99495000
    PS U:\python> exit

    U:\python> pi.py
    Error: nu::shell::io_error

    × I/O error
    help: %1 is not a valid Win32 application. (os error 193)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:external-commandscategory:bugplatform:windowsstatus:investigate

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions