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

fix: mise does not operate well under Git Bash on Windows #4048

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

roele
Copy link
Contributor

@roele roele commented Jan 10, 2025

Fixes #4011

Gave this a try but detection of msys or cygwin does not work via $OSTYPE as this is not available via env so env::var() does not return anything. Not sure how reliable my approach via MSYSTEM and PWD is though.

Another unresolved issue seems to be related to line breaks on cygwin. The .bashrc file does not source successfully until its opened and saved (without changes). Probably would require a dos2unix on cygwin, not sure why that is not an issue on msys.

$ source .bashrc
': not a valid identifiernctions
-bash: .bashrc: line 35: syntax error near unexpected token `$'\r''
'bash: .bashrc: line 35: `function __zsh_like_cd() {

@roele roele force-pushed the issues/4011 branch 2 times, most recently from f90a9ea to 2a08412 Compare January 11, 2025 00:36
@jdx
Copy link
Owner

jdx commented Jan 11, 2025

for what it's worth, I'm not sure git bash is a necessary use-case for us. There are many alternatives like shims, powershell, and wsl for windows users.

@roele
Copy link
Contributor Author

roele commented Jan 11, 2025

I'm generally not a Windows user but I agree that shims and powershell seem to work well enough. Windows is a PITA anyways, in my last project Git Bash was my only option (was not allowed to use WSL or install anything else). While probably rare there might be some users restrained to such a setup.

@roele roele force-pushed the issues/4011 branch 2 times, most recently from 9554ca3 to acdaf32 Compare January 11, 2025 02:18
@jdx
Copy link
Owner

jdx commented Jan 11, 2025

right but even then shims will work. What won't work is you won't see env vars unless you wrap things in tasks but as I'm sure you've heard me explain, that's kind of how I think projects should be setup anyways since it doesn't rely on shell extensions.

mise en would be another alternative

@roele
Copy link
Contributor Author

roele commented Jan 11, 2025

I noticed that when using shims in context of cygwin/mysys a .cmd suffix has to be added though.

@jdx
Copy link
Owner

jdx commented Jan 11, 2025

isn't that already happening?

@roele
Copy link
Contributor Author

roele commented Jan 11, 2025

What i mean is that i would expect for example that i can use java -version in Git Bash but instead i have to use java.cmd -version.

@jdx
Copy link
Owner

jdx commented Jan 11, 2025

is this something that can be configured with PATHEXT? or is there a git bash equivalent for that?

@roele
Copy link
Contributor Author

roele commented Jan 13, 2025

PATHEXT is a Windows environment variable and i am not aware of an equivalent in Git Bash/Cygwin. Emulating PATHEXT in Bash does also not seem to work.

export PATHEXT=.com:.exe:.bat:.cmd

if declare -f command_not_found_handle >/dev/null; then 
    eval "original_command_not_found_handle() $(declare -f command_not_found_handle|tail -n +2)"
fi
command_not_found_handle(){
    local PATHEXT_EXPANDED i
    IFS=: read -a PATHEXT_EXPANDED<<<"$PATHEXT"
    for i in "${PATHEXT_EXPANDED[@]}"; do
        if type "$1$i" &>/dev/null; then
            "$1$i" "${@:2}"
            return $?
        fi
    done
    if declare -f original_command_not_found_handle >/dev/null; then
        original_command_not_found_handle "$@"
    else
        return 127
    fi
}

While this PR makes activate work for Git Bash and Cygwin detection seems a bit hacky. I wonder if we should drop this for now. Having at least the shims work would be great though.

@roele
Copy link
Contributor Author

roele commented Jan 13, 2025

The following hook might be a workaround for the shims .cmd ending issue. We might simply document this as possible workaround in the troubleshooting docs "Windows problems" section.

command_not_found_handle()
{
   cmd=$1
   shift
   args=( "$@" )

   IFS=:
   for dir in $PATH; do
      for executable in $dir/$cmd.bat $dir/$cmd.cmd $dir/$cmd.exe; do
         if [ -f "$executable" ]; then
            "$executable" "${args[@]}"
            return
         fi
      done
   done

   orig_command_not_found_handle "$cmd" "${args[@]}"
} 
if declare -f command_not_found_handle >/dev/null; then 
    eval "orig_command_not_found_handle() $(declare -f command_not_found_handle|tail -n +2)"
fi

@ffaen
Copy link

ffaen commented Jan 28, 2025

One possible fix for this is to add two shims on windows, one for cmd and one for git bash:

Tested with two shims for node

node.cmd (unchanged)

@echo off
setlocal
mise x -- node %*

node (no extension)

#!/usr/bin/env bash

mise x -- node "$@"

This works totally fine on Git Bash, Cmd, and Powershell (as cmd and powershell will ignore the file with no ext)

Unsure if this works well with the current implementation of reshim() as at first glance it expects a one-to-one shim to app? (My primary language isn't rust so happy to be wrong!)

@roele
Copy link
Contributor Author

roele commented Jan 29, 2025

@ffaen Was also thinking about something in that direction which should be doable.

src/shims.rs Outdated Show resolved Hide resolved
@roele roele marked this pull request as ready for review January 30, 2025 02:38
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

Successfully merging this pull request may close these issues.

mise does not operate well under GitBash on Windows
3 participants