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

Rewrite bundled/tool/ruff_server.py to typescript #479

Open
T-256 opened this issue May 23, 2024 · 4 comments
Open

Rewrite bundled/tool/ruff_server.py to typescript #479

T-256 opened this issue May 23, 2024 · 4 comments

Comments

@T-256
Copy link

T-256 commented May 23, 2024

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.

Originally posted by @T-256 in #443 (comment)

@T-256
Copy link
Author

T-256 commented May 26, 2024

IMO, then we can also drop ms-python.python dependency since we don't need Python interpreter anymore.

"Python interpreter missing:\r\n" +
"[Option 1] Select Python interpreter using the ms-python.python.\r\n" +
`[Option 2] Set an interpreter using "${serverId}.interpreter" setting.\r\n` +
"Please use Python 3.7 or greater.",

At there we could use ms-python.python as optional dep instead of mandatory dep.

@MichaReiser
Copy link
Member

I'm not sure if removing the ms-python.python dependency is desired in the long term, or at least, not without implementing our own python selector.

I suspect that we'll soon need to call into Python from Ruff to discover the module search paths and possibly more. But we could make ms-python.python an optional dependency.

@charliermarsh
Copy link
Member

Note that we have to call into Python if we want broad coverage for all system Pythons. Otherwise, there's no way to (completely) reliably get the path to the scripts directory.

@T-256
Copy link
Author

T-256 commented May 27, 2024

I'm not sure if removing the ms-python.python dependency is desired in the long term, or at least, not without implementing our own python selector.

I suspect that we'll soon need to call into Python from Ruff to discover the module search paths and possibly more. But we could make ms-python.python an optional dependency.

Exactly, in addition I think, at this point Ruff itself has well defined its config selector (based on Extension settings which then fallbacks to pyproject.toml/ruff.toml either on workspace dir or its parent dir). IIUC, in this extension, Python discovery is mainly used to pass target version of python to ruff. we could rely on project.required-python or tool.ruff.target-version instead.

Otherwise, there's no way to (completely) reliably get the path to the scripts directory.

IIUC, we use scripts directory to find if Ruff binary available locally into that. This is good. What I suggested is to drop ms-python.python as hard dependency. which means if user already installed ms-python.python we can still get scripts directory and when not installed, this extension will lookup on $PATH only.

dhruvmanila added a commit that referenced this issue Jul 4, 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

```json
{
  "ruff.nativeServer": true
}
```
Or
```json
{
  "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

```json
{
  "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

```json
{
  "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

```json
{
  "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
```
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

No branches or pull requests

4 participants