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

Keep Python script limited to sysconfig usage #511

Merged
merged 2 commits into from
Jul 4, 2024
Merged

Conversation

dhruvmanila
Copy link
Member

@dhruvmanila dhruvmanila commented Jul 3, 2024

Summary

This PR makes progress towards #476 and #479 by making the following changes:

  • Update the ruff_server.py script to limit itself to just getting the Ruff binary path using sysconfig
  • Use the which dependency and move the logic of locating the Ruff binary from PATH to TypeScript
  • Add log messages similar to ruff-lsp

Test Plan

1

{
  "ruff.nativeServer": true
}

Or

{
  "ruff.nativeServer": true,
  "ruff.importStrategy": "fromEnvironment"
}

Logs:

When the ruff binary is installed in the current virtual environment:

2024-07-03 15:28:35.674 [info] Using the Ruff binary: /Users/dhruv/work/astral/ruff-vscode/.venv/bin/ruff
2024-07-03 15:28:35.675 [info] Server run command: /Users/dhruv/work/astral/ruff-vscode/.venv/bin/ruff server --preview

If there's no ruff binary in the current virtual environment:

2024-07-03 15:25:19.605 [info] Using the Ruff binary: /Users/dhruv/.local/bin/ruff
2024-07-03 15:25:19.606 [info] Server run command: /Users/dhruv/.local/bin/ruff server --preview

If the ruff binary is only available via PATH:

2024-07-03 15:32:12.172 [info] Using environment executable: /opt/homebrew/bin/ruff
2024-07-03 15:32:12.173 [info] Server run command: /opt/homebrew/bin/ruff server --preview

And, if there's no ruff binary anywhere on the system, use the bundled binary:

2024-07-03 15:29:47.948 [info] Falling back to bundled executable: /Users/dhruv/work/astral/ruff-vscode/bundled/libs/bin/ruff
2024-07-03 15:29:47.948 [info] Server run command: /Users/dhruv/work/astral/ruff-vscode/bundled/libs/bin/ruff server --preview

2

{
  "ruff.nativeServer": true,
  "ruff.importStrategy": "useBundled"
}

Logs:

2024-07-03 15:26:28.767 [info] Using bundled executable: /Users/dhruv/work/astral/ruff-vscode/bundled/libs/bin/ruff
2024-07-03 15:26:28.767 [info] Server run command: /Users/dhruv/work/astral/ruff-vscode/bundled/libs/bin/ruff server --preview

3

{
  "ruff.nativeServer": true,
  "ruff.path": ["/Users/dhruv/work/astral/ruff/target/debug/ruff"]
}

Logs:

2024-07-03 15:27:42.342 [info] Using 'path' setting: /Users/dhruv/work/astral/ruff/target/debug/ruff
2024-07-03 15:27:42.342 [info] Server run command: /Users/dhruv/work/astral/ruff/target/debug/ruff server --preview

4

{
  "ruff.nativeServer": true,
  "ruff.interpreter": [
    "/Users/dhruv/work/astral/ruff-lsp/.venv/bin/python"
  ]
}

Logs:

2024-07-03 15:35:23.578 [info] Using the Ruff binary: /Users/dhruv/work/astral/ruff-lsp/.venv/bin/ruff
2024-07-03 15:35:23.578 [info] Server run command: /Users/dhruv/work/astral/ruff-lsp/.venv/bin/ruff server --preview

5

What if the script failed? I modified the script path in TypeScript code and we get the following logs:

2024-07-03 15:42:41.035 [error] Error while trying to find the Ruff binary: Error: Command failed: /Users/dhruv/work/astral/ruff-lsp/.venv/bin/python /Users/dhruv/work/astral/ruff-vscode/bundled/tool/find_ruff_binary_pat.py
/Users/dhruv/work/astral/ruff-lsp/.venv/bin/python: can't open file '/Users/dhruv/work/astral/ruff-vscode/bundled/tool/find_ruff_binary_pat.py': [Errno 2] No such file or directory

2024-07-03 15:42:41.037 [info] Using environment executable: /Users/dhruv/.local/bin/ruff
2024-07-03 15:42:41.040 [info] Server run command: /Users/dhruv/.local/bin/ruff server --preview

Copy link

@T-256 T-256 left a comment

Choose a reason for hiding this comment

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

LGTM, Thank you!

src/common/constants.ts Outdated Show resolved Hide resolved
src/common/server.ts Show resolved Hide resolved
@dhruvmanila dhruvmanila force-pushed the dhruv/reduce-python branch 2 times, most recently from 9173f37 to 0043fcc Compare July 3, 2024 10:10
Comment on lines 71 to 78
try {
const stdout = await executeCommand(
`${settings.interpreter[0]} ${FIND_RUFF_BINARY_SCRIPT_PATH}`,
);
ruffBinaryPath = stdout.trim();
} catch (err) {
traceError(`Error while trying to find the Ruff binary: ${err}`);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm a bit torn on whether we should notify the user or not. The script itself doesn't raise any error so the failure should come from any external means. If it does, we'll log the failure and continue ahead by checking the PATH variable and falling back to using the bundled executable.

Copy link
Member

Choose a reason for hiding this comment

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

It seems fine to notify if the error seems rare and indicative of an unexpected situation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I think that makes sense. I've added a "Show Logs" action button which opens the Ruff logs directly.

Screenshot 2024-07-04 at 09 15 58

@dhruvmanila dhruvmanila marked this pull request as ready for review July 3, 2024 10:16
traceError(`Error while trying to find the Ruff binary: ${err}`);
}

if (ruffBinaryPath && ruffBinaryPath.length > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (ruffBinaryPath && ruffBinaryPath.length > 0) {
if (ruffBinaryPath?.length > 0) {

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, it's giving me:

typescript: 'ruffBinaryPath.length' is possibly 'undefined'. [18048]

I'm going to leave it as is for now

Copy link
Member

Choose a reason for hiding this comment

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

This is probably a dumb question and I think I saw a related PR today.
Are we now checking the ruff version in the typescript code for both the native and legacy LSP?

Copy link
Member Author

Choose a reason for hiding this comment

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

We check whether the Ruff executable we found supports the native server (ruff server) by looking at the version. This is removed here from the Python script and added in TypeScript in #512. I'm not sure what you mean by "version check in legacy LSP"?

Copy link
Member

@charliermarsh charliermarsh left a comment

Choose a reason for hiding this comment

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

Where / when do we validate the Ruff version?

Base automatically changed from dhruv/bundled to main July 4, 2024 03:05
@dhruvmanila
Copy link
Member Author

Where / when do we validate the Ruff version?

I'm assuming you mean by the validation for the native server which is in #512 in TypeScript code.

@dhruvmanila dhruvmanila merged commit fbb0467 into main Jul 4, 2024
6 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/reduce-python branch July 4, 2024 03:49
dhruvmanila added a commit that referenced this pull request Jul 4, 2024
## Summary

This PR adds back the message to suggest the user that the current Ruff
binary doesn't support the native server.

This was removed from the Python script in #511.

fixes: #476 

## Test Plan

Install an older Ruff version to test this out using `pipx install
--force "ruff==0.3.3"`.

<img width="1432" alt="Screenshot 2024-07-03 at 14 50 41"
src="https://github.com/astral-sh/ruff-vscode/assets/67177269/267e2bab-e050-4cdf-98ff-fb140b7d63ee">

And, we've another log message which displays the Ruff version similar
to `ruff-lsp`:
```
2024-07-03 16:00:03.089 [info] Using the Ruff binary: /Users/dhruv/work/astral/ruff-lsp/.venv/bin/ruff
2024-07-03 16:00:03.094 [info] Found Ruff 0.4.10 at /Users/dhruv/work/astral/ruff-lsp/.venv/bin/ruff
2024-07-03 16:00:03.095 [info] Server run command: /Users/dhruv/work/astral/ruff-lsp/.venv/bin/ruff server --preview
```
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.

None yet

5 participants