-
Notifications
You must be signed in to change notification settings - Fork 54
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
Experimental ruff server
now uses local ruff
binaries when available
#443
Merged
snowsignal
merged 8 commits into
pre-release
from
jane/pre-release/user-local-executable
Apr 11, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a17b202
Implement ruff executable search for the new experimental server
snowsignal 339000b
Check the user install scheme if sysconfig.get_path('scripts') is not…
snowsignal f72f03e
Rename script path constant
snowsignal edd105a
Describe the branches in createExperimentalServer
snowsignal 126602a
Bump minimum required version to v0.3.5
snowsignal 7dad2d1
Simplify clause checking for Ruff path
snowsignal 0d3eaa6
Explain why `ruff_server.py` uses a split main block
snowsignal c08ff5e
Bump Ruff version to v0.3.6
snowsignal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import os | ||
import shutil | ||
import site | ||
import subprocess | ||
import sys | ||
import sysconfig | ||
from pathlib import Path | ||
|
||
RUFF_EXE = "ruff.exe" if sys.platform == "win32" else "ruff" | ||
|
||
BUNDLE_DIR = Path(__file__).parent.parent | ||
|
||
|
||
def update_sys_path(path_to_add: str) -> None: | ||
"""Add given path to `sys.path`.""" | ||
if os.path.isdir(path_to_add): | ||
# The `site` module adds the directory at the end, if not yet present; we want | ||
# it to be at the beginning, so that it takes precedence over any other | ||
# installed versions. | ||
sys.path.insert(0, path_to_add) | ||
|
||
# Allow development versions of libraries to be imported. | ||
site.addsitedir(path_to_add) | ||
|
||
|
||
# This is separate from the 'main' entrypoint because we need | ||
# to update the system path _before_ importing `pacakging` | ||
if __name__ == "__main__": | ||
# Ensure that we can import bundled libraries like `packaging` | ||
update_sys_path(os.fspath(BUNDLE_DIR / "libs")) | ||
|
||
snowsignal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
from packaging.specifiers import SpecifierSet | ||
from packaging.version import Version | ||
|
||
# This is subject to change in the future | ||
RUFF_VERSION_REQUIREMENT = SpecifierSet(">=0.3.5") | ||
|
||
|
||
def executable_version(executable: str) -> Version: | ||
"""Return the version of the executable at the given path.""" | ||
output = subprocess.check_output([executable, "--version"]).decode().strip() | ||
version = output.replace("ruff ", "") | ||
return Version(version) | ||
|
||
|
||
def check_compatibility( | ||
executable: str, | ||
requirement: SpecifierSet, | ||
) -> None: | ||
"""Check the executable for compatibility against various version specifiers.""" | ||
version = executable_version(executable) | ||
if not requirement.contains(version, prereleases=True): | ||
message = f"Ruff {requirement} required, but found {version} at {executable}" | ||
raise RuntimeError(message) | ||
|
||
|
||
def find_ruff_bin(fallback: Path) -> Path: | ||
"""Return the ruff binary path.""" | ||
path = Path(sysconfig.get_path("scripts")) / RUFF_EXE | ||
if path.is_file(): | ||
return path | ||
snowsignal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if sys.version_info >= (3, 10): | ||
user_scheme = sysconfig.get_preferred_scheme("user") | ||
elif os.name == "nt": | ||
user_scheme = "nt_user" | ||
elif sys.platform == "darwin" and sys._framework: | ||
user_scheme = "osx_framework_user" | ||
else: | ||
user_scheme = "posix_user" | ||
|
||
path = Path(sysconfig.get_path("scripts", scheme=user_scheme)) / RUFF_EXE | ||
if path.is_file(): | ||
return path | ||
|
||
path = shutil.which("ruff") | ||
if path: | ||
return path | ||
|
||
return fallback | ||
|
||
|
||
if __name__ == "__main__": | ||
ruff = os.fsdecode( | ||
find_ruff_bin( | ||
Path(BUNDLE_DIR / "libs" / "bin" / RUFF_EXE), | ||
), | ||
) | ||
check_compatibility(ruff, RUFF_VERSION_REQUIREMENT) | ||
completed_process = subprocess.run([ruff, *sys.argv[1:]], check=False) | ||
sys.exit(completed_process.returncode) | ||
snowsignal marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ authors = [{ name = "Charlie Marsh", email = "[email protected]" }] | |
maintainers = [{ name = "Charlie Marsh", email = "[email protected]" }] | ||
requires-python = ">=3.7" | ||
license = "MIT" | ||
dependencies = ["packaging>=23.1", "ruff-lsp==0.0.53", "ruff==0.3.3"] | ||
dependencies = ["packaging>=23.1", "ruff-lsp==0.0.53", "ruff==0.3.6"] | ||
|
||
[project.optional-dependencies] | ||
dev = ["mypy==1.2.0", "python-lsp-jsonrpc==1.0.0"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IF we could convert this file into typescript, then extension wouldn't need Python installation/activation on target system at all.
Currently Ruff extension needs python interpreter to start the server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, our eventual plan is to move this logic to Typescript 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #479 to track it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be an awesome change.