Replies: 8 comments
-
Invoking Also, |
Beta Was this translation helpful? Give feedback.
-
Is doing it programmatically a supported scenario? At least i had that impression reading this tutorial i followed from the docs: https://fsharp.github.io/fsharp-compiler-docs/fcs/compiler.html
It works with other scripts that use #r nuget though. In my example, if i go back one major version in the dependency (which is targeting netstandard, while 2.0.0 targets .net 8), it works. If this is not a supported scenario though, how would one proceed to check wether a script contains correct F# code programmatically? |
Beta Was this translation helpful? Give feedback.
-
This is not exactly invoking it, it's just part of API, it's not actually calling
It is supported in scripts, when you run them with Update: I think I know what you mean, I initially misunderstood the question/issue. I think you need to pass all references (including runtime ones) if you run it from compiled code. @TheAngryByrd or @baronfel might actually know better since they work much more with FCS from the consumer perspective. |
Beta Was this translation helpful? Give feedback.
-
No worries, I see why. I tried to include all information i deemed relevant, but using fsc.exe directly is not really what this issue is about, i just thought it was relevant that that also fails. To be fair, this line checker.Compile([| "fsc.exe"; "-o"; tempPath; "-a"; scriptPath |]) is directly from the docs and confused me since it clearly looks like it is calling To come back to the issue at hand, compiling the script programmatically with
How would i proceed adding these references? I do not think i can just add these assemblies via their path, as this is running in a CI job. also, it seems like I hope this is formulated more clearly now, sorry for the confusion |
Beta Was this translation helpful? Give feedback.
-
Compiling an F# application requires the developer to provide all of the required compiler inputs, these are:
For fsc.exe these inputs are gathered by the Dotnet SDK build process, so Ideally you can build an fsproj file, and run the command Back in the day fsc myapp.fs used to create a runnable .exe file, that was relatively painless for the compiler to do, because most of the runtime was in the mscorlib.dll and System.XML.dll and one other whose name escapes for now, which we could discover at runtime. However, with the advent of Windows 8 and the refactoring of the runtime references got more compilated and with the coreclr even more complicated. To compile a coreclr app, the compiler is now presented with a list of On day one of converting the FSC compiler to the coreclr we determined we would never want it to 'helpfully' find a framework to compile against, we would always rely on the dotnet SDK to do that. That principle is the same as that held by the CSC compiler. We did not stick to that principle for FSI, although in my opinion we should have, it would have certainly improved startup and script execution time. My recommendation to you is you create a new project that Includes your script file and use I hope this helps Kevin |
Beta Was this translation helpful? Give feedback.
-
Thanks for the interesting insights @KevinRansom ! So if i understand correctly,
This will not work for me i am afraid, because then i would have to programmatically create projects and compile them, hundreds of times for each CI job. However, i found two projects that are dotnet tools that compile fsx scripts to executables: fflat and FSharpPacker, which seem to do exactly that - so i think i'll take the route of calling them and just checking if they have an exit code of 0. On a side note, I do not understand why |
Beta Was this translation helpful? Give feedback.
-
@kMutagene #r Mutagene ... productizes a different nasty hack. Or at least it's nasty hack exists in a different separate assembly from the compiler. Someone from the C# team was violently ill, when I explained how we did it, and it should be noted that C# interactive has nothing like it, although they do have a nasty hack for locating framework assemblies, otherwise it wouldn't run. I am not at all proud of either piece of code, but I have to admit they are very useful, both have been on my "I absolutely must rewrite this better and differently list since the day they were committed".
In order to avoid doing this once per test we build the project and cache the reference list once per target framework. And re-use the paths to the references over and over again. It's mostly handled in our tests here: It is not well factored or has great code, but it shows you some of the ways we deal with this problem in our testing. I hope this clears things up, please don't think Ill of me; the msbuild/nuget dependency to get framework assembly references needs a better solution that is less hacky and contextual it just hasn't bubbled to the top of anyone's consciousness. Kevin |
Beta Was this translation helpful? Give feedback.
-
@kMutagene I may have some code that you may find interesting in my project. Essentially |
Beta Was this translation helpful? Give feedback.
-
Hi there, i am new to working with the compiler directly, so please bear with me if this issue does not contain all relevant information.
My basic use case is that i want to use the
FSharp.Compiler.Service
nuget package to verify whether the content of a script contains valid F# code. To do that, i followed this tutorial.this compilation fails, while just running the script via
dotnet fsi
works without issues (meaning that there is actually nothing wrong with the code)Repro steps
Above tutorial basically means callingfsc.exe -o ./test.dll -a test.fsxThe respective F# code using
FSharp.Compiler.Service
looks like this (i am using this in a xUnit test project):try to compile a script with this content (i removed much of the original content trying to narrow this issue down):
Expected behavior
Script compiles (exitcode is 0 and error list is empty)
Actual behavior
this results in the following error for both, compiling via cli or compiling programmatically:
but running it via
dotnet fsi
just works fine (also both via cli or programmatically):Known workarounds
use
dotnet fsi
to "verify" the script, however this is not a good workaround since this actually executes the code, but i just want to know whether it could runRelated information
Provide any related information (optional):
<PackageReference Include="FSharp.Compiler.Service" Version="[43.8.300]" />
Beta Was this translation helpful? Give feedback.
All reactions