Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio code VS代码-使用代码运行程序扩展在虚拟机(VM)上运行程序_Visual Studio Code_Vagrant_Virtual Machine_Vagrant Windows - Fatal编程技术网

Visual studio code VS代码-使用代码运行程序扩展在虚拟机(VM)上运行程序

Visual studio code VS代码-使用代码运行程序扩展在虚拟机(VM)上运行程序,visual-studio-code,vagrant,virtual-machine,vagrant-windows,Visual Studio Code,Vagrant,Virtual Machine,Vagrant Windows,我通过Vagrant和Virtual Box在Windows上使用Linux虚拟机进行开发。我试图弄清楚如何让代码运行程序扩展在VM上运行我的文件。到目前为止,最大的障碍是,对于给定的文件,我需要将Windows主机路径转换为Linux来宾路径 背景: 扩展名允许将文件类型映射到shell命令以运行这些文件。比如说, “java”:“cd$dir&&javac$fileName&&java$fileNameWithoutExt” 告诉代码运行程序,当我尝试运行Java文件时,它应该cd到包含该

我通过Vagrant和Virtual Box在Windows上使用Linux虚拟机进行开发。我试图弄清楚如何让代码运行程序扩展在VM上运行我的文件。到目前为止,最大的障碍是,对于给定的文件,我需要将Windows主机路径转换为Linux来宾路径


背景:

扩展名允许将文件类型映射到shell命令以运行这些文件。比如说,

“java”:“cd$dir&&javac$fileName&&java$fileNameWithoutExt”

告诉代码运行程序,当我尝试运行Java文件时,它应该cd到包含该文件的目录,编译该文件,然后运行编译后的文件。从文件类型到命令的映射称为
code runner.executomap
,它包含在
settings.json
中。通过添加选项

“code runner.runinternal”:true

对于我的
settings.json
,我可以告诉CodeRunner在集成终端中运行。因此,只需通过
vagrant ssh
从集成终端将SSHing下载到我的虚拟机中,我就有了针对虚拟机的code runner

这就是问题所在-Code Runner使用我的Windows样式路径和我的Windows文件结构作为VM的命令行参数

例如,假设我的Windows文件结构看起来像
c:\a\b\c\d
,并且我的VM的根位于
c
,因此
c
d
是共享文件夹。如果我想在
d
中运行文件,命令
cd$dir
将告诉我的虚拟机执行
cd c:\a\b\c\d

我考虑过一些变通方法,比如在设置中添加以下内容以运行python文件

“python”:“cd\”$(dirname\“$(locate-l1$fileName)\”;“python3$fileName”,

此命令在集成终端(VM)上运行,用于定位并更改包含要运行的文件的目录。然后它告诉python3解释器运行该文件。但是,这并不总是有效(例如,具有相同名称的多个文件),并且要求我在每次添加文件时更新
locate
所依赖的数据库


必须有某种方法将我的Windows文件路径转换为虚拟机上的路径(例如
c:\a\b\c\d
->
/c/d
)。也许是通过流浪汉?我将感谢任何帮助

我制定了一个变通办法。我仍然对“更清洁”的解决方案感兴趣


解决办法如下:

首先,我编写了一个Python脚本,将Windows路径转换为虚拟机上的路径。脚本将文件的windows路径和文件名作为参数

#pathconverter.py
import sys
windows_path=sys.argv[1]
file_name=sys.argv[2]

path_to_vagrantfile = r"C:\Users\Evan\Google Drive\Development\Vagrantfile"
slashes=path_to_vagrantfile.count("\\")

y=windows_path.split("\\")[slashes:]
linux_path="/vagrant/"+'/'.join(y) + "/" + file_name
print(linux_path)
因此,以下代码将从windows文件位置转换为我的虚拟机上的文件位置(假设您将pathconverter.py保存在共享目录的根目录下,
\vagrant

python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName
因此,要运行各种解释语言的大多数文件,我只需将此命令的输出作为参数提供给解释器

"python": "python3 \"$(python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName)\""
或者对于球拍/方案,我只做:

"scheme": "racket \"$(python3 \"/vagrant/pathconverter.py\" $dirWithoutTrailingSlash $fileName)\""