Skip to content

Commit dfaebec

Browse files
dklilleyGitHub Enterprise
authored andcommitted
Merge pull request mathworks#39 from development/dlilley.bugfix-allow_quote_in_install_path
Enable connection to MATLAB when single quote appears in file path
2 parents fc1eca4 + e48dbd5 commit dfaebec

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ MATLAB language server supports these editors by installing the corresponding ex
2727

2828
### Unreleased
2929

30+
Fixed:
31+
* Allow connection to MATLAB when a single quote appears in the file path
32+
3033
### 1.2.3
3134
Release date: 2024-06-14
3235

src/lifecycle/MatlabSession.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ async function getMatlabLaunchCommand (outFile: string): Promise<{ command: stri
398398
'-memmgr', 'release', // Memory manager
399399
'-noAppIcon', // Hide MATLAB application icon in taskbar/dock, if applicable
400400
'-nosplash', // Hide splash screen
401-
'-r', `addpath(fullfile('${__dirname}', '..', 'matlab')); initmatlabls('${outFile}')`, // Startup command
401+
'-r', getMatlabStartupCommand(outFile), // Startup command
402402
'-useStartupFolderPref', // Startup folder flag
403403
'-nodesktop' // Hide the MATLAB desktop
404404
]
@@ -418,3 +418,23 @@ async function getMatlabLaunchCommand (outFile: string): Promise<{ command: stri
418418
args
419419
}
420420
}
421+
422+
/**
423+
* Gets the MATLAB command which the MATLAB application should run at startup.
424+
*
425+
* Note: This will sanitize the file paths so that they can be safely used within
426+
* character vectors in MATLAB. This is done by replacing all single-quote characters
427+
* with double single-quotes.
428+
*
429+
* @param outFile The file in which MATLAB should output connection details
430+
* @returns The MATLAB startup command
431+
*/
432+
function getMatlabStartupCommand (outFile: string): string {
433+
// Sanitize file paths for MATLAB:
434+
// Replace single-quotes in the file path with double single-quotes
435+
// to preserve the quote when used within a MATLAB character vector.
436+
const extensionInstallationDir = __dirname.replace(/'/g, "''")
437+
const outFilePath = outFile.replace(/'/g, "''")
438+
439+
return `addpath(fullfile('${extensionInstallationDir}', '..', 'matlab')); initmatlabls('${outFilePath}')`
440+
}

0 commit comments

Comments
 (0)