【问题标题】:F# compiler throws exception when invoked from spawned processF# 编译器从衍生进程调用时抛出异常
【发布时间】:2012-05-22 08:22:03
【问题描述】:

我生成一个新进程并让它像这样调用 F# 编译器:

var exeName = args[0];
var commandLine = args[1];
using (var process = new Process())
{
    process.StartInfo = new ProcessStartInfo(exeName, commandLine);
    process.StartInfo.UseShellExecute = true;
    process.StartInfo.LoadUserProfile = true;

    process.Start();
}

我传入的参数是 fsc.exe 的路径以及我要构建的代码的参数。
结果异常:

Unhandled Exception: System.ArgumentException: chop_extension
   at Internal.Utilities.Filename.chop_extension(String s)
   at Microsoft.FSharp.Compiler.Build.TcConfigBuilder.DecideNames(FSharpList`1 sourceFiles)
   at Microsoft.FSharp.Compiler.Driver.main1(String[] argv)
   at Microsoft.FSharp.Compiler.ErrorLogger.ErrorLoggerExtensions.ReraiseIfWatsonable(Exception exn)
   at Microsoft.FSharp.Compiler.ErrorLogger.ErrorLoggerExtensions.ErrorLogger.ErrorRecovery(ErrorLogger x, Exception exn, range m)
   at Microsoft.FSharp.Compiler.ErrorLogger.errorRecovery(Exception exn, range m)
   at Microsoft.FSharp.Compiler.Driver.main1(String[] argv)
   at Microsoft.FSharp.Compiler.Driver.main(String[] argv)
   at Microsoft.FSharp.Compiler.CommandLineMain.main(String[] argv)
   at Microsoft.FSharp.Compiler.ErrorLogger.ErrorLoggerExtensions.ReraiseIfWatsonable(Exception exn)
   at Microsoft.FSharp.Compiler.ErrorLogger.ErrorLoggerExtensions.ErrorLogger.ErrorRecovery(ErrorLogger x, Exception exn, range m)
   at Microsoft.FSharp.Compiler.ErrorLogger.errorRecovery(Exception exn, range m)
   at Microsoft.FSharp.Compiler.CommandLineMain.main(String[] argv)

但是,当我从命令提示符运行相同的命令和参数时,它编译时没有错误

知道是什么原因造成的吗?

【问题讨论】:

  • 你使用的论据是什么?可能是输入字符串格式错误。
  • 您是从 C# 代码调用 fsc exe 吗?这就是第一个代码片段的样子,我只想确认我的假设。我同意 Pad——向我们展示 exeName 和 commandLine 的值。

标签: .net process f#


【解决方案1】:

在不知道您传递给编译器的参数的情况下很难给出具体的答案 - 错误可能来自一些格式错误的命令行参数。

但是,如果要从 C# 调用 F# 编译器,则无需使用 Process 类显式执行此操作。您可以使用F# PowerPack 中提供的 F# CodeDom 提供程序 - 它负责格式化参数(以及定位 F# 编译器,这可能非常微妙)。

下面是一个简短的示例,展示了如何从 F# 调用它(从 C# 中调用类似):

#r "FSharp.Compiler.CodeDom.dll"

open System.CodeDom.Compiler
open Microsoft.FSharp.Compiler.CodeDom

let provider = new FSharpCodeProvider()
let parameters = CompilerParameters()
provider.CompileAssemblyFromFile(parameters, [| "C:...file.fsx" |])

【讨论】:

  • 没有意识到你可以直接从代码中做到这一点。很高兴知道。
猜你喜欢
  • 1970-01-01
  • 2010-09-12
  • 1970-01-01
  • 2011-04-02
  • 2014-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
相关资源
最近更新 更多