英文:
VS Code extension: How to convert workspace folder uri path to filesystem path?
问题
我正在编写一个使用工作区文件夹路径(File > Open Folder)来确定运行shell命令的基本目录的Visual Studio Code扩展程序。我可以从vscode.workspace.workspaceFolders[0].uri.path
中获取路径,但当我与fs.readdir()
一起使用时,会出现问题,因为它在开头重复了驱动器字母。
Error: ENOENT: no such file or directory, scandir 'C:\c:\Users\Dave\Desktop\ESP Labs'
请注意多余的"C:"
我想找到一种方法来将URI路径转换为适用于fs.readdir()
的文件系统路径。我尝试了url.fileURLToPath()
,但它没有起作用,实际上导致扩展程序停止运行,直到我注释掉了尝试显示结果的console.debug
行。
还有一个名为vscode.workspace.workspaceFolders[0].uri._fspath
的属性,它实现了我想要的效果,但我认为下划线表示该属性是私有的,不应直接使用。
所以我的问题是...
是否有一种方法可以将uri.path转换为文件系统路径,或者我应该忘记它,使用uri._fspath?
以下是代码:
/*
* 复制整个项目目录到远程闪存文件系统。
*/
let syncCommand = vscode.commands.registerCommand('mpremote.sync', async () => {
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length != 1) {
vscode.window.showErrorMessage('无法同步。首先打开一个文件夹。')
}
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('项目文件夹路径:', projectRoot)
let port = await getDevicePort()
term.sendText(`cd '${projectRoot}'`)
fs.readdir(projectRoot, { withFileTypes: true }, (err, entries) => {
if (err) {
console.error(err)
vscode.window.showErrorMessage('无法读取目录。')
}
else {
console.debug('找到的目录条目:', entries.length)
entries.forEach(entry => {
console.debug('检查目录条目:', entry)
if (entry.isDirectory()) {
if (SYNC_IGNORE.includes(entry.name)) {
console.debug('跳过目录:', 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} :`)
}
})
}
})
}
})
调试输出显示了可用的uri属性:
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:\Users\Dave\Desktop\ESP Labs
项目文件夹路径: c:\Users\Dave\Desktop\ESP Labs
英文:
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:\Users\Dave\Desktop\ESP 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:\Users\Dave\Desktop\ESP Labs
Project folder path: c:\Users\Dave\Desktop\ESP Labs
答案1
得分: 1
vscode.workspace.workspaceFolders[0].uri.fsPath
属性,注意没有下划线,可以用来获取与 _fsPath
属性相同的值。详见 vscode API 文档: Uri。
英文:
Well, there is a
vscode.workspace.workspaceFolders[0].uri.fsPath
property you could use - note no underscore. It returns the same thing as the _fsPath
property.
See vscode api docs: Uri.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论