Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompileAssemblyFromFile crashes with "The system cannot find the file specified" even though File.Exists reports that the file exists #27

Closed
sharma7n opened this issue Dec 18, 2019 · 2 comments

Comments

@sharma7n
Copy link

Description

When I call CompileAssemblyFromFile on my provider instance, the program throws the following error stack trace:

Unhandled Exception: System.ComponentModel.Win32Exception: The system cannot find the file specified
   at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
   at FSharp.Compiler.CodeDom.Internal.Compiler.compileFiles(String args, CompilerResults res)
   at FSharp.Compiler.CodeDom.Internal.Compiler.compileAssemblyFromFileBatch(CompilerParameters options, String[] fileNames, CompilerResults results, FSharpFunc`2 sortf)
   at Program.main(String[] argv)

This occurs despite the fact that System.IO.File.Exists returns true when passed in the file path.

Here is the full code:

open System.IO
open System.CodeDom.Compiler
open FSharp.Compiler.CodeDom


[<EntryPoint>]
let main argv =
    let sourceFilePath = Array.get argv 0
    if File.Exists(sourceFilePath) then
        let provider = new FSharpCodeProvider()
        let mutable compilerParameters = CompilerParameters()
        compilerParameters.GenerateInMemory <- true
        provider.CompileAssemblyFromFile(compilerParameters, [| sourceFilePath |])
        |> ignore
    else
        printf "File does not exist"
    0

Here is myfsproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net461</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="FSharp.Compiler.CodeDom" Version="1.0.0.1" />
  </ItemGroup>

</Project>

Repro steps

  1. Create a new application using: dotnet new console -lang F#.
  2. cd into the application directory.
  3. Paste the above code block into Program.fs.
  4. Paste the above config block into <application>.fsproj.
  5. dotnet build (succeeds).
  6. Create an F# source file in the same directory and save it as foo.fs. I used the following:
namespace Foo

type Bar = { baz : int }
  1. dotnet run foo.fs

Expected behavior

The program executes without throwing an exception.

Actual behavior

The program throws System.ComponentModel.Win32Exception with the above stack trace.

Known workarounds

No known workarounds.

Related information

  • Operating system: Windows 10, 64-bit OS, x64-based processor
  • Branch: Hopefully master? (installed from nuget)
  • .NET Runtime, CoreCLR or Mono Version: dotnet --version returns '3.0.100'
@drvink
Copy link
Collaborator

drvink commented Nov 29, 2020

Sorry I'm so late in replying to this. May I ask how you're using this package with .NET Core? FCS is a better, well-supported option for F#; this package really exists for legacy compatibility at this point...

@sharma7n
Copy link
Author

Unfortunately, I'm no longer working at the company where I was trying to do this and no longer have access to the machine I was developing on, so I can't answer your question. I'll just go ahead and close the issue. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants