I have a Visual Studio Code extension I m writing that uses the workspace folder path (File > Open Folder) to determine the base directory to run shell commands from. I can get the path from vscode.workspace.workspaceFolders[0].uri.path
, but it causes problems when I use it with fs.readdir()
because it s repeating the drive letter at the beginning.
Error: ENOENT: no such file or directory, scandir C:c:UsersDaveDesktopESP Labs
Notice the extra C:
I would like to find a way to convert the URI path to a suitable filesystem path that will work with fs.readdir()
. I tried url.fileURLToPath()
but it didn t work and actually caused the extension to stop functioning until I commented out the console.debug line where I tried to display the result.
There s also a property called vscode.workspace.workspaceFolders[0].uri._fspath
This does what I want, but I m thinking the underscore indicates the property is private and should not be used directly.
So my question is this...
Is there a method to convert uri.path to a filesystem path or should I just forget about it and use uri._fspath?
The code:
/*
* Copy entire project directory to remote flash file system.
*/
let syncCommand = vscode.commands.registerCommand( mpremote.sync , async () => {
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length != 1) {
vscode.window.showErrorMessage( Unable to sync. Open a folder first. )
}
else {
console.debug( vscode.workspace.workspaceFolders[0] , vscode.workspace.workspaceFolders[0])
console.debug( uri._fsPath: , vscode.workspace.workspaceFolders[0].uri._fsPath)
//console.debug( url.fileURLToPath: , url.fileURLToPath(vscode.workspace.workspaceFolders[0].uri.path))
let projectRoot = vscode.workspace.workspaceFolders[0].uri._fsPath
console.debug( Project folder path: , projectRoot)
let port = await getDevicePort()
term.sendText(`cd ${projectRoot} `)
fs.readdir(projectRoot, { withFileTypes: true }, (err, entries) => {
if (err) {
console.error(err)
vscode.window.showErrorMessage( Unable to read directory. )
}
else {
console.debug( Directory entries found: , entries.length)
entries.forEach(entry => {
console.debug( Examining directory entry: , entry)
if (entry.isDirectory()) {
if (SYNC_IGNORE.includes(entry.name)) {
console.debug( Skipping directory: , entry.name)
}
else {
term.sendText(`${PYTHON_BIN} -m mpremote connect ${port} fs cp -r ${entry.name} :`)
}
}
else {
term.sendText(`${PYTHON_BIN} -m mpremote connect ${port} fs cp ${entry.name} :`)
}
})
}
})
}
})
Debug output showing available uri properties:
vscode.workspace.workspaceFolders[0] {uri: v, name: ESP Labs , index: 0}
vscode.workspace.workspaceFolders[0] {
uri: v {
scheme: file ,
authority: ,
path: /c:/Users/Dave/Desktop/ESP Labs ,
query: ,
fragment: ,
_formatted: file:///c%3A/Users/Dave/Desktop/ESP%20Labs ,
_fsPath: c:\Users\Dave\Desktop\ESP Labs
},
name: ESP Labs ,
index: 0
}
uri._fsPath: c:UsersDaveDesktopESP Labs
Project folder path: c:UsersDaveDesktopESP Labs