fsprojects / FSharp.Formatting

F# tools for generating documentation (Markdown processor and F# code formatter)
https://fsprojects.github.io/FSharp.Formatting/
Other
464 stars 155 forks source link

Guidance on literate programming #804

Open TheAngryByrd opened 1 year ago

TheAngryByrd commented 1 year ago

I'm looking for some guidance on how to deal with a specific problem when doing Literate Prgramming. This involves trying to update the doc's examples with new code that isn't available yet on nuget.

For example lets say I have this snippet:

#r "nuget: FsToolkit.Errorhandling"
open FsToolkit.ErrorHandling

let test : Result<int,string> = result { return 4 }

test
|> Result.map (fun x -> x * 2)
|> printf "A result is: %A" 
(*** include-output ***)

but then I added a new Result.someCoolThing function.

#r "nuget: FsToolkit.Errorhandling"
open FsToolkit.ErrorHandling

let test : Result<int,string> = result { return 4 }

test
|> Result.someCoolThing (fun x -> x * 2)
|> printf "A result is: %A" 
(*** include-output ***)

However, since I haven't published the nuget package yet, this wouldn't work. Is there any good tips/guidance on dealing with this problem?


I've looked into Referencing projects with #r: fsproj but I couldn't get this to work. Also with this workaround, I wouldn't necessarily want to show the referencing of the project or dlls, but only the nuget package so people would be easily able to copy snippets from the published docs.

baronfel commented 1 year ago

One thing to do might be to build the docs after you package the local copy. When you build the docs, you can use a nuget.config file that specifies the location of the built package as an additional package source. That would keep you from having to specify the local source in the documentation files themselves, which means that after you publish the package to NuGet.org users could just copy and paste directly.

TheAngryByrd commented 1 year ago

Yeah this seems to work somewhat. The problem here is once you reference a nuget package, you'd have to go clear out your local nuget cache if you have to regenerate the package for any reason. Is there some type of way to get FSI to force reinstall those local packages? (Or maybe not cache to begin with Looks like people have been asking for this for a while.)

Do you foresee any other issues with having some output package directory always in the NuGet.config file?

baronfel commented 1 year ago

If that directory doesn't exist then dotnet restore/FSI will report errors. To get around that I commit a dummy file to the folder, but I also put the folder under gitignore to prevent packages being checked in.

Another thing to do would be to set a different global packages dir to prevent corrupting your global cache. I forget the nuget.config setting for that, but I think it's the NUGET_PACKAGES env var.

nhirschey commented 1 year ago

You can use a hidden (*** condition:prepare ***) section to reference the dlls explicitly.

https://github.com/fsprojects/FSharp.Data/blob/main/docs/library/CsvProvider.fsx

you can then fake a reference to the nuget within a markdown comment block. See also

https://github.com/DiffSharp/DiffSharp/blob/29af24ad69a96aace8a49420996861b2a3ca0fa9/docs/install.fsx#L49

If you use {{fsdocs-package-version}} you can automatically update to latest your nuget version on publication, see the diffsharp docs use of it. I think fsharp data uses that trick too.

Here’s a use of package version https://github.com/fsprojects/FSharp.Data.Toolbox/blob/4cd853c39ffa2131976f9681b4837dd88731cb58/docs/TwitterProvider.fsx#L41

TheAngryByrd commented 1 year ago

Another thing to do would be to set a different global packages dir to prevent corrupting your global cache. I forget the nuget.config setting for that, but I think it's the NUGET_PACKAGES env var.

Yeah this is possible but it's a very weird experience making this work. If you don't set NUGET_PACKAGES before you open your editor, it won't pick up these changes.

If you only set it for the dotnet fsdocs command itself, it doesn't seem to pull from the new cache.

Content:
  generating model for C:\Users\jimmy\Repositories\public\TheAngryByrd\fsdocstster\docsSrc\Tutorials\Tutorial-3.fsx --> .\Tutorials\Tutorial-3.html
   C:\Users\jimmy\Repositories\public\TheAngryByrd\fsdocstster\docsSrc\Tutorials\Tutorial-3.fsx: error(20,17)-(20,20) The value, constructor, namespace or type 'Foo' is not defined.
   C:\Users\jimmy\Repositories\public\TheAngryByrd\fsdocstster\docsSrc\Tutorials\Tutorial-3.fsx: error(0,0)-(0,0) The module/namespace 'Project1.Say' from compilation unit 'Project1' did not contain the namespace, module or type 'Foo'

You can use a hidden ( condition:prepare ) section to reference the dlls explicitly.

https://github.com/fsprojects/FSharp.Data/blob/main/docs/library/CsvProvider.fsx

you can then fake a reference to the nuget within a markdown comment block. See also

This is good to know! Only problem I have here is it becomes difficult to reference a project if it has dependencies. Yeah, you can generate it load references in VSCode but in my experience, the load order isn't correct always.

f you use {{fsdocs-package-version}} you can automatically update to latest your nuget version on publication, see the diffsharp docs use of it. I think fsharp data uses that trick too.

This issue is more about before publication of the nuget package but still valuable to know. Thanks!

TheAngryByrd commented 1 year ago

The package caching wouldn't be so bad if NuGet locals command should allow deleting specific packages from NuGet global packages folder was available also. :(

nhirschey commented 1 year ago

This issue is more about before publication of the nuget package.

Just fyi for future reference, fsdocs takes {{fsdocs-package-version}} from your project or directory build props, not nuget.org. There’s no nuget dependency. It’ll work pre-publication.

nojaf commented 1 year ago

Not sure if this helps but in Fantomas, we switched to local assemblies instead of #r "nuget:: https://github.com/fsprojects/fantomas/blob/v6.0/docs/docs/end-users/Configuration.fsx#L18-L19

kMutagene commented 1 year ago

In Plotly.NET, we use local assembly references as well. If your project has external dependencies, they can be loaded via #r nuget:

https://github.com/plotly/Plotly.NET/blob/1e5fec1496e5b78dc4bb7cad7648a95cefdb975a/docs/02_0_line-scatter-plots.fsx#L12-L16