Schniz / fnm

🚀 Fast and simple Node.js version manager, built in Rust
https://fnm.vercel.app
GNU General Public License v3.0
17.52k stars 445 forks source link

fnm env produce error: Can't infer shell! #561

Closed codeitlikemiley closed 2 years ago

codeitlikemiley commented 2 years ago

Recently im facing this error,

error: Can't infer shell!
fnm can't infer your shell based on the process tree.
Maybe it is unsupported? we support the following shells:
* cmd
* powershell
* bash
* zsh
* fish

Im using windows 11 , and before im not facing this issue, i did try to reinstall fnm and build from cargo but the error persist.

is there a way to fix this? Only in windows this is persisting, im using it in all my dev machine, artix linux, mac big sur no problem.

hope this will be resolve coz the command wont simple generate the correct fnm path for windows now. so the command node and npm are both not found.

i just use pure node js installation for now until this is resolve thanks for building fnm

Discreater commented 2 years ago

Same problem

Schniz commented 2 years ago

Please try to call RUST_LOG=fnm=debug fnm env instead of fnm env and send here the output 😃

ghost commented 2 years ago

Please try to call RUST_LOG=fnm=debug fnm env instead of fnm env and send here the output 😃

Does this work on windows, this is the results from Powershell preview

$env:RUST_LOG="debug" ; fnm env 
error: Can't infer shell!
fnm can't infer your shell based on the process tree.
Maybe it is unsupported? we support the following shells:
* cmd
* powershell
* bash
* zsh
* fish
Schniz commented 2 years ago

Should work. Do you know what the executable name is of PowerShell preview?

dabide commented 2 years ago

It is still pwsh.exe, but it doesn't work anymore. Could be a permissions issue in Windows 11.

Schniz commented 2 years ago

@rhacker Ahhhh now I see that I did not add the debug logging in Windows. I'm an idiot. Sorry!

the free Windows dev-VM is still Windows 10 and worked perfectly for me. Can you try cloning the repo and building it from source, and change the following line:

https://github.com/Schniz/fnm/blob/8d0d78ca2463be2efa1550cb87b40d6d9635723d/src/shell/infer/windows.rs#L33

into something like:

https://github.com/Schniz/fnm/blob/8d0d78ca2463be2efa1550cb87b40d6d9635723d/src/shell/infer/unix.rs#L33

that would be a great (and fairly simple) contribution, and would help debug your case 😃 🙏

dabide commented 2 years ago

Found the reason: wmic was deprecated earlier this year, and is no longer there in the latest Insiders build. https://docs.microsoft.com/en-us/windows/win32/wmisdk/wmic

dabide commented 2 years ago

You could perhaps replace it with tasklist.exe?

Schniz commented 2 years ago

tasklist.exe does not seem to provide parent process id. Do you know any alternatives? Or maybe it does support it and it is not documented? I'm not a Windows user :(

ghost commented 2 years ago

tasklist.exe does not seem to provide parent process id. Do you know any alternatives? Or maybe it does support it and it is not documented? I'm not a Windows user :(

I'm not sure if this works for us

$p = Get-Process pwsh
$p.Parent.Id
15024
15024

it's powershell script, to spin powershell process with command, we can try powershell.exe --comand "<command-above>"

ghost commented 2 years ago

not so familiar with rust, I have tried to print! out some debug message in the infer/windows.rs but it doesn't seem to work.

Schniz commented 2 years ago

I will try taking a look on my free time. I'm not a Windows user so I will try migrating off wmic, but I don't know if this will work on Win11. The VM I will use is https://developer.microsoft.com/en-us/windows/downloads/virtual-machines/

ghost commented 2 years ago

I think you might need the windows 11 insider latest build to get this error.

dabide commented 2 years ago

@Schniz This command might work, but you will need to skip the first line of the the output, and possibly handle quoting. I don't know Rust at all, so I can't help...

    let stdout = std::process::Command::new("powershell")
        .args(&[
            "-command",
            "Get-CimInstance",
            "-ClassName",
            "win32_process",
            "|",
            "Select-Object",
            "-property",
            "\"ProcessID\",\"ParentProcessID\",\"Name\"",
            "|",
            "ConvertTo-CSV",
        ])

The output of the command looks like this:

#TYPE Selected.Microsoft.Management.Infrastructure.CimInstance
"ProcessID","ParentProcessID","Name"
"0","0","System Idle Process"
"4","0","System"
"168","4","Secure System"
"248","4","Registry"

You should be able to test this in your Windows 10 VM.

Schniz commented 2 years ago

Amazing @dabide! Let me play with it

dabide commented 2 years ago

I was too tired last night, but now I managed to get it to work, and created a PR. 😄

dabide commented 2 years ago

@Schniz But perhaps it would be faster to use this instead of spinning up an external command? https://docs.rs/sysinfo/0.3.19/sysinfo/trait.SystemExt.html#tymethod.get_process_list

Schniz commented 2 years ago

@Schniz But perhaps it would be faster to use this instead of spinning up an external command? https://docs.rs/sysinfo/0.3.19/sysinfo/trait.SystemExt.html#tymethod.get_process_list

Interesting! This can simplify the code if it supports all platforms. The most updated version is https://docs.rs/sysinfo/0.20.5/sysinfo/trait.SystemExt.html#tymethod.processes

Schniz commented 2 years ago

And I was concerned about spawning PowerShell performance hit so it can be amazing to use this crate

dabide commented 2 years ago

Seems that it is supported on all platforms: https://docs.rs/sysinfo/0.20.5/sysinfo/index.html#supported-oses

dabide commented 2 years ago

There is a workaround, @codeitlikemiley : You can specify the shell on the command line, like this: fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression

ghost commented 2 years ago

@dabide thanks for this, it works perfectly fine :)

Schniz commented 2 years ago

I really think about deprecating the automated shell check, it makes so much sense to just provide it when installing. The magic is fragile.

radiorz commented 3 months ago

There is a workaround, @codeitlikemiley : You can specify the shell on the command line, like this: fnm env --use-on-cd --shell powershell | Out-String | Invoke-Expression

works for me: fnm env --use-on-cd --shell power-shell | Out-String | Invoke-Expression

mk6281 commented 3 months ago

i am also facing this issue