Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions bundled/tool/lsp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,23 @@ def on_shutdown(_params: Optional[Any] = None) -> None:
jsonrpc.shutdown_json_rpc()


def get_cwd(settings: dict, document: Optional[workspace.Document]) -> str:
"""Returns the working directory for running the tool.

Resolves ``${fileDirname}`` to the directory of the current document.
If no document is available, falls back to the workspace root.

Examples of supported patterns: ``${fileDirname}``, ``${fileDirname}/subdir``.
"""
cwd = settings.get("cwd", settings["workspaceFS"])
Copy link

Choose a reason for hiding this comment

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

There are a lot more patterns than this. See this for the full list:

https://code.visualstudio.com/docs/reference/variables-reference

if "${fileDirname}" in cwd:
if document and document.path:
cwd = cwd.replace("${fileDirname}", os.path.dirname(document.path))
else:
cwd = settings["workspaceFS"]
return cwd


def _get_global_defaults():
return {
"path": GLOBAL_SETTINGS.get("path", []),
Expand Down Expand Up @@ -393,7 +410,8 @@ def _run_tool_on_document(
settings = copy.deepcopy(_get_settings_by_document(document))

code_workspace = settings["workspaceFS"]
cwd = settings["cwd"]
# Pass document so get_cwd can resolve ${fileDirname} to this file's directory.
cwd = get_cwd(settings, document)

use_path = False
use_rpc = False
Expand Down Expand Up @@ -498,7 +516,7 @@ def _run_tool(extra_args: Sequence[str]) -> utils.RunResult:
settings = copy.deepcopy(_get_settings_by_document(None))

code_workspace = settings["workspaceFS"]
cwd = settings["workspaceFS"]
cwd = get_cwd(settings, None)

use_path = False
use_rpc = False
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
"contributes": {
"configuration": {
"properties": {
"<pytool-module>.cwd": {
"default": "${workspaceFolder}",
"description": "Sets the working directory for <pytool-module>. Supported variables: `${workspaceFolder}` (workspace root) and `${fileDirname}` (directory of the current file).",
"scope": "resource",
"type": "string"
},
"<pytool-module>.args": {
"default": [],
"description": "Arguments passed in. Each argument is a separate item in the array.",
Expand Down
8 changes: 7 additions & 1 deletion src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export function getInterpreterFromSetting(namespace: string, scope?: Configurati
return config.get<string[]>('interpreter');
}

function getCwd(config: WorkspaceConfiguration, workspace: WorkspaceFolder): string {
const cwd = config.get<string>('cwd', '${workspaceFolder}');
return resolveVariables([cwd], workspace)[0];
}

export async function getWorkspaceSettings(
namespace: string,
workspace: WorkspaceFolder,
Expand All @@ -62,7 +67,7 @@ export async function getWorkspaceSettings(
}

const workspaceSetting = {
cwd: workspace.uri.fsPath,
cwd: getCwd(config, workspace),
workspace: workspace.uri.toString(),
args: resolveVariables(config.get<string[]>(`args`) ?? [], workspace),
path: resolveVariables(config.get<string[]>(`path`) ?? [], workspace),
Expand Down Expand Up @@ -104,6 +109,7 @@ export async function getGlobalSettings(namespace: string, includeInterpreter?:
export function checkIfConfigurationChanged(e: ConfigurationChangeEvent, namespace: string): boolean {
const settings = [
`${namespace}.args`,
`${namespace}.cwd`,
`${namespace}.path`,
`${namespace}.interpreter`,
`${namespace}.importStrategy`,
Expand Down