终端进程以退出代码终止:1

时间:2019-05-05 20:27:54

标签: json powershell visual-studio-code mql

我想使用Visual Studio Code IDE(“ VSC”)在MQL中(而不是在本机MetaEditor IDE中)进行开发,如下所述:How to code & compile MQL5 in Visual Studio

我的问题涉及到编译过程,该过程由一个VSC任务组成,该任务调用一个PowerShell脚本,该脚本调用MetaEditor.exe来执行实际的编译。

当我直接运行PowerShell脚本(通过选择其代码并点击 F8 )时,一切正常,但是当我尝试通过指定的VSC任务运行它时,我得到了错误

  

终端进程终止,退出代码为:1

(如链接描述中所述,在我选择PowerShell作为默认外壳之前)。

这是PowerShell脚本(与 F8 一起使用):

#gets the File To Compile as an external parameter... Defaults to a Test file...
Param($FileToCompile = "C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\Experts\Advisors\ExpertMACD.mq5")

#cleans the terminal screen and sets the log file name...
Clear-Host
$LogFile = $FileToCompile + ".log"
& "C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5\compile.bat" "C:\Program Files\MetaTrader 5\metaeditor64.exe" "$FileToCompile" "$LogFile" "C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5"

#before continue check if the Compile File has any spaces in it...
if ($FileToCompile.Contains(" ")) {
    "";"";
    Write-Host "ERROR!  Impossible to Compile! Your Filename or Path contains SPACES!" -ForegroundColor Red;
    "";
    Write-Host $FileToCompile -ForegroundColor Red;
    "";"";
    return;
}

#first of all, kill MT Terminal (if running)... otherwise it will not see the new compiled version of the code...
Get-Process -Name terminal64 -ErrorAction SilentlyContinue |
    Where-Object {$_.Id -gt 0} |
    Stop-Process

#fires up the Metaeditor compiler...
& "C:\Program Files\MetaTrader 5\metaeditor64.exe" /compile:"$FileToCompile" /log:"$LogFile" /inc:"C:\Users\Username\AppData\Roaming\MetaQuotes\Terminal\D0E8209F77C8CF37AD8BF550E51FF075\MQL5" | Out-Null

#get some clean real state and tells the user what is being compiled (just the file name, no path)...
"";"";"";"";""
$JustTheFileName = Split-Path $FileToCompile -Leaf
Write-Host "Compiling........: $JustTheFileName"
""

#reads the log file. Eliminates the blank lines. Skip the first line because it is useless.
$Log = Get-Content -Path $LogFile |
       Where-Object {$_ -ne ""} |
       Select-Object -Skip 1

#Green color for successful Compilation. Otherwise (error/warning), Red!
$WhichColor = "Red"
$Log | ForEach-Object {
    if ($_.Contains("0 error(s), 0 warning(s)")) {
        $WhichColor="Green"
    }
}

#runs through all the log lines...
$Log | ForEach-Object {
     #ignores the ": information: error generating code" line when ME was successful
     if (-not $_.Contains("information:")) {
          #common log line... just print it...
          Write-Host $_ -ForegroundColor $WhichColor
     }
}

#get the MT Terminal back if all went well...
if ($WhichColor -eq "Green") {
    & "c:\program files\metatrader 5\terminal64.exe"
}

这是.json格式的VSC任务,应调用先前的PowerShell脚本(但以上述错误结尾):

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile-MQL",
            "type": "shell",
            "command": "C:\\Users\\Username\\AppData\\Roaming\\MetaQuotes\\Terminal\\D0E8209F77C8CF37AD8BF550E51FF075\\MQL5\\Compile-MQL.ps1 ${file}",
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": false
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

有人可以告诉我如何摆脱这个错误吗?

PS:要重现此问题,MetaTrader(包括MetaEditor IDE)必须为downloaded (for free)

3 个答案:

答案 0 :(得分:0)

之所以可以在F8上运行,是因为您已经在PowerShell会话中,因此可以本地运行。使用任务,您尝试运行.ps1,而不启动PowerShell。

任务运行,是任务运行,您正在协同运行的其他事物的详细信息(开关,参数等)可能有其自己的需求。但是,您的查询可能被视为以下内容的重复:---

How do i configure a task to call a PowerShell script in vscode

# Accepted answer below:

{
    "version": "0.1.0",
    "command": "powershell",
    "args": [   
        "-ExecutionPolicy",
        "Unrestricted",
        "-NoProfile",
        "-File",
        "${cwd}/source/deployment/build.ps1"       
    ],
    "taskSelector": "-task ",
    "showOutput": "always",
    "tasks": [
        {
            "taskName": "build",
            "showOutput": "always",
            "isBuildCommand": true
        }
    ]
}

See also this MSDN Channel 9 video on Task Runners.

恕我直言,如果您已经拥有所需的.ps1,那么为什么要从任务中调用它呢?当然可以,但是您已经在VSC中,只需键入名称即可从VSCode PowerShell控制台终端运行脚本。

您也不说如何定义VSCode用户设置。

示例-您的使用设置中的以下设置:

"terminal.integrated.shell.windows":
"powershell.powerShellExePath":
"shellLauncher.shells.windows": 

OP更新

您似乎两次发布了此查询,而我又回答了两次。

  

How to configure a task to start a .ps1-script in VSC IDE (version 2.0.0)?

当然,解决方案不是我的,而是从我为您提供的问答中得到的答案。

请参阅另一篇文章,在这篇文章中我向您指出了VSCode文档,该文档涉及为要使用的终端配置用户环境。

您的错误专门意味着无法启动Shell进程 ,因为它无法找到所需的内容,因为VSCode中包含/不包含使用设置。

答案 1 :(得分:0)

我遇到了这个问题,因为我不小心删除了tsconfig.json

答案 2 :(得分:0)

我和你有同样的问题(我使用的是 Windows 10)。

尝试以下操作:

  1. 如果您的计算机正在运行 Powershell,请将其关闭。
  2. 控制面板 -> 程序 -> 打开或关闭 Windows 功能
  3. 搜索 Window Powershell 2.0 -> 取消复选框 -> 确定
  4. 重启

enter image description here

希望能帮到你