Skip to content

Add configurable cwd setting to extension template#260

Merged
edvilme merged 3 commits intomainfrom
copilot/fix-hardcoded-working-directory
Feb 26, 2026
Merged

Add configurable cwd setting to extension template#260
edvilme merged 3 commits intomainfrom
copilot/fix-hardcoded-working-directory

Conversation

Copy link
Contributor

Copilot AI commented Feb 26, 2026

  • src/common/settings.ts: Added getCwd helper; replaced hardcoded cwd; added cwd to watched settings
  • package.json: Added <pytool-module>.cwd setting
  • bundled/tool/lsp_server.py: Added get_cwd(); updated _run_tool_on_document and _run_tool
  • Improved get_cwd docstring: removed misleading ${workspaceFolder} mention (it's pre-resolved by TypeScript, not Python)
  • Added inline comment at get_cwd call site explaining why document is passed
Original prompt

Problem

The extension template hardcodes the working directory (cwd) to the workspace root in src/common/settings.ts (line 65):

const workspaceSetting = {
    cwd: workspace.uri.fsPath,  // hardcoded

This means all extensions built from this template (isort, Black, Flake8, Mypy, Pylint, Autopep8, etc.) always run their underlying tool from the workspace root. Users with monorepo workspaces — where the Python project lives in a subdirectory (e.g., ${workspaceRoot}/python/) — cannot configure the working directory, so tools fail to pick up config files (pyproject.toml, isort.cfg, .flake8) located in subdirectories.

This was originally reported as microsoft/vscode-isort#141 and affects all extensions derived from this template.

Interestingly, the server-side Python code in downstream extensions like vscode-isort already has a get_cwd() function in bundled/tool/lsp_server.py that correctly resolves ${workspaceFolder} and ${fileDirname} tokens — but the client-side TypeScript never sends a user-configured value; it always sends the hardcoded workspace root path.

Required Changes

1. src/common/settings.ts

  • Add a getCwd helper function that reads the cwd value from the VS Code configuration and resolves variable substitutions like ${workspaceFolder} and ${fileDirname}:
    function getCwd(config: WorkspaceConfiguration, workspace: WorkspaceFolder): string {
        const cwd = config.get<string>('cwd', '${workspaceFolder}');
        return resolveVariables([cwd], workspace)[0];
    }
  • In getWorkspaceSettings, replace the hardcoded cwd: workspace.uri.fsPath with a call to getCwd(config, workspace).
  • In checkIfConfigurationChanged, add ${namespace}.cwd to the list of watched settings so the server restarts when the user changes it.

2. package.json

  • Add a <TOOL>.cwd setting under contributes.configuration.properties. The setting should:
    • Have type string
    • Default to "${workspaceFolder}"
    • Include a description explaining that it sets the working directory for the tool, and that ${workspaceFolder} and ${fileDirname} are supported tokens
    • Use the same pattern as existing settings in the template (using the <TOOL> placeholder)

3. bundled/tool/lsp_server.py

  • Ensure the get_cwd() function in the Python LSP server handles the ${workspaceFolder}, ${fileDirname}, and arbitrary path cases (this function already exists in downstream extensions like vscode-isort and should be present or added in the template).

Related Issue

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix hardcoded working directory in extension template Add configurable cwd setting to extension template Feb 26, 2026
Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
@edvilme edvilme added bug Issue identified by VS Code Team member as probable bug feature-request Request for new features or functionality labels Feb 26, 2026
@edvilme
Copy link
Contributor

edvilme commented Feb 26, 2026

Fixes microsoft/vscode-isort#141

@edvilme edvilme marked this pull request as ready for review February 26, 2026 22:44
@vs-code-engineering vs-code-engineering bot added this to the February 2026 milestone Feb 26, 2026
@edvilme edvilme enabled auto-merge (squash) February 26, 2026 22:47
@edvilme edvilme merged commit 49de257 into main Feb 26, 2026
2 checks passed
@edvilme edvilme deleted the copilot/fix-hardcoded-working-directory branch February 26, 2026 22:48

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

github-actions bot pushed a commit to microsoft/vscode-isort that referenced this pull request Feb 27, 2026
…substitution

Syncs changes from microsoft/vscode-python-tools-extension-template#260 and #262.

- Add isort.cwd configuration property to package.json (default: ${workspaceFolder})
- Add getCwd() usage in settings.ts getWorkspaceSettings() and checkIfConfigurationChanged()
- Expand get_cwd() in lsp_server.py to support additional VS Code file-related variables:
  ${file}, ${fileBasename}, ${fileBasenameNoExtension}, ${fileExtname},
  ${fileDirnameBasename}, ${relativeFile}, ${relativeFileDirname}, ${fileWorkspaceFolder}
- Add test_get_cwd.py with 17 unit tests covering all variable substitutions

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
github-actions bot pushed a commit to microsoft/vscode-mypy that referenced this pull request Feb 27, 2026
Applies changes from:
- microsoft/vscode-python-tools-extension-template#262 (get_cwd variable substitution)
- microsoft/vscode-python-tools-extension-template#260 (cwd setting - lsp_server.py portion)

Updates get_cwd() in bundled/tool/lsp_server.py to support all VS Code
file-related variable substitutions: ${file}, ${fileBasename},
${fileBasenameNoExtension}, ${fileExtname}, ${fileDirname},
${fileDirnameBasename}, ${relativeFile}, ${relativeFileDirname},
${fileWorkspaceFolder}.

Replaces the previous equality-based checks with substring replacement,
allowing variables to be embedded inside longer paths (e.g.
${fileDirname}/subdir). Preserves the mypy-specific ${nearestConfig}
behaviour. Fixes the dmypy (no-document) path to correctly return a
plain cwd value rather than always falling back to workspaceFS.

Adds src/test/python_tests/test_get_cwd.py with unit tests covering
all variables, composite patterns, no-document fallbacks, and the
mypy-specific ${nearestConfig} option.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
github-actions bot pushed a commit to microsoft/vscode-flake8 that referenced this pull request Feb 27, 2026
Applies changes from upstream template PRs:
- microsoft/vscode-python-tools-extension-template#260 (cwd setting - already applied)
- microsoft/vscode-python-tools-extension-template#262 (get_cwd variable substitution)

Expands get_cwd() to resolve nine VS Code file-related variables:
${file}, ${fileBasename}, ${fileBasenameNoExtension}, ${fileExtname},
${fileDirname}, ${fileDirnameBasename}, ${relativeFile},
${relativeFileDirname}, ${fileWorkspaceFolder}.

Adds 17 unit tests covering every variable, composite patterns,
multi-variable strings, and no-document fallback behaviour.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
github-actions bot pushed a commit to microsoft/vscode-pylint that referenced this pull request Feb 27, 2026
Sync changes from upstream template PRs:
- microsoft/vscode-python-tools-extension-template#260 (Add configurable cwd setting)
- microsoft/vscode-python-tools-extension-template#262 (get_cwd() variable substitution)

Expands get_cwd() to support 9 VS Code file-related variable substitutions:
${file}, ${fileBasename}, ${fileBasenameNoExtension}, ${fileExtname},
${fileDirname}, ${fileDirnameBasename}, ${relativeFile},
${relativeFileDirname}, ${fileWorkspaceFolder}.

Also adds unit tests for get_cwd() helper.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
github-actions bot pushed a commit to microsoft/vscode-black-formatter that referenced this pull request Feb 27, 2026
…ution

- Add `${namespace}.cwd` to checkIfConfigurationChanged in settings.ts
  (from microsoft/vscode-python-tools-extension-template#260)
- Update get_cwd() in lsp_server.py to support full VS Code variable
  substitution (9 file-related variables) instead of exact-match only
  (from microsoft/vscode-python-tools-extension-template#262)
- Add unit tests for get_cwd() helper (test_get_cwd.py)
  (from microsoft/vscode-python-tools-extension-template#262)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
github-actions bot pushed a commit to microsoft/vscode-isort that referenced this pull request Feb 27, 2026
…le substitution

Syncs changes from:
- microsoft/vscode-python-tools-extension-template#260 (Add configurable cwd setting)
- microsoft/vscode-python-tools-extension-template#262 (get_cwd() variable substitution)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
edvilme added a commit to microsoft/vscode-isort that referenced this pull request Feb 27, 2026
#543)

* Sync with template: Add configurable cwd setting and expand variable substitution

Syncs changes from microsoft/vscode-python-tools-extension-template#260 and #262.

- Add isort.cwd configuration property to package.json (default: ${workspaceFolder})
- Add getCwd() usage in settings.ts getWorkspaceSettings() and checkIfConfigurationChanged()
- Expand get_cwd() in lsp_server.py to support additional VS Code file-related variables:
  ${file}, ${fileBasename}, ${fileBasenameNoExtension}, ${fileExtname},
  ${fileDirnameBasename}, ${relativeFile}, ${relativeFileDirname}, ${fileWorkspaceFolder}
- Add test_get_cwd.py with 17 unit tests covering all variable substitutions

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Simplify test_get_cwd.py to match vscode-mypy PR #444 style (#544)

* Initial plan

* Initial plan

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>

* Fix failing settings tests, clean up test_get_cwd.py, add cwd translations

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>

* Add tests for each cwd variable substitution in settings.unit.test.ts

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>

* Simplify test_get_cwd.py to match mypy PR #444 style; revert extra TS tests

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>

* Lint

---------

Co-authored-by: GitHub Copilot <copilot@github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
edvilme added a commit to microsoft/vscode-mypy that referenced this pull request Feb 27, 2026
* Sync get_cwd() variable substitution from template

Applies changes from:
- microsoft/vscode-python-tools-extension-template#262 (get_cwd variable substitution)
- microsoft/vscode-python-tools-extension-template#260 (cwd setting - lsp_server.py portion)

Updates get_cwd() in bundled/tool/lsp_server.py to support all VS Code
file-related variable substitutions: ${file}, ${fileBasename},
${fileBasenameNoExtension}, ${fileExtname}, ${fileDirname},
${fileDirnameBasename}, ${relativeFile}, ${relativeFileDirname},
${fileWorkspaceFolder}.

Replaces the previous equality-based checks with substring replacement,
allowing variables to be embedded inside longer paths (e.g.
${fileDirname}/subdir). Preserves the mypy-specific ${nearestConfig}
behaviour. Fixes the dmypy (no-document) path to correctly return a
plain cwd value rather than always falling back to workspaceFS.

Adds src/test/python_tests/test_get_cwd.py with unit tests covering
all variables, composite patterns, no-document fallbacks, and the
mypy-specific ${nearestConfig} option.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Fix _MockLS stub missing window_log_message and LogMessageParams in test_get_cwd.py (#445)

* Initial plan

* Fix _MockLS missing window_log_message and LogMessageParams kwargs in test_get_cwd.py

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>

* Fix linting issues in test_get_cwd.py (flake8 E302, black formatting)

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>

---------

Co-authored-by: GitHub Copilot <copilot@github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue identified by VS Code Team member as probable bug feature-request Request for new features or functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants