Skip to content

Simplify FCS interaction code by using Yaaf.FSharp.Scripting. - #305

Merged
tpetricek merged 16 commits into
fsprojects:masterfrom
matthid:improve_fcs_error
Apr 28, 2015
Merged

tpetricek merged 16 commits into
fsprojects:masterfrom
matthid:improve_fcs_error

Conversation

@matthid

@matthid matthid commented Apr 24, 2015

Copy link
Copy Markdown
Member

EDIT

As discussed below, this PR now references the source code of https://github.com/matthid/Yaaf.FSharp.Scripting and fixes several bugs:

  • When specifying an FsiEvalutator we currently have the nasty side effect of changing the current directory, this will follow up with a vast number of different exceptions (depending on which paths are relative and which are full)
  • The current "fsi"-object workaround did not work on mono.
  • When something goes wrong in FCS we get improved exceptions.
  • Our own documentation generation has now the same environment, this makes sure we detect problems (like the above) sooner here at FSharp.Formatting (Dont Merge #306).

At the same time the code is simplified.


Original (continuation of #303)

We cannot see the "real" error, because FSharp.Formatting is swallowing it...

For the FsLab issue:

The root cause seems to be in https://github.com/tpetricek/FSharp.Formatting/blob/master/src/FSharp.Literate/Evaluator.fs#L130 which pretty much expects all libraries to be available near FSharp.Literate.dll. As it is searching for compile-time assemblies, I'm now wondering why it is actually working on windows. I think we will know more when we have the exact error message...
While I'm looking for the exact error this can already run through the CI build in the mean time...

@matthid

matthid commented Apr 24, 2015

Copy link
Copy Markdown
Member Author

Ok thats the message after this patch:

Error:
System.Exception: FSI failed:
        Error:

/home/matthid/projects/FsLab.Templates/input.fsx(1,1): error FS0078: Unable to find the file 'FSharpVSPowerTools.Core.dll' in any of
 /usr/lib64/mono/4.5
 /home/matthid/projects/FsLab.Templates/packages/FSharp.Formatting/lib/net40
 /home/matthid/projects/FsLab.Templates
 /home/matthid/projects/FsLab.Templates/packages/FAKE/tools/

        Out:  ---> System.Exception: Evaluation failed ---> Microsoft.FSharp.Compiler.Build+FileNameNotResolved: Exception of type 'Microsoft.FSharp.Compiler.Build+FileNameNotResolved' was thrown.
  at Microsoft.FSharp.Compiler.Build+TcConfig.ResolveLibWithDirectories (Microsoft.FSharp.Compiler.CcuLoadFailureAction ccuLoadFaulureAction, Microsoft.FSharp.Compiler.AssemblyReference r) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Compiler.Build+TcImports.TryResolveAssemblyReference (Microsoft.FSharp.Compiler.AssemblyReference assemblyReference, Microsoft.FSharp.Compiler.ResolveAssemblyReferenceMode mode) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Compiler.Build.tryFile@4408 (Microsoft.FSharp.Compiler.TcImports tcImports, range m, System.String speculativeFileName) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Compiler.Build+TcImports.ImplicitLoadIfAllowed (range m, System.String assemblyName, Boolean lookupOnly) [0x00000] in <filename unknown>:0
  at Microsoft.FSharp.Compiler.Build+TcImports.FindCcuInfo (range m, System.String assemblyName, Boolean lookupOnly) [0x00000] in <filename unknown>:0
...

It's not like super nice but definitely better than before. But still: I have no idea why it is working on windows :(.

@matthid

matthid commented Apr 24, 2015

Copy link
Copy Markdown
Member Author

But yes after looking at this, bundling as proposed in #303 would help here as well...

@matthid

matthid commented Apr 24, 2015

Copy link
Copy Markdown
Member Author

@tpetricek : I'm giving up on the FsLab/FCS issue, I guess there is code somewhere in FCS to consider currently loaded assemblies when resolving (compile) assemblies, but I couldn't find it. Maybe this is even just another FCS issue?

We can work around this particular issue within FSharp.Formatting quite easily by just adding additional "#I" and "#r" statements not only for the currently executing, but for all assemblies we need.

Or we consider this as by design (IE tell users of FSharp.Formatting that we expect our dependencies to live in the same folder as "FSharp.Formatting.dll").

@tpetricek

Copy link
Copy Markdown
Member

Ah! Thanks a lot for looking into this, I think I now understand what's going on (looks like I did break something with a recent change :-( ...)

I basically added the broken piece of code to avoid dependency on FSharp.Compiler.Interactive.Settings.dll which was major source of pain... So, I changed the code to initialize dummy fsi object, but that introduced this new issue.

I think the safest option is just to eval some ugly inlined string with the definition of fsi object at the beginning.

BTW: I used this to get better error info: http://fssnip.net/qF

@tpetricek

Copy link
Copy Markdown
Member

Well, this was definitely my mistake - after fixing this (I'll send a PR), we won't need to have all the libraries in the same folder, so perhaps that also reduces the urgency of #303 - but I think the discussion there is still valid and it is probably a good direction (but we can perhaps wait a bit more to see if other people have any thoughts on that...)

@matthid

matthid commented Apr 24, 2015

Copy link
Copy Markdown
Member Author

to avoid dependency on FSharp.Compiler.Interactive.Settings.dll

This is really a PIA, that's one of the reason why I have extracted the scripting use case in https://github.com/matthid/Yaaf.FSharp.Scripting for a scripting API with all those little bugfixes like nice error messages (this PR) or fixing the FSharp.Compiler.Interactive.Settings.dll thing.

Currently we have those fixes spread out in several locations:

To show two examples :)

we won't need to have all the libraries in the same folder

Ah, so we don't actually need to load ourself into the interactive we are hosting? That should definitely fix this as well :)

However I still feel like we should add this (or something similar), because it will give people using FSharp.Literate exactly the error message they would get when running in FSI.exe (and it will simplify our lives at well when people report errors or when we encounter them)

BTW: I used this to get better error info: http://fssnip.net/qF

Oo never noticed this, thanks!

but we can perhaps wait a bit more to see if other people have any thoughts on that...

Yes to get feedback was exactly the intention of #303 ...

@tpetricek

Copy link
Copy Markdown
Member

Yaaf.FSharp.Scripting looks very nice! I would probably prefer to avoid another DLL dependency, but if we could import something like that as a few source files via Paket, that might be nice (and I suspect FAKE could do the same).

The way I understand the F# Compiler Interactive Service thing is that:

  1. FsiEvaluationSession.GetDefaultConfiguration(new InteractiveSettings(), false) where InteractiveSettings is your type - this gives you fsi object, but it requires FSharp.Compiler.Interactive.Settings.dll.
  2. FsiEvaluationSession.GetDefaultConfiguration() this does not give you fsi object and does not require F.C.I.S.dll (FAKE does this and as a result you cannot just #load "FsLab.fsx" in a FAKE script, because FsLab calls fsi.AddPrinter...). I did a workaround (part 1 & part 2) for this.
  3. FsiEvaluationSession.GetDefaultConfiguration() and then EvalInteraction("let fsi = ...") like what we are doing here is a way to get fsi object without requiring F.C.I.S.dll. (But we need to do it without having to reference ourselves - I thought that was clever, but it breaks things. Basically, having the implementation in a big string is probably OK.)

Having all the tweaks in some file that we could reference via Paket would be nice (and we could send PR to FAKE to use it too.) It would be nice to add (3) to FAKE too - I could then get rid of my dirty workarounds for (2)...

@matthid

matthid commented Apr 24, 2015

Copy link
Copy Markdown
Member Author

Very nice idea! I will try to refactor the features in easy to include source files!

FsiEvaluationSession.GetDefaultConfiguration(new InteractiveSettings(), false) where InteractiveSettings is your type - this gives you fsi object, but it requires FSharp.Compiler.Interactive.Settings.dll.

Actually I experienced the reverse when working these things out in the past: https://github.com/fsharp/FSharp.Compiler.Service/blob/89d936ca760b790f7741a0bab9122002a792810c/src/fsharp/fsi/fsi.fs#L2705 The useauxlib parameter means adding a reference to F.C.I.S.dll. It's definitely hard, confusing and impossible to get it perfectly right, I learned that the hard way :)

But we need to do it without having to reference ourselves - I thought that was clever, but it breaks things.

The funny thing is we could do this in Yaaf.FSharp.Scripting.dll but only when it's a separate dll without and additional references :). Not when it's included via source files.

FsiEvaluationSession.GetDefaultConfiguration()

https://github.com/fsharp/FSharp.Compiler.Service/blob/master/src/fsharp/fsi/fsi.fs#L2855 it literally uses (obj, false)

Bah I hope at least some of that is correct :) . I'll report back once I have refactored the files.

Edit: Removed wrong sentence.

@matthid

matthid commented Apr 24, 2015

Copy link
Copy Markdown
Member Author

Uh, I think some of my "magic" is not working when included as script file :(

@matthid

matthid commented Apr 24, 2015

Copy link
Copy Markdown
Member Author

FsiEvaluationSession.GetDefaultConfiguration(new InteractiveSettings(), false)

Did you mean true here? Then everything makes sense now...
So you are saying that we cannot make the fsi object work properly with false (ie no F.S.I.S.dll reference) but we can hook in our own implementation to not fail immediately (even when scripts now produce potentially different output)?

Ok done, I re-used this PR (the title is still true).
This is my magic string by the way: https://github.com/matthid/Yaaf.FSharp.Scripting/blob/develop/src/source/Yaaf.FSharp.Scripting/YaafFSharpScripting.fs#L316.
But now after re-reading your post it probably doesn't really matter what implementation we use (any dummy implementation would probably do the same)...

@matthid matthid changed the title improve fcs error reporting in FSharp.Literate. Simplify FCS interaction code by using Yaaf.FSharp.Scripting. Apr 25, 2015
@matthid

matthid commented Apr 25, 2015

Copy link
Copy Markdown
Member Author

Added another simplification.

matthid added a commit to matthid/FAKE that referenced this pull request Apr 25, 2015
This also fixes some problems with the current implementation like missing "fsi" (noticed by tpetricek here: fsprojects/FSharp.Formatting#305)
matthid added a commit to matthid/Yaaf.AdvancedBuilding that referenced this pull request Apr 26, 2015
@matthid matthid changed the title Simplify FCS interaction code by using Yaaf.FSharp.Scripting. (WIP/discuss) Simplify FCS interaction code by using Yaaf.FSharp.Scripting. Apr 26, 2015
@matthid matthid mentioned this pull request Apr 26, 2015
@matthid matthid changed the title (WIP/discuss) Simplify FCS interaction code by using Yaaf.FSharp.Scripting. Simplify FCS interaction code by using Yaaf.FSharp.Scripting. Apr 26, 2015
@matthid

matthid commented Apr 26, 2015

Copy link
Copy Markdown
Member Author

OK finally everything seems to work as it should. A lot of commits but actually only a very few amount of changes to review (details in the edited start comment).

tpetricek added a commit that referenced this pull request Apr 28, 2015
Simplify FCS interaction code by using Yaaf.FSharp.Scripting.
@tpetricek
tpetricek merged commit 06eba67 into fsprojects:master Apr 28, 2015
@tpetricek

Copy link
Copy Markdown
Member

I wanted to be sure that relative paths are still handled correctly, so I added a test for that and... everything worked perfectly :-)

In fact, I think it is actually even redirecting the work to the fsi object (which is doing a few unexpected things in FsLab - like showing charts - but that's okay - this is the right thing to do :-))

@matthid

matthid commented Apr 29, 2015

Copy link
Copy Markdown
Member Author

Thanks for the testing and the release!

everything worked perfectly :-)

That was exactly the intention: Make F# scripting work as easily as possible without the need to care about all this FCS nuances/bugs/argument stuff.

But something (which looks related to this change) is broken now :( , looking into it...

wanted to be sure that relative paths are still handled correctly,

After noticing this I'm also wondering why FsiEvaluator is not used by default? I have planned to enable it for all my projects (now, after this has been merged & released). At least we could enable it in ProjectScaffold so you can use those "enable script output" features out-of-the-box?

In fact, I think it is actually even redirecting the work to the fsi object (which is doing a few unexpected things in FsLab - like showing charts - but that's okay - this is the right thing to do :-))

Thanks for this, I did some testing myself but I wasn't sure if everything works as it should when some advanced stuff is done with it :)

Everything is done with reflection so it may be a bit slow, but I don't think anyone is calling the fsi object like crazy anyway (only for initial setup).

Btw FAKE does now also contain this, so you may be able to remove some workarounds you mentioned above (please report if something breaks)

@tpetricek

Copy link
Copy Markdown
Member

I think we could enable it for ProjectScaffold - can you send a PR there? We initially thought it was a bit experimental and perhaps we don't want to run things on people's machine when building docs :-) but I think it is mature and people expect this now...

@matthid

matthid commented Apr 29, 2015

Copy link
Copy Markdown
Member Author

Yeah, but first I'll need figure out why something broke with the update...

@matthid
matthid deleted the improve_fcs_error branch September 13, 2015 12:21
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

Successfully merging this pull request may close these issues.

2 participants