Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"jenv use" doesn't work in powershell #52

Open
9th0top opened this issue Apr 21, 2023 · 11 comments
Open

"jenv use" doesn't work in powershell #52

9th0top opened this issue Apr 21, 2023 · 11 comments

Comments

@9th0top
Copy link

9th0top commented Apr 21, 2023

powershell

Snipaste_2023-04-21_14-38-01

cmd
Snipaste_2023-04-21_14-37-38

@kid1412621
Copy link
Contributor

seems failed to set $Env:JENVUSE somehow. same issue here.

@kid1412621
Copy link
Contributor

kid1412621 commented May 23, 2023

seems invoking ps1 script via pwsh got another scope.

> pwsh -Command '& {$Env:var=1; echo "Inner: $Env:var"}' ; echo "outer: $Env:var"
Inner: 1
outer: 

I think the root cause is here. When pwsh invokes the script to run, it creates another process which shares different env vars scope.

pwsh -command '& { [System.Diagnostics.Process]::GetCurrentProcess() | Select-Object -ExpandProperty ID}' && [System.Diagnostics.Process]::GetCurrentProcess() | Select-Object -ExpandProperty ID
6224
17804

@kid1412621
Copy link
Contributor

hi @FelixSelter, I came upon a solution which might be not elegant. Each time run jenv use, the bat execution remains on the pwsh session. The env var takes effect. But the catch is the pwsh session will keep incrementing.

IF %1==use (
    %ps% -executionpolicy remotesigned -NoExit --NoLogo -File "%~dp0/src/jenv.ps1" %* --output
) ELSE (
    %ps% -executionpolicy remotesigned -File "%~dp0/src/jenv.ps1" %* --output
)

My question is, why we just ditch bat entirely, and use pwsh/powershll directly?

@FelixSelter
Copy link
Owner

FelixSelter commented May 23, 2023

We still need to use batch because there are windows users using cmd.

Normally the powershell script can change the env vars of powershell. The batch file is not ment to be used by powershell users as stated in the readme. Please call jenv.ps1 directly. Then no batch file is called and your env vars will be set correctly. At least I hope so. :) The batch scripts only purpose is to be called by cmd users

@kid1412621
Copy link
Contributor

We still need to use batch because there are windows users using cmd.

Normally the powershell script can change the env vars of powershell. The batch file is not ment to be used by powershell users as stated in the readme. Please call jenv.ps1 directly. Then no batch file is called and your env vars will be set correctly. At least I hope so. :) The batch scripts only purpose is to be called by cmd users

Thx for the explanation. The awkward situation is when JEnv-for-Windows directory put into PATH, the jenv.bat precedes the jenv.ps1(even putting JEnv-for-Windows/src before JEnv-for-Windows). If it's possible to provide jenv by installing it as PowerShell module.

@FelixSelter
Copy link
Owner

FelixSelter commented May 25, 2023 via email

@kid1412621
Copy link
Contributor

need minor change in java.bat.

where /q pwsh
IF ERRORLEVEL 1 (
    where /q powershell
    IF ERRORLEVEL 1 (
        echo Neither pwsh.exe nor powershell.exe was found in your path.
        echo Please install powershell it is required
        exit /B
    ) ELSE (
        set ps=powershell
    )
) ELSE (
    set ps=pwsh
)
for /f "delims=" %%i in ('%ps% -Command & {jenv getjava}') do set "var=%%i"

You may just delete or rename jenv.bat for now. All of these issues will be fixed once I have time to do a proper rewrite kid1412621 @.> schrieb am Do., 25. Mai 2023, 17:39:

We still need to use batch because there are windows users using cmd. Normally the powershell script can change the env vars of powershell. The batch file is not ment to be used by powershell users as stated in the readme. Please call jenv.ps1 directly. Then no batch file is called and your env vars will be set correctly. At least I hope so. :) The batch scripts only purpose is to be called by cmd users Thx for the explanation. The awkward situation is when JEnv-for-Windows directory put into PATH, the jenv.bat precedes the jenv.ps1(even putting JEnv-for-Windows/src before JEnv-for-Windows). If it's possible to provide jenv by installing it as PowerShell module. — Reply to this email directly, view it on GitHub <#52 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANHZIAVWSQYXJRXM76O7CU3XH54JHANCNFSM6AAAAAAXGOPDZU . You are receiving this because you were mentioned.Message ID: @.
>

Thanks, no worries. Take your time, and I'd glad to help.

@FelixSelter
Copy link
Owner

Thank you for your suggestion. I`ll keep this in mind when doing the proper rewrite. Its planned to add a fully functional powershell integration as well as many more for other shells. Tomorrow holidays will start so I have two weeks of partially spare time to enhance this tool

@darkmastermindz
Copy link

@FelixSelter I see this same issue while using Git Bash on Windows

longqinsi added a commit to longqinsi/JEnv-for-Windows that referenced this issue Jul 29, 2023
@longqinsi
Copy link

longqinsi commented Jul 29, 2023

This problem can be solved by adding a jenv.ps1 at the top level of this project:

$jenv_path = "${PSScriptRoot}/src/jenv.ps1" -replace ' ','` '
Invoke-Expression "${jenv_path} $args"

@longqinsi
Copy link

@FelixSelter I see this same issue while using Git Bash on Windows

if you use git bash on windows, you should use https://www.jenv.be/ instead of this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants