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

Having relative paths in .code-workspace files for interpreter path setting doesn't work #11918

Closed
Halkcyon opened this issue May 20, 2020 · 18 comments
Labels
area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug

Comments

@Halkcyon
Copy link

Halkcyon commented May 20, 2020

Environment data

  • VS Code version: 1.45.1
  • Extension version (available under the Extensions sidebar): 2020.5.80290
  • OS and version: Windows 7 Enterprise
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.0
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: n/a
  • Relevant/affected Python-related VS Code extensions and their versions: n/a
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info How to update the language server to the latest stable version #3977): Language Server
  • Value of the python.languageServer setting: Microsoft

Expected behaviour

{
  "python.pythonPath": "..\\.venv\\Scripts\\python.exe",
}

I expect this to be able to use a parent virtual environment as the editor's environment. I also tried ${workspaceFolder}/../.venv. I am trying to use the multi-root workspace feature with a full-stack app scaffold I work with for maintainability in my editor experience.

Setting pythonPath in the .code-workspace also seems to have no effect.

Actual behaviour

I receive generic extension crash modals and a re-prompt to select my python environment. The crash was intermittent, but it always re-prompts for a python interpreter.

Steps to reproduce:

[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]

PowerShell MVP setup script:

#requires -Version 3
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'

$root = "${PWD}\repro-example"

$niParams = @{
    Path     = "$root\src\server\.vscode", "$root\src\client"
    ItemType = 'Directory'
    Force    = $true
}
New-Item @niParams > $null

@'
{
  "folders": [
    {"path": "."},
    {"path": ".\\src\\server"},
    {"path": ".\\src\\client"}
  ]
}
'@ | Out-File -FilePath "$root\vscode.code-workspace"

@'
{
  "python.pythonPath": "..\\.venv\\Scripts\\python.exe",
  "python.jediEnabled": false,
}
'@ | Out-File -FilePath "$root\src\server\.vscode\settings.json"

'' | Out-File -FilePath "$root\src\server\__init__.py" -NoNewline

py.exe -3 -m venv "$root\.venv"

code.cmd "$root\vscode.code-workspace"

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

Starting Microsoft Python language server.
> c:\Users\maximilian\repos\repro-example\src\.venv\Scripts\python.exe c:\Users\maximilian\.vscode\extensions\ms-python.python-2020.5.80290\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> c:\Users\maximilian\repos\repro-example\src\.venv\Scripts\python.exe c:\Users\maximilian\.vscode\extensions\ms-python.python-2020.5.80290\pythonFiles\pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
Diagnostic Code: InvalidPythonInterpreterDiagnostic, Message: No Python interpreter is selected. You need to select a Python interpreter to enable features such as IntelliSense, linting, and debugging.

@Halkcyon Halkcyon added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels May 20, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label May 20, 2020
@karrtikr
Copy link

I think you may have set the incorrect path to python. It should be

"python.pythonPath": "..\\..\\.venv\\Scripts\\python.exe",

Also, if you've a common interpreter for all workspace folders, you could just set the workspace setting instead of workspace folder setting. Run the command Open workspace settings and set python.pythonPath there.

@karrtikr karrtikr added the info-needed Issue requires more information from poster label May 21, 2020
@Halkcyon
Copy link
Author

@karrtikr Is pythonPath meant to be relative to .vscode/settings.json or to the working folder that ${workspaceFolder} would refer to? The logs indicate that it does find the python interpreter, but the extension is still reporting that an interpreter isn't selected.

Setting pythonPath in the workspace settings doesn't have any effect. Does it need any additional configuration or action? Looking at multi-root documentation, it looks like the only settings that can apply there are window-specific, not necessarily extension-specific.

But, I tried your suggestion of adding an extra parent reference ("..\..\") and it worked. It wasn't clear that that path was relative to the settings.json file from the documentation. It also didn't work when I used ${workspaceFolder}\\.. which is unintuitive.

@ghost ghost removed the triage label May 21, 2020
@karrtikr
Copy link

it looks like the only settings that can apply there are window-specific, not necessarily extension-specific.

Nope that's not true. You can use any type of settings.

Setting pythonPath in the workspace settings doesn't have any effect.

I think you maybe setting the wrong location to pythonPath. Can you try using the absolute path in workspace settings and see if it works?

@Halkcyon
Copy link
Author

Halkcyon commented May 21, 2020

Can you try using the absolute path in workspace settings and see if it works?

Okay, this works but defeats the purpose of having a shared workspace for my team. Using a relative path or using ${workspaceFolder} here doesn't work for the other roots.

Nope that's not true. You can use any type of settings.

https://code.visualstudio.com/docs/editor/multi-root-workspaces#_settings

"To avoid setting collisions, only resource (file, folder) settings are applied when using a multi-root workspace. Settings which affect the entire editor (for example, UI layout) are ignored."

This was my cause for confusion, but it looks like it's referring to the individual workspace settings.

@karrtikr
Copy link

Using a relative path or using ${workspaceFolder} here doesn't work for the other roots.

I am suspecting you may have entered the wrong relative interpreter path, what path did you enter?

The workspace settings are saved here $root\vscode.code-workspace, so maybe try entering the path relative to this file.

I realize docs may not be entirely clear. You can bring that up with https://github.com/microsoft/vscode-docs if you want to.

@Halkcyon
Copy link
Author

Halkcyon commented May 21, 2020

I am suspecting you may have entered the wrong relative interpreter path, what path did you enter?

The workspace settings are saved here $root\vscode.code-workspace, so maybe try entering the path relative to this file.

I've tried .\\.venv\\Scripts\\python.exe with and without the explicit CWD specifier (".\"), but I still get prompted to select an interpreter in the other roots.

It seems the extension is trying to resolve that path relative to the different roots which is the issue here.

@karrtikr
Copy link

karrtikr commented May 21, 2020

Can you try using ..\\.venv\\Scripts\\python.exe?
From my understanding, . resolves to ${workspaceFolder} in case when you Open a folder, in case of multiroot workspaces, it resolves to path to .code workspace file.

@karrtikr
Copy link

It also didn't work when I used ${workspaceFolder}\.. which is unintuitive.

It should be ${workspaceFolder}\\..\\.. because ${workspaceFolder} is $root\src\server and interpreter location is $root\.venv.

@Halkcyon
Copy link
Author

It also didn't work when I used ${workspaceFolder}.. which is unintuitive.

It should be ${workspaceFolder}\\..\\.. because ${workspaceFolder} is $root\src\server and interpreter location is $root\.venv.

The unintuitive part to me is that ${workspaceFolder} resolves not to where .code-workspace is, but where the other root is (wherever that may be) when used inside the settings property of the code workspace.

@karrtikr
Copy link

The unintuitive part to me is that ${workspaceFolder} resolves not to where .code-workspace is

Why would you expect that? ${workspaceFolder} refers the workspace folder, not the workspace (the .code-workspace file).

Remember workspace folder & workspace are separate things.

@Halkcyon
Copy link
Author

Remember workspace folder & workspace are separate things.

See, that is confusing right there. Is it possible for pythonPath to resolve respective to .code-workspace for a multi-root project?

@karrtikr
Copy link

See, that is confusing right there.

I see. Maybe I can help, what exactly is confusing there? Workspace is the .code-workspace file, and you can add workspace folders to that workspace. For instance, $root\src\server is the workspace folder in your case.

Is it possible for pythonPath to resolve respective to .code-workspace for a multi-root project?

If there's a . in pythonPath mentioned inside .code-workspace file settings section, it would resolve respective to .code-workspace. Quoting my understanding,

. resolves to ${workspaceFolder} in case when you Open a folder, in case of multiroot workspaces, it resolves to path to .code workspace file.

@Halkcyon
Copy link
Author

Halkcyon commented May 22, 2020

If there's a . in pythonPath mentioned inside .code-workspace file settings section, it would resolve respective to .code-workspace. Quoting my understanding,

. resolves to ${workspaceFolder} in case when you Open a folder, in case of multiroot workspaces, it resolves to path to .code workspace file.

Okay, here's where our miscommunication stems. . does not resolve to the folder that .code-workspace is in, but the folder of wherever another root is. So my server folder above is actually resolving it to $root\src\server\.vscode\ instead of $root\ when I am editing __init__.py.

@karrtikr
Copy link

Okay, here's where our miscommunication stems. . does not resolve to the folder that .code-workspace is in, but the folder of wherever another root is.

My bad, you're right. I just tested it out.

If there's a . in pythonPath mentioned inside .code-workspace file settings section, each workspace folder resolves it differently. Short answer, having relative paths in .code-workspace doesn't work.

For your server folder, it should resolve to $root\src\server, not $root\src\server\.vscode\. Please double check.

@karrtikr karrtikr reopened this May 22, 2020
@ghost ghost added the triage-needed Needs assignment to the proper sub-team label May 22, 2020
@karrtikr karrtikr added needs PR and removed info-needed Issue requires more information from poster labels May 22, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label May 22, 2020
@karrtikr karrtikr added area-environments Features relating to handling interpreter environments triage-needed Needs assignment to the proper sub-team labels May 22, 2020
@karrtikr karrtikr removed their assignment May 22, 2020
@karrtikr
Copy link

The correct solution should be to resolve relative paths in .code-workspace files against the location of the workspace file.

@karrtikr karrtikr changed the title Using a parent-relative ("..") path for the interpeter fails to find exe Having relative paths in .code-workspace files for interpreter path setting doesn't work May 22, 2020
@Halkcyon
Copy link
Author

For your server folder, it should resolve to $root\src\server, not $root\src\server.vscode. Please double check.

Yep, you're right. I was missing a layer mentally.

The correct solution should be to resolve relative paths in .code-workspace files against the location of the workspace file.

Yes, thanks.

@karrtikr
Copy link

karrtikr commented May 23, 2020

Feature request from VSCode: microsoft/vscode#98424

See, that is confusing right there.

Another upstream issue you may relate to microsoft/vscode#77718

Please upvote these if you would like that.

@karthiknadig karthiknadig removed the triage-needed Needs assignment to the proper sub-team label May 26, 2020
@karrtikr
Copy link

karrtikr commented Jun 29, 2020

Closing this issue as pythonPath setting is no longer shareable with this. #12313

@ghost ghost removed the needs PR label Jun 29, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

3 participants