diff --git a/bundled/tool/lsp_server.py b/bundled/tool/lsp_server.py index d3c630a..1af2a06 100644 --- a/bundled/tool/lsp_server.py +++ b/bundled/tool/lsp_server.py @@ -368,6 +368,19 @@ def _get_settings_by_document(document: workspace.Document | None): # ***************************************************** # Internal execution APIs. # ***************************************************** +def get_cwd(settings: Dict[str, Any], document: Optional[workspace.Document]) -> str: + """Returns cwd for the given settings and document.""" + if settings["cwd"] == "${workspaceFolder}": + return settings["workspaceFS"] + + if settings["cwd"] == "${fileDirname}": + if document is not None: + return os.fspath(pathlib.Path(document.path).parent) + return settings["workspaceFS"] + + return settings["cwd"] + + # pylint: disable=too-many-branches def _run_tool_on_document( document: workspace.Document, @@ -393,7 +406,7 @@ def _run_tool_on_document( settings = copy.deepcopy(_get_settings_by_document(document)) code_workspace = settings["workspaceFS"] - cwd = settings["cwd"] + cwd = get_cwd(settings, document) use_path = False use_rpc = False @@ -476,7 +489,7 @@ def _run_tool_on_document( def _run_tool(extra_args: Sequence[str], settings: Dict[str, Any]) -> utils.RunResult: """Runs tool.""" code_workspace = settings["workspaceFS"] - cwd = settings["cwd"] + cwd = get_cwd(settings, None) use_path = False use_rpc = False diff --git a/src/common/server.ts b/src/common/server.ts index affb971..0204b1e 100644 --- a/src/common/server.ts +++ b/src/common/server.ts @@ -2,7 +2,7 @@ // Licensed under the MIT License. import * as fsapi from 'fs-extra'; -import { Disposable, env, l10n, LanguageStatusSeverity, LogOutputChannel, WorkspaceFolder } from 'vscode'; +import { Disposable, env, l10n, LanguageStatusSeverity, LogOutputChannel, Uri, WorkspaceFolder } from 'vscode'; import { State } from 'vscode-languageclient'; import { LanguageClient, @@ -28,7 +28,7 @@ async function createServer( initializationOptions: IInitOptions, ): Promise { const command = settings.interpreter[0]; - const cwd = settings.cwd; + const cwd = settings.cwd === '${fileDirname}' ? Uri.parse(settings.workspace).fsPath : settings.cwd; // Set debugger path needed for debugging python code. const newEnv = { ...process.env };