Skip to content

[WIP] Enhance get cwd function to resolve additional file-related variables#261

Closed
Copilot wants to merge 1 commit intomainfrom
copilot/expand-get-cwd-functionality
Closed

[WIP] Enhance get cwd function to resolve additional file-related variables#261
Copilot wants to merge 1 commit intomainfrom
copilot/expand-get-cwd-functionality

Conversation

Copy link
Contributor

Copilot AI commented Feb 26, 2026

Thanks for asking me to work on this. I will get started on it and keep this PR's description up to date as I form a plan and make progress.

Original prompt

Context

PR #260 added a configurable cwd setting with variable substitution. The TypeScript client (src/common/settings.ts resolveVariables()) pre-resolves ${workspaceFolder}, ${userHome}, ${cwd}, and ${workspaceFolder:name} before sending settings to the Python server. The Python server's get_cwd() in bundled/tool/lsp_server.py then handles document-dependent variables that can't be resolved at settings-load time.

However, as @rchiodo pointed out in #260 (comment), the current get_cwd() only resolves ${fileDirname}. There are more file-related variables from VS Code's variables reference (https://code.visualstudio.com/docs/reference/variables-reference) that should also be resolved server-side since they depend on the current document.

Required Change

Only modify bundled/tool/lsp_server.py — expand the get_cwd() function (currently at line 292) to resolve additional file-related VS Code variables using a substitution-map approach. The current implementation is:

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"])
    if "${fileDirname}" in cwd:
        if document and document.path:
            cwd = cwd.replace("${fileDirname}", os.path.dirname(document.path))
        else:
            cwd = settings["workspaceFS"]
    return cwd

Replace it with a version that builds a substitution map of all document-dependent variables and applies them. When a document is available and has a path, resolve:

  • ${file} → absolute path of the document (document.path)
  • ${fileBasename} → file name with extension (os.path.basename(document.path))
  • ${fileBasenameNoExtension} → file name without extension (use os.path.splitext on the basename)
  • ${fileExtname} → file extension including the dot (use os.path.splitext)
  • ${fileDirname} → directory of the document (os.path.dirname(document.path)) — already supported
  • ${fileDirnameBasename} → just the name of the containing directory (os.path.basename(os.path.dirname(document.path)))
  • ${relativeFile} → document path relative to settings["workspaceFS"] (use os.path.relpath)
  • ${relativeFileDirname} → directory of the document relative to settings["workspaceFS"]
  • ${fileWorkspaceFolder}settings["workspaceFS"]

If no document is available (or document.path is falsy), and the cwd string contains any ${file...} or ${relativeFile...} pattern, fall back to settings["workspaceFS"].

Update the docstring to list all supported variables.

Do NOT modify settings.ts, package.json, or any other file — only bundled/tool/lsp_server.py.

This pull request was created from Copilot chat.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

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

Successfully merging this pull request may close these issues.

2 participants