Skip to content

Bug: Symlinks in Testing #22658

@eleanorjboyd

Description

@eleanorjboyd

Please see the first comment on this issue for the proposed solution.

Description of Issue:

In investigating the symlink issue affecting Python testing in VS Code, I uncovered several contributing factors. The problem begins during the test discovery phase: the Python extension initiates a subprocess which uses the symlink as its current working directory (since the symlink or at least a path relative to the symlink workspace is set as the cwd). Then when node calls the python subprocess which run pytest discovery, Node resolves the symlink cwd to target path. This means the subprocess which pytest run on is on the target path of the symlinked workspace. Consequently, when the PyTest extension is activated, it encounters already-resolved symlinks. This leads to the collection of test data with paths relative to the symlink's target value, not its relative location. These paths are then utilized to populate the Test Explorer in VS Code. As a result, when a user attempts to open a file from the Test Explorer, VS Code erroneously navigates to the target path which is incorrect for the workspace since it is workspace opened at the location of a symlink. Also each test then has its path set as the target path to its file, instead of the symlink, meaning the test is no longer associated with the given workspace it is in.

Given these findings, a design decision is needed to address and rectify the symlink issue in the context of testing within VS Code. This decision will determine the most effective solution or workaround to ensure accurate and efficient test handling.

I will explore how other symlink items are handled in vscode but some ideas include:

  • passing the symlink into the subprocess and having the plugin handle returning data relative to the symlink
  • editing resulting data on the extension side to substitute symlinks in for the target link if present.

Test Case Design:

Below are the steps to make this problem occur for testing and as an example:

  • in terminal cd to parent folder parent
  • create folder inside parent target_folder with tests such as test_example.py (a file with a test inside)
  • from terminal create a symlink ln -s target_folder symlink_to_target_folder
  • open vscode with the folder symlink_to_target_folder can be done with code symlink_to_target_folder in terminal
  • run test discovery, test UI will be populated
  • 🐛 the root folder will be target_folder instead of symlink_to_target_folder in the test explorer view
  • 🐛 click on "go to file" on a test, the incorrect file will be opened
  • 🐛 try to run tests, nothing will happen

Notable Code:

  1. const proc = spawn(file, args, spawnOptions); when spawnOptions in rawProcessApis.ts has a value cwd. On this spawn call, the symlink is resolved.

  2. here run requests are evaluated and tests included in the request and checked against known workspace folder to gather correct workspaces. This does not work correctly for symlinks. Code in controller.ts

if (request.include) {
            uniq(request.include.map((r) => this.workspaceService.getWorkspaceFolder(r.uri))).forEach((w) => {
                if (w) {
                    workspaces.push(w);
                }
            });
  1. fs.lstatSync(workspaceFolderPath).isSymbolicLink(); a way to check if a given workspace folder is a symlink, possible addition to the code.

Metadata

Metadata

Assignees

Labels

area-testingbugIssue identified by VS Code Team member as probable bugneeds proposalNeed to make some design decisionson-testplanAdded to test plan

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions