Skip to content

Nushell no longer respects file associations on Windows #6440

Closed

Description

@CAD97

Describe the bug

e.g. given some x.py and a file association of

>ASSOC .py
.py=Python.File
>FTYPE Python.File
Python.File="C:\Windows\py.exe" "%L" %*

How to reproduce

〉x.py --help
Error: nu::shell::external_command (link)

  × External command failed
   ╭─[entry #4:1:1]
 1 │ x.py --help
   · ──┬─
   ·   ╰── did you mean 'cp'?
   ╰────
  help: program not found

〉./x.py --help
Error: nu::shell::external_command (link)

  × External command failed
   ╭─[entry #5:1:1]
 1 │ ./x.py --help
   · ───┬──
   ·    ╰── can't run executable
   ╰────
  help: %1 is not a valid Win32 application. (os error 193)

Expected behavior

The file should be run using the file association. This is what happened when running via a cmd /c host shell for execution.

Screenshots

No response

Configuration

key value
version 0.67.0
branch
commit_hash 91976988f032276ca48faff2d1ac13c079eda836
build_os windows-x86_64
build_target x86_64-pc-windows-msvc
rust_version rustc 1.63.0 (4b91a6ea7 2022-08-08)
rust_channel stable-x86_64-pc-windows-msvc
cargo_version cargo 1.63.0 (fd9c4297c 2022-07-01)
pkg_version 0.67.0
build_time 2022-08-17 15:20:56 +00:00
build_rust_channel release
features database, dataframe, default, trash, which, zip
installed_plugins

Additional context

The Windows documentation for file types is https://docs.microsoft.com/en-us/windows/win32/shell/fa-file-types. If I'm reading this right, in order to read the file associations you need to read the registry directly.

Activity

  1. rgwood commented on Aug 29, 2022

    @rgwood
    Contributor

    Hmm. My thinking is that running foo in Nushell means "attempt to run a command or executable named foo", not "open foo as if it was double-clicked in Explorer." If you need to do the later, start (i.e. start x.py) works for that.

    I'm open to other points of view, but I think the new behaviour is correct.

  2. CAD97 commented on Aug 29, 2022

    @CAD97
    ContributorAuthor

    Counterpoint: pwsh launches scripts in this manner. At the very least, even if nushell doesn't use the Windows filetype associations, it probably should have some functionality to allow running scripts without specifying the interpreter explicitly.

    If nushell respects shebang lines on unix, imho it should respect file associations on Windows, which is roughly the equivalent feature.

  3. CAD97 commented on Aug 29, 2022

    @CAD97
    ContributorAuthor

    Also of note; ./x.py should run the script in the same terminal; start x.py opens it in a new terminal window.

  4. self-assigned this
    on Aug 29, 2022
  5. rgwood commented on Aug 29, 2022

    @rgwood
    Contributor

    I think I'm coming around to your point of view. Would be nice if ./x.py and .\x.py worked the way they do in PowerShell - I'll see if I can make that happen.

  6. added
    deprecated:platform-specific
    on Aug 29, 2022
  7. rgwood commented on Sep 5, 2022

    @rgwood
    Contributor

    @CAD97 I might be busy with job stuff for a little while. Are you interested in picking this up (design and/or implementation)?

  8. rgwood commented on Sep 7, 2022

    @rgwood
    Contributor

    I just gave this a try, and it works for me on Windows 11. I'm running main from ~4 days ago.
    image

    Nu Version

    key value
    version 0.67.1
    branch main
    commit_hash 34e58bc
    build_os windows-x86_64
    build_target x86_64-pc-windows-msvc
    rust_version rustc 1.61.0 (fe5b13d68 2022-05-18)
    rust_channel 1.61.0-x86_64-pc-windows-msvc
    cargo_version cargo 1.61.0 (a028ae42f 2022-04-29)
    pkg_version 0.67.1
    build_time 2022-09-02 09:47:59 -07:00
    build_rust_channel release
    features database, default, trash, which, zip
    installed_plugins
  9. CAD97 commented on Sep 7, 2022

    @CAD97
    ContributorAuthor

    ...huh, I wonder what changed. It's working for me as well now, with

    key value
    version 0.68.0
    branch
    commit_hash b0e5723
    build_os windows-x86_64
    build_target x86_64-pc-windows-msvc
    rust_version rustc 1.63.0 (4b91a6ea7 2022-08-08)
    rust_channel stable-x86_64-pc-windows-msvc
    cargo_version cargo 1.63.0 (fd9c4297c 2022-07-01)
    pkg_version 0.68.0
    build_time 2022-09-06 20:03:51 +00:00
    build_rust_channel release
    features database, dataframe, default, trash, which, zip
    installed_plugins

    I guess I'll close this, then.

  10. CAD97 commented on Sep 7, 2022

    @CAD97
    ContributorAuthor

    I bet it's #6278; if which is finding the file, then IIUC it'll get run through cmd which will do the dispatch to the associated interpreter.

    I'd need to step the procedure to be sure, though. (The "is same name" check will be false because ./x.py != x.py, as it's comparing the command name to the resolved file name.)

  11. CAD97 commented on Sep 7, 2022

    @CAD97
    ContributorAuthor

    Confirmed

    D:\git\rust-lang〉.\rust\x.py --help
    'x.py' is not recognized as an internal or external command,
    operable program or batch file.
    
  12. rgwood commented on Sep 7, 2022

    @rgwood
    Contributor

    Interesting. I wouldn't expect #6278 to fix this; you're sure it was that commit?

  13. added this to the v0.68.0 milestone on Sep 7, 2022
  14. CAD97 commented on Sep 7, 2022

    @CAD97
    ContributorAuthor

    It's definitely an accidental "fix". What's happening is roughly

    $command = "./x.py"
    $path = $command | path canonicalize
    if $command != ($path | path file-name) {
        ^cmd /c (path | file-name)
    }

    and the if is taken since ./x.py != x.py

  15. rgwood commented on Sep 7, 2022

    @rgwood
    Contributor

    Did you confirm that #6278 is the change that fixed this?

  16. rgwood commented on Sep 7, 2022

    @rgwood
    Contributor

    I’m trying to understand whether you manually tested Nushell before+after that commit, or if you’re drawing conclusions based on a reading of the code/PR.

  17. CAD97 commented on Sep 7, 2022

    @CAD97
    ContributorAuthor

    Oh, sorry; I'm drawing conclusions from reading the code that launches the executable and observing behavior; I haven't specifically tested that it was that change, but it's the only one that touched the extern dispatch, so it kind of has to be it.

  18. rgwood commented on Sep 7, 2022

    @rgwood
    Contributor

    I did the bisect, and it's not that change. It's the which-rs upgrade from v4.2.2 to v4.3.0:

    commit 11531b76303d8c883f99b4098e678ae0e9ef7bb0
    Author: Reilly Wood <[email protected]>
    Date:   Wed Aug 31 09:50:18 2022 -0700
    
        Upgrade `which` dependency to fix case on Windows (#6453)
    
     Cargo.lock                   | 6 +++---
     crates/nu-command/Cargo.toml | 2 +-
     2 files changed, 4 insertions(+), 4 deletions(-)
    

    I think this PR is the one that changed it: harryfei/which-rs#57

  19. CAD97 commented on Sep 7, 2022

    @CAD97
    ContributorAuthor

    So we're kindof both right, in that it is the which path executing it, but that the behavior changed after that PR when updating the dependency.

    That said, the "proper" behavior is probably to

    • Determine the file to execute by checking the given path and the given path with exts from PATHEXT (what which is doing)
    • Determine how to run the file (assoc/ftype) similarly to how pwsh/cmd do; this does use the registry file associations (even in cmd!) and not the legacy ones visible through assoc/ftype. For example, try "running" a .html in cmd; ftype says iexplore.exe but it will be your actual browser choice.

    The point is to "act like a shell" in terms of executing files; matching the behavior of cmd and pwsh in launching files is probably better than being "pure" and only launching directly-executables or PATHEXT executables.

  20. CAD97 commented on Sep 8, 2022

    @CAD97
    ContributorAuthor

    Did a bit of research. pwsh use{s|d} ShellExecuteEx to launch documents (their name for files which aren't directly executable). This means that if you do ./test.py in pwsh, it'll open in a new window! (I could've sworn it wasn't that way previously...)

    I also wrote a bit of code to read the file type associations, which appears to be working, but it's a huge tail of edge cases. It might be better to just use ShellExecuteEx on windows, with the SEE_MASK_NO_CONSOLE flag to inherit the parent's console. Using ShellExecuteEx also allows us to easily tell the difference between opening the file failing (returns FALSE; check GetLastError) and the process failing (outptr process handle).

  21. rgwood commented on Sep 8, 2022

    @rgwood
    Contributor

    Nice, thanks for the research.

    I'm kinda leaning toward leaving things as-is, since the desired Python functionality works as expected. Are there specific cases you'd like to behave differently?

  22. PJB3005 commented on Jul 8, 2023

    @PJB3005

    Sorry if I should open a new issue for this, but this still seems broken if the Python script you're trying to run isn't in the current directory:

    image

  23. pfmoore commented on Dec 28, 2024

    @pfmoore

    Did a bit of research. PowerShell/PowerShell#4523 ShellExecuteEx to launch documents (their name for files which aren't directly executable). This means that if you do ./test.py in pwsh, it'll open in a new window! (I could've sworn it wasn't that way previously...)

    Sorry to comment on an old and closed issue, but this is not the case for me. If you have .py in your PATHEXT environment variable, it does run the command in the same console window - I've tested this in both Powershell (powershell.exe) and Powershell Core (pwsh.exe). Without the PATHEXT setting, it does open a new window as you describe.

  24. pfmoore commented on Dec 28, 2024

    @pfmoore

    This is a really useful bit of functionality, which I'd like to have available (in some form - it doesn't have to be via the PATHEXT mechanism) in nushell.

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

Metadata

Metadata

Assignees

Labels

deprecated:platform-specific

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions