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

Add Support for MSYS2 in Windows #1605

Closed
3 tasks
danyeaw opened this issue Sep 20, 2020 · 8 comments · Fixed by #1606
Closed
3 tasks

Add Support for MSYS2 in Windows #1605

danyeaw opened this issue Sep 20, 2020 · 8 comments · Fixed by #1606
Labels

Comments

@danyeaw
Copy link

danyeaw commented Sep 20, 2020

Thanks @asottile and all the other contributors for the great tool! ❤️ 👍

I am one of the maintainers for Gaphor, and we make heavy use of pre-commit as part of our work flow. It is also built in GTK, and we have to use MSYS2 to build the app for Windows. I know it is a pretty niche environment to support, but it would be really great if we could make it happen.

Current Behavior

version information

pre-commit version: 2.7.1
sys.version:
    3.8.5 (default, Sep  7 2020, 18:26:56)  [GCC 10.2.0 64 bit (AMD64)]
sys.executable: c:/tools/msys64/home/dyeaw/gaphor/.venv/bin/python.exe
os.name: nt
sys.platform: win32

error information

An unexpected error has occurred: FileNotFoundError: [WinError 3] The system cannot find the path specified: '/home/dyeaw/gaphor'
Traceback (most recent call last):
  File "C:/tools/msys64/home/dyeaw/gaphor/.venv/lib/python3.8/site-packages/pre_commit/error_handler.py", line 63, in error_handler
    yield
  File "C:/tools/msys64/home/dyeaw/gaphor/.venv/lib/python3.8/site-packages/pre_commit/main.py", line 343, in main
    _adjust_args_and_chdir(args)
  File "C:/tools/msys64/home/dyeaw/gaphor/.venv/lib/python3.8/site-packages/pre_commit/main.py", line 163, in _adjust_args_and_chdir
    os.chdir(toplevel)
FileNotFoundError: [WinError 3] The system cannot find the path specified: '/home/dyeaw/gaphor'

Updates Needed

  • Update pre-commit to make use of the cygpath command to convert the path to the standard Windows path format after it gets the top-level path from git.
  • Remove cygwin warnings about incompatible Python+git versions if in the MSYS2 environment
  • Make upstream updates to setuptools_scm to patch additional path conversions for similar things as 1 above.

My plan, as long is everyone is good with adding support for this environment, is to submit a PR for the first two, and then I'll start to work with the setuptools_scm team to see if I can fix the third one as well.

@asottile
Copy link
Member

if you're using both msys git and msys python this works as intended -- there's supposed to be a warning for this but it's possible that it crashes too early to show that warning now

def check_for_cygwin_mismatch() -> None:
"""See https://github.com/pre-commit/pre-commit/issues/354"""
if sys.platform in ('cygwin', 'win32'): # pragma: no cover (windows)
is_cygwin_python = sys.platform == 'cygwin'
toplevel = cmd_output('git', 'rev-parse', '--show-toplevel')[1]
is_cygwin_git = toplevel.startswith('/')
if is_cygwin_python ^ is_cygwin_git:
exe_type = {True: '(cygwin)', False: '(windows)'}
logger.warn(
f'pre-commit has detected a mix of cygwin python / git\n'
f'This combination is not supported, it is likely you will '
f'receive an error later in the program.\n'
f'Make sure to use cygwin git+python while using cygwin\n'
f'These can be installed through the cygwin installer.\n'
f' - python {exe_type[is_cygwin_python]}\n'
f' - git {exe_type[is_cygwin_git]}\n',
)

@danyeaw
Copy link
Author

danyeaw commented Sep 20, 2020

if you're using both msys git and msys python this works as intended -- there's supposed to be a warning for this but it's possible that it crashes too early to show that warning now

Yes, I am using msys2 Python and msys2 git in a virtualenv.

@asottile
Copy link
Member

if sys.platform is win32 this is the windows-like python and not the msys-like python so this is a mismatch. this combination is unsupported

@danyeaw
Copy link
Author

danyeaw commented Sep 20, 2020

@asottile No, msys2 Python returns win32. Look at my version information above. It is Python compiled with GCC (mingw64) and sys.platform is win32. This is what is expected in msys2.

I did say it is a niche platform, it is definitely a strange one 😄

@asottile
Copy link
Member

no I'm very familiar with what you're saying, the problem is that win32-platformed pythons produce windows paths C:\etc\etc and posix-platformed gits produce posix paths /home/etc/etc and trying to combine the two is a nightmare -- thus the warning

you're best to use the posix-like pythons with posixlike git: https://github.com/asottile/scratch/wiki/platforms#msys2-msys-cpython-3x-64-bit vs https://github.com/asottile/scratch/wiki/platforms#msys2-mingw64-cpython-3x-64-bit

@danyeaw
Copy link
Author

danyeaw commented Sep 20, 2020

@asottile Ahh, OK, I see what you are saying. Thanks for further explaining!

MSYS2 compiled packages run on Windows using a POSIX emulation layer, in this case that would be the msys/python package. I am developing for Windows directly using the Win32 API with MINGW, and I'm using the mingw-w64-x86_64-python package. My program doesn't depend on being in POSIX, so using an emulation layer isn't desired. Sorry, I should have been more clear about my use case in the initial issue description.

This is the supported way to run GTK applications in Windows. The maintainer the PyGObject bindings has packaged Quod Libet the same way.

I understand though if you don't want to support this use case. 👍 I'll either not use pre-commit when developing in Windows or patch it downstream.

@danyeaw
Copy link
Author

danyeaw commented Sep 20, 2020

If anyone stumbles on this issue in the future and you want to run MINGW Python:

  1. Remove msys git using pacman -R git
  2. Install git-for-windows, if you use chocolately you can run choco install git
  3. Add git to your path:
echo 'export PATH=$PATH:/c/ProgramData/chocolatey/bin' >> ~/.bashrc
source ~/.bashrc

Git-for-windows is compiled using MINGW64 as well, and returns the correct Windows paths. Thanks @asottile for the guidance, I don't think this is well known for MSYS2 users, I'll see if I can't add it to their FAQ or other documentation. Sorry for being hard headed 😄.

@asottile
Copy link
Member

no problem! thanks for the helpful comment as well :)

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

Successfully merging a pull request may close this issue.

2 participants