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

jbang jdk default <version> fails on Windows #1793

Closed
jcrben opened this issue Apr 5, 2024 · 5 comments · Fixed by #1804
Closed

jbang jdk default <version> fails on Windows #1793

jcrben opened this issue Apr 5, 2024 · 5 comments · Fixed by #1804
Labels
bug Something isn't working

Comments

@jcrben
Copy link

jcrben commented Apr 5, 2024

Describe the bug
Follow-up to #1574 by @quintesse

This is when using "Git Bash" - and particularly the msys bash inside of it.

This is what I see:

Ben@t480-ben dotfiles [master]$ jbang jdk default 21
[jbang] Creation of symbolic link failed C:\Users\Ben\.jbang\currentjdk -> C:\Users\Ben\.jbang\cache\jdks\21
[jbang] Now try creating a hard link instead of symbolic.
[jbang] Creation of hard link failed C:\Users\Ben\.jbang\currentjdk -> C:\Users\Ben\.jbang\cache\jdks\21
[jbang] [ERROR] Failed to create link C:\Users\Ben\.jbang\currentjdk -> C:\Users\Ben\.jbang\cache\jdks\21
[jbang] Run with --verbose for more details

To Reproduce
I'm running bash on Windows 10. uname -a reports MSYS_NT-10.0-19045 t480-ben 3.4.7-25de8b84.x86_64 2023-08-28 21:32 UTC x86_64 Msys

I reproduced this in Powershell IIRC but my main way of working in Windows is inside cygwin / msys.

I execute via the Windows Terminal - I configure a profile like this after installing Git via winget install Git.Git. I'm using /Git/usr/bin/bash.exe which has the cygwin dll included, but not sure that matters.

            {
                "commandline": "%PROGRAMFILES%/Git/usr/bin/bash.exe -i -l",
                "guid": "{00000000-0000-0000-ba54-000000000002}",
                "icon": "%PROGRAMFILES%/Git/mingw64/share/git/git-for-windows.ico",
                "name": "Bash",
                "startingDirectory": "%USERPROFILE%"
            },
scoop install jbang
jbang jdk install 21
jbang jdk default 21

Expected behavior
jbang jdk default 21 should succeed

JBang version

$ jbang version --verbose
[jbang] [0:425] jbang version 0.116.0
Cache: C:\Users\Ben\.jbang\cache
Config: C:\Users\Ben\.jbang
Repository: C:\Users\Ben\.m2\repository
0.116.0

Additional context
I don't think requiring Developer Mode is good - many people developing in Windows are doing it for security reasons and it's hard convince a security department to enable Developer Mode.

As noted earlier in the linked issue, you can't hard link to directories. But there's another option - use junctions instead. fnm did that recently Schniz/fnm#519 - see also kubernetes-sigs/krew#843

An example of creating a junction from my initial dotfiles setup script: powershell.exe New-Item -ItemType Junction -Force -Path $(cygpath -m "$HOME/scoop/persist/vscode/data/user-data/User") -Target $(cygpath -m "$SCRIPT_DIR"/config/vscode/User)

@jcrben jcrben added the bug Something isn't working label Apr 5, 2024
@jcrben
Copy link
Author

jcrben commented Apr 5, 2024

I dropped into a debugger to see what was happening - fyi it skipped the if condition here which triggers the warning message.
image

Also these are the jdks I have installed:
image

@quintesse
Copy link
Contributor

quintesse commented Apr 5, 2024

Unfortunately I don't think there's an easy way to create junctions with Java, which is what we'd need.

Edit: btw, on Windows we shouldn't even try to create a Hard Link.
Edit2: Looking at the code I don't see how it even gets there, it should have given you the explanatory warning message...
Edit3: What JDK are you running by default? (typing java -version) and are you running a patched JBang? Because it shows unfamiliar output.

@jcrben
Copy link
Author

jcrben commented Apr 5, 2024

Not sure about the Java version, will check later. When debugging it's perhaps the vscode embedded (21). Otherwise 17 or 21

As fat as edit3, I was running the latest commit on main last night after running 0.116 from scoop

Looking at the screenshot of my debugging session closely shows you why it got there - Files.isDirectory(src) is false.

As far creating a junction in Java, how about calling powershell? It's built into Windows

@jcrben
Copy link
Author

jcrben commented May 27, 2024

@quintesse for the junction question - to avoid Developer Mode in a place where it isn't permitted - what are your thoughts on just calling powershell or cmd.exe as an external process on Windows? would like to be able to use this in a secured corporate environment

if not, maybe I'll just fork it privately inside the secure environment and maintain the forked version

@quintesse
Copy link
Contributor

quintesse added a commit to quintesse/jbang that referenced this issue Jun 6, 2024
quintesse added a commit to quintesse/jbang that referenced this issue Jun 7, 2024
maxandersen pushed a commit that referenced this issue Sep 12, 2024
* feat: we can now create junctions on Windows

Fixes #1793

* chore: made variable names clearer

* fix: fixed setting default JDK on Windows

On Windows it was possible to leave a JDK in a broken state by changing the default JDK

* chore: remove Dev Mode check from jbang script

Checking for Developer Mode when it's only necessary for the `edit` command will only bother users, best to only give the warning when actually needed

* docs: refined special Windows setup instructions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants