shinyypig / matlab-in-vscode

A vscode extension for matlab.
MIT License
45 stars 4 forks source link

`runMatlabFile()` fails in multi root workspaces #28

Closed cincofe closed 9 months ago

cincofe commented 10 months ago

If the workspace contains an additional folder (on top of the main workspace folder), the relative path used as argument of the Matlab's run() instruction contains the name of the folder itself and execution fails.

Possibly related to this issue with vscode.workspace.asRelativePath?

Successful execution example

In the folder /path/to/sometoolbox, the sometoolbox workspace is defined by sometoolbox.code-workspace, which contains the following:

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {}
}

When the ⏩ button is pushed while somescript.m is the current document, the add-on issues the following Matlab command, which is correct:

run('somescript.m')

Failed execution example

If the /path/to/anothertoolbox folder is added to the same workspace, the resulting sometoolbox.code-workspace is:

{
    "folders": [
        {
            "path": "."
        },
        {
            "path": "../anothertoolbox"
        }
    ],
    "settings": {}
}

Now, running the current document produces the following Matlab command:

run('sometoolbox/somescript.m')

which is of course incorrect and produces the following error:

Error using cd
Unable to change current folder to '/path/to/sometoolbox/sometoolbox' (Name is nonexistent or not a folder).
Error in run (line 48)
    cd(fileDir); 

Tested with:

Name: Matlab in VSCode Id: shinyypig.matlab-in-vscode Description: Run Matlab code in VSCode Version: 0.4.20 Publisher: shinyypig VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=shinyypig.matlab-in-vscode

on VS Code:

Version: 1.84.1 Commit: 2b35e1e6d88f1ce073683991d1eff5284a32690f Date: 2023-11-06T12:37:31.808Z Electron: 25.9.2 ElectronBuildId: 24603566 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Darwin arm64 22.6.0

shinyypig commented 10 months ago

You need to ensure that the workspace path of matlab is the same as that of vscode.workspace, which is also the default setting.

Sorry, I just cannot understand the problem. Do you mean that you want to run a file that is outside the workspace?

cincofe commented 9 months ago

In /src/extension.ts line 222:

let relativePath = vscode.workspace.asRelativePath(filePath);

relativePath has the correct value most of the time, except when the VS workspace ha multiple roots, i.e. when you added a second folder using File > Add folder to workspace ....

The workaround is of course to remove the extra folder before running the code, but sometimes it comes handy to keep related files/folders open in the some workspace.