mathworks / MATLAB-extension-for-vscode

This extension provides support for editing MATLAB® code in Visual Studio® Code and includes features such as syntax highlighting, code analysis, navigation support, and more.
https://marketplace.visualstudio.com/items?itemName=MathWorks.language-matlab
MIT License
234 stars 15 forks source link

Start MATLAB from the current workspace directory instead of the previous working directory #98

Open goldrik opened 7 months ago

goldrik commented 7 months ago
  1. Start MATLAB from VS Code
  2. Change current directory to the current one in the workspace (fol1)
  3. Close VS Code, and open again from a different directory (fol2)
  4. Start MATLAB from VS Code, again
  5. Type pwd Every time, I find that VS Code is still in the previous directory (fol1) instead of the current one (fol2).

While I believe this is consistent with MATLAB's desktop app behavior (where it just opens up the previous working directory), I don't think this makes sense for VS Code. Your workspace defines exactly which folders and files you want to use. There is no reason for MATLAB to start from a totally different directory, in this case. Another major consequence of this is that intellisense doesnt work properly until you change the current directory.

The current workaround is to change the current directory every time you open a new workspace for MATLAB. Kind of inconvenient.

Personally, I would want all of the workspace folders (parent folders, not necessarily children) to be added to the path right away. Again, anything already open in the workspace is there for a reason, I think it just makes sense to add them to the path to begin with.

Note - I do want to acknowledge how great the new update has been, it's been a legitimate game changer. There's still some quirks for sure, but Im sure they will be ironed out in time.

dklilley commented 7 months ago

Thanks for raising this suggestion! I have also thought about this.

One question: The question seems to be focused around MATLAB's initial working directory. However, when there are multiple VS Code workspace folders, there is no clear initial working directory. When there is only one workspace folder, do you want the initial working directory to be changed? When there are multiple workspace folders, what is your expectation for MATLAB's initial working directory?

goldrik commented 7 months ago

In general, I think starting from the top-most workspace folder makes the most sense.

I did realize though that there could be an issue if the extension chooses a folder different from what the user wants. In that case, instead of changing directory once and not worrying about it, the user would have to change the directory every time they open the same project on VS Code. So I do see the merits of always using the previous working directory. Perhaps if the "previous working directory" is found in the workspace, MATLAB could just keep that (even if it is not top-most).

I also know people have mentioned wanting the ability for MATLAB to run a script every time it starts up. If this is implemented (and can be project-specific), that could cover all use cases (i.e. automatically cd-ing and adding all relevant directories on startup).

briochemc commented 7 months ago

I also know people have mentioned wanting the ability for MATLAB to run a script every time it starts up. If this is implemented (and can be project-specific), that could cover all use cases (i.e. automatically cd-ing and adding all relevant directories on startup).

I am exactly in this case. For each workspace, I have a custom startup.m file at its root (the top-most workspace folder), which adds all the paths I need in the worskpace but also runs some custom stuff. It'd be great if MATLAB was started from the top-most workspace folder by default.


EDIT in the interest of clarity (hopefully):

  1. if I open up VSCode from a command line interface (e.g., bash) with:

    code path/to/my/worskapce
  2. and then run a MATLAB script from within VSCode:

    Screenshot 2024-03-07 at 9 48 50 am
  3. then I'd want MATLAB to start at path/to/my/worskapce (whch contains a custom startup.m file), i.e., be the command line equivalent to:

    cd path/to/my/worskapce
    matlab
J-Sorenson commented 6 months ago

Now that we have the ability to run MATLAB code within VS Code, does that mean we can run a script that changes the directory and adds the necessary paths? We could use mfilename('fullpath') within the script to configure the folder and paths properly. Or is the path not retained because MATLAB is using multiple processes such that only one process is affected by the script?

goldrik commented 6 months ago

@J-Sorenson Yes, I think that works. You could open VS Code, run that script, and those paths would be there for the remainder of the session (until you disconnect from MATLAB). The next time VS Code connects to MATLAB, it'll be in the same directory as before, but you'll have to re-run the script to add the paths again.

The only thing is that MATLAB does not necessarily start from the directory you have open in VS Code. You might have to manually "change directory" to the folder containing your script. Once you do that, you can run the script, and from then on MATLAB will open from that directory (until you manually "change directory" again).