fsprojects / FSharp.Formatting

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

Using testcontainers in a fsx script gives wrong output #897

Open rbauduin opened 5 months ago

rbauduin commented 5 months ago

Putting this fsx in the docs directory:

> (**
# Usage
*)
(*** hide ***)

#r "nuget: Testcontainers.PostgreSql, 3.6.0"
open Testcontainers.PostgreSql
task {
(** # Start container *)
  let container =
    (new PostgreSqlBuilder())
          .WithImage("postgres:16")
          .Build();
  do! container.StartAsync()
  printfn "%s" (container.GetConnectionString())
(** But let's not forget to stop it with *)
  do! container.DisposeAsync().AsTask()
(*** hide ***)
}
|> Async.AwaitTask |> Async.RunSynchronously

and then running fsdocs build gives a page with content about test containers: image

It can be reproduced on linux with these commands:

:/tmp$ dotnet new console --language "F#" -n fsdocdemo
:/tmp$ cd fsdocdemo/
:/tmp/fsdocdemo$ mkdir docs
:/tmp/fsdocdemo$ cat >docs/minimal.fsx <<EOF
> (**
# Usage
*)
(*** hide ***)

#r "nuget: Testcontainers.PostgreSql, 3.6.0"
open Testcontainers.PostgreSql
task {
(** # Start container *)
  let container =
    (new PostgreSqlBuilder())
          .WithImage("postgres:16")
          .Build();
  do! container.StartAsync()
  printfn "%s" (container.GetConnectionString())
(** But let's not forget to stop it with *)
  do! container.DisposeAsync().AsTask()
(*** hide ***)
}
|> Async.AwaitTask |> Async.RunSynchronously
EOF
:/tmp/fsdocdemo$ fsdocs build
:/tmp/fsdocdemo$ cd output
:/tmp/fsdocdemo$ python -m http.server

and access http://localhost:8000/minimal.html

Some more information:

 $ dotnet --version
6.0.418
$ fsdocs --version
fsdocs 19.1.1
$ uname -r
6.2.0-37-generic
nojaf commented 4 months ago

Hello,

This worked for me: image

Are you using a custom _template.html maybe?

rbauduin commented 4 months ago

Thanks for having a look. I have the same behaviour when running in a docker container. There's some content that shouldn't be there. And it's no due to test containers, just running a simple task has the problem. Here's a Dockerfile reproducing the problem:

FROM mcr.microsoft.com/dotnet/sdk

RUN apt-get update && apt-get install -y python3
RUN dotnet tool install --global fsdocs-tool --version 19.1.1
RUN dotnet new console --language "F#" -n fsdocdemo
RUN cd fsdocdemo/
RUN mkdir docs
RUN cat >docs/minimal.fsx <<EOF
(**
# Usage
*)
(*** hide ***)
task {
 return ()
}
|> Async.AwaitTask |> Async.RunSynchronously
EOF

RUN ~/.dotnet/tools/fsdocs build
WORKDIR output
CMD python3 -m http.server

Building the image and running it with this command:

image=$(docker build -q .) ; docker run --rm -it -p8000:8000 $image

I can access the result on http://localhost:8000/minimal.html and get this:

image

nojaf commented 4 months ago

Could you try --version 20.0.0-beta-002?

rbauduin commented 4 months ago

I tried with --version 20.0.0-beta-002 but also get additional content :-( Some CSS seemed to be missing as layout was wrong.

nojaf commented 4 months ago

Oh, I see what you are doing now. You will need to provide a root for your scenario I believe. The main problem is that it will not generate the proper links to the .js and .css files. Try adding --parameters root ./ (Or maybe --parameters root / even)

rbauduin commented 4 months ago

Adding the value for the root parameter fixes it, thanks @nojaf !

After experimenting a bit, I see what I was doing 'wrong'. However, I don't understand the default value of the root parameter, which leads to paths like /$projectName/content/*.css while it is actually generated at /$projectName/output/content/*.css.

Not a big deal as it's working for me, just curious ;-)

Just for future readers, here's the fixed Dockerfile (including fixing the change of work directory). But note that in the end I went for a root parameter / and running the http server inside the output directory. Using / as root parameter allows content generated in nested directories to also be styled correctly.

FROM mcr.microsoft.com/dotnet/sdk

RUN apt-get update && apt-get install -y python3
RUN dotnet tool install --global fsdocs-tool --version 20.0.0-beta-002
RUN dotnet new console --language "F#" -n fsdocsdemo
WORKDIR fsdocsdemo
RUN mkdir docs
RUN cat >docs/minimal.fsx <<EOF
(**
# Usage
*)
(*** hide ***)
#r "nuget: Testcontainers.PostgreSql, 3.6.0"
open Testcontainers.PostgreSql
task {
(** # Start container *)
  let container =
    (new PostgreSqlBuilder())
          .WithImage("postgres:16")
          .Build();
  do! container.StartAsync()
  printfn "%s" (container.GetConnectionString())
(** But let's not forget to stop it with *)
  do! container.DisposeAsync().AsTask()
(*** hide ***)

}
|> Async.AwaitTask |> Async.RunSynchronously
EOF
RUN ~/.dotnet/tools/fsdocs build --parameters root ./
CMD python3 -m http.server
nojaf commented 4 months ago

I agree that the default value is a bit weird here. We should document the root parameter workaround.