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

Fixup getting project path on Windows #1003

Merged
merged 3 commits into from
Jul 31, 2024

Conversation

md11235
Copy link

@md11235 md11235 commented Jul 29, 2024

Sometimes ‘git rev-parse —show-toplevel’ is used in core/utils.py to get project path for an existing source file. That git subcommand would convert a Windows style path like “f:/foo/bar.py” to “/f/foo” and cause unexpected behavior on Windows OS’s.

I’ve made a possible fix to that git behavior. Thank you for reviewing this patch.

@manateelazycat
Copy link
Owner

@md11235
Thanks for patch, can you tell me what's value of path_from_git in your system? And what's your target string for windows_path?

I think it's dangerous if we direct access 1 or 2 index in path_parts, will maybe crash lsp-bridge.

@md11235
Copy link
Author

md11235 commented Jul 30, 2024

Thank you for you reply. You're correct about this.

Previously I only tested the git command provided MSYS2 (https://www.msys2.org/). And I've instead tested the git command provided by "Git for Windows" (https://gitforwindows.org/) just now and it behaved differently.

Suppose f:/foo is a local git repository. In MSYS2, git rev-parse --show-toplevel f:/foo/bar.py produces:

/f/foo

Yet in "Git For Windows", git rev-parse --show-toplevel f:/foo/bar.py produces:

F:/foo

So it's just the git command from MSYS2 when using the subcommand rev-parse has the problem of converting Windows-style path to a Unix-style one.

@md11235
Copy link
Author

md11235 commented Jul 30, 2024

Thank you for you reply. You're correct about this.

Previously I only tested the git command provided MSYS2 (https://www.msys2.org/). And I've instead tested the git command provided by "Git for Windows" (https://gitforwindows.org/) just now and it behaved differently.

Suppose f:/foo is a local git repository. In MSYS2, git rev-parse --show-toplevel f:/foo/bar.py produces:

/f/foo

Yet in "Git For Windows", git rev-parse --show-toplevel f:/foo/bar.py produces:

F:/foo

So it's just the git command from MSYS2 when using the subcommand rev-parse has the problem of converting Windows-style path to a Unix-style one.

Both these git commands were executed on Windows 10(Version 10.0.19045.4651).

@@ -362,7 +362,13 @@ def get_project_path(filepath):
import os
dir_path = os.path.dirname(filepath)
if get_command_result("git rev-parse --is-inside-work-tree", dir_path) == "true":
return get_command_result("git rev-parse --show-toplevel", dir_path)
path_from_git = get_command_result("git rev-parse --show-toplevel", dir_path)
if get_os_name() == "windows":
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should change to:

if get_os_name() == "windows" and path_from_git.startswith("/"):

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or only run convert code when env is MSYS2

@manateelazycat manateelazycat merged commit 37583f0 into manateelazycat:master Jul 31, 2024
1 check passed
@manateelazycat
Copy link
Owner

thanks for patch

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.

3 participants