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

Guidance on literate programming #804

Open
TheAngryByrd opened this issue Mar 4, 2023 · 9 comments
Open

Guidance on literate programming #804

TheAngryByrd opened this issue Mar 4, 2023 · 9 comments

Comments

@TheAngryByrd
Copy link

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
Copy link
Collaborator

baronfel commented Mar 4, 2023

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
Copy link
Author

TheAngryByrd commented Mar 4, 2023

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
Copy link
Collaborator

baronfel commented Mar 4, 2023

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
Copy link
Collaborator

nhirschey commented Mar 4, 2023

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
Copy link
Author

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Collaborator

nojaf commented Mar 6, 2023

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
Copy link
Contributor

kMutagene commented Mar 7, 2023

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

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

5 participants