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

Environment variables are not expanded in "ruff.path" VSCode settings #448

Closed
erjac77 opened this issue Apr 13, 2024 · 3 comments · Fixed by #554
Closed

Environment variables are not expanded in "ruff.path" VSCode settings #448

erjac77 opened this issue Apr 13, 2024 · 3 comments · Fixed by #554
Labels
configuration Related to settings and configuration

Comments

@erjac77
Copy link

erjac77 commented Apr 13, 2024

Ruff: 0.3.7
VSCode: 1.88.0
OS: Ubuntu 22.04
Python: 3.11.8

If I specify a path to a custom ruff executable containing environment variables:

"ruff.path": [
  "${workspaceFolder}/dist/export/python/virtualenvs/ruff/${env:PYTHON_VERSION}/bin/ruff"
]

I get the following error message:

Interpreter executable (/workspace/dist/export/python/virtualenvs/ruff/${env:PYTHON_VERSION}/bin/ruff) not found

Seems like predefined variables are expanded, but not the environment variables.

Related to #413.

Thank you.

@dhruvmanila
Copy link
Member

I'm unsure where VS Code retrieves the environment variables from. Is it possible that the environment variable isn't defined in the environment where VS Code is launched? Could you try opening VS Code via the shell in which the environment variable is defined (like code .)?

@dhruvmanila dhruvmanila added the question Asking for support or clarification label Apr 24, 2024
@dhruvmanila
Copy link
Member

Ok, that actually doesn't work either.

@dhruvmanila
Copy link
Member

I looked into this a bit more and it seems that the extension only substitutes certain variables which I assume is just a side effect of inheriting from the Python extension template:

function resolveVariables(value: string[], workspace?: WorkspaceFolder): string[] {
const substitutions = new Map<string, string>();
const home = process.env.HOME || process.env.USERPROFILE;
if (home) {
substitutions.set("${userHome}", home);
}
if (workspace) {
substitutions.set("${workspaceFolder}", workspace.uri.fsPath);
}
substitutions.set("${cwd}", process.cwd());
getWorkspaceFolders().forEach((w) => {
substitutions.set("${workspaceFolder:" + w.name + "}", w.uri.fsPath);
});
return value.map((s) => {
for (const [key, value] of substitutions) {
s = s.replace(key, value);
}
return s;
});
}

@dhruvmanila dhruvmanila added configuration Related to settings and configuration and removed question Asking for support or clarification labels Jul 23, 2024
@dhruvmanila dhruvmanila linked a pull request Jul 24, 2024 that will close this issue
dhruvmanila added a commit that referenced this issue Jul 24, 2024
## Summary

This PR updates the `resolveVariables` to consider environment
variables.

Reference:
https://github.com/microsoft/vscode-black-formatter/blob/458ed8f03f03c6e021361b7dcf4a8c7ab7c3d5fb/src/common/settings.ts#L64-L71

fixes: #448 

## Test Plan

Note that you'll need to launch the VS Code from the shell so that the
VS Code process can see the variables which are present in an activate
virtual environment.

```console
$ echo $VIRTUAL_ENV
/Users/dhruv/playground/ruff/.venv

$ code --extensionDevelopmentPath=/Users/dhruv/work/astral/ruff-vscode .
```

Using the following settings:
```json
{
  "ruff.interpreter": ["${env:VIRTUAL_ENV}/bin/python"]
}
```

Logs:
```
2024-07-24 09:15:11.563 [info] Using interpreter: /Users/dhruv/playground/ruff/.venv/bin/python
2024-07-24 09:15:11.634 [info] Using the Ruff binary: /Users/dhruv/playground/ruff/.venv/bin/ruff
2024-07-24 09:15:11.640 [info] Resolved 'ruff.nativeServer: auto' to use the native server
2024-07-24 09:15:11.642 [info] Found Ruff 0.5.4 at /Users/dhruv/playground/ruff/.venv/bin/ruff
2024-07-24 09:15:11.642 [info] Server run command: /Users/dhruv/playground/ruff/.venv/bin/ruff server
2024-07-24 09:15:11.642 [info] Server: Start requested.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
configuration Related to settings and configuration
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants