You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编译.mq4文件无输出求助:GitHub Action中PowerShell脚本异常

解决MetaEditor命令行编译.mq4无输出且日志为空的问题

以下是针对你的PowerShell脚本问题的排查和修改方案:

核心问题定位

你的脚本存在几个关键问题,导致编译无输出且日志为空:

  • Start-Process默认异步执行,脚本未等待编译完成就读取日志
  • 异常捕获存在拼写错误($_.Excaption应为$_.Exception)
  • 路径引号处理可能导致参数解析失败
  • 日志文件初始化未确保目录存在

具体修改步骤

1. 等待MetaEditor进程完成

修改Start-Process调用,添加-Wait和-NoNewWindow参数,确保脚本等待编译完成后再继续:

try {
    Start-Process $exePath -ArgumentList $args -Wait -NoNewWindow
} catch {
    Write-Error "Compilation failed, error: $($_.Exception.Message)"
}

2. 修正日志文件初始化

确保日志文件所在目录存在,避免因目录缺失导致日志无法写入:

# 确保日志目录存在
$logDir = Split-Path $LogFile -Parent
if (-not (Test-Path $logDir)) {
    New-Item -ItemType Directory -Path $logDir | Out-Null
}
# 创建空日志文件
Set-Content -Path $LogFile -Value "" -Force

3. 修复路径参数的引号问题

Start-Process的ArgumentList会自动处理带空格的路径,无需手动添加引号,修改参数添加逻辑:

$args.Add("/portable")
$args.Add("/compile:$((Resolve-Path $Files).Path)")
$args.Add("/inc:$((Resolve-Path $Includes).Path)")
$args.Add("/log:$((Resolve-Path $LogFile).Path)")
if ([System.Convert]::ToBoolean($SyntaxOnly)) {
    $args.Add("/s")
}

4. 增加调试输出(可选)

添加调试信息,确认所有路径和参数正确:

if ($verbose) {
    Write-Host "MetaEditor download URL: $url"
    Write-Host "MetaEditor path: $exePath"
    Write-Host "Compilation arguments: $($args -join ' ')"
    Write-Host "Target files: $Files"
    Write-Host "Include directory: $Includes"
    Write-Host "Log file path: $LogFile"
}

5. 验证MetaEditor版本兼容性

从外部下载的MetaEditor版本可能与你的.mq4文件语法不兼容,建议在本地测试对应版本的MetaEditor能否正常编译目标文件,确认版本匹配。

6. 适配GitHub Action环境路径

在GitHub Action中,使用$env:GITHUB_WORKSPACE构建绝对路径,避免工作目录问题:

# 示例:替换为你的实际路径
$Files = Join-Path $env:GITHUB_WORKSPACE "src/indicator.mq4"
$Includes = Join-Path $env:GITHUB_WORKSPACE "include"
$LogFile = Join-Path $env:GITHUB_WORKSPACE "compile.log"

修改后的完整脚本

$url = "http://www.rosasurfer.com/.mt4/{0}/metaeditor.exe" -f $Version
$exePath = "metaeditor.exe"
if ($verbose) {
    Write-Host "Attempting download from $url..."
}

try {
    Invoke-WebRequest -Uri $url -OutFile $exePath
} catch {
    Write-Error "Failed to download MetaEditor from $url, error: $($_.Exception.Message)"
}

# 确保日志目录存在并创建空日志文件
$logDir = Split-Path $LogFile -Parent
if (-not (Test-Path $logDir)) {
    New-Item -ItemType Directory -Path $logDir | Out-Null
}
Set-Content -Path $LogFile -Value "" -Force

$args = New-Object System.Collections.Generic.List[string]
$args.Add("/portable")
$args.Add("/compile:$((Resolve-Path $Files).Path)")
$args.Add("/inc:$((Resolve-Path $Includes).Path)")
$args.Add("/log:$((Resolve-Path $LogFile).Path)")
if ([System.Convert]::ToBoolean($SyntaxOnly)) {
    $args.Add("/s")
}

$exePath = Resolve-Path $exePath
if ($verbose) {
    Write-Host "Executing MetaEditor with arguments: $($args -join ' ')"
}

try {
    Start-Process $exePath -ArgumentList $args -Wait -NoNewWindow
} catch {
    Write-Error "Compilation failed, error: $($_.Exception.Message)"
}

Get-Content $LogFile

内容的提问来源于stack exchange,提问作者Woody1193

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 07:23:18