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

Remove root parameter override in watch mode #925

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

tymokvo
Copy link

@tymokvo tymokvo commented Jun 26, 2024

Summary

  • Add optional watch flag for injecting a URL root into static site content for non-localhost browsers
  • Add case in if watch to replace root override with the passed URL root
  • Update injected websocket script to change request protocol based on window.location

Description

This adds the ability to serve the generated documentation site to browsers not running on localhost. For example, when working in a remote development environment like GitHub Codespaces or an ngrok endpoint.

@tymokvo tymokvo marked this pull request as ready for review June 26, 2024 03:44
Copy link
Collaborator

@nojaf nojaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these changes are too disruptive for any existing scenarios.
I think most people don't pass root as parameter and that should still work as everybody expects it.

build.fsx Outdated Show resolved Hide resolved
| _ -> None

r, userParameters
let userRoot =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will clash later on with

let root =
let projectUrl = projectInfoForDocs.PackageProjectUrl |> Option.map ensureTrailingSlash
defaultArg userRoot (defaultArg projectUrl ("/" + collectionName) |> ensureTrailingSlash)

Just following the instructions in the readme (dotnet build and src\fsdocs-tool\bin\Debug\net6.0\fsdocs.exe watch) will launch everything pointing to https://fsprojects.github.io/FSharp.Formatting and not localhost.

Copy link
Collaborator

@nojaf nojaf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is heading in the right direction but I do have some remarks.

[<Option("contenthost", Required = false, HelpText = "URI root to inject in static content.")>]
member val contenthost = "" with get, set

override x.static_content_host_option =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code violates some analyzers:

image

Run dotnet fsi build.fsx -- -p Verify for more info.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip with -p Verify! That's helpful.

Though the output is quite verbose with results from the other projects. It also seems to create diffs in the tests/ directory for me. Not sure if that's intentional.

@@ -2115,3 +2119,13 @@ type WatchCommand() =

[<Option("port", Required = false, Default = 8901, HelpText = "Port to serve content for http://localhost serving.")>]
member val port = 8901 with get, set

[<Option("contenthost", Required = false, HelpText = "URI root to inject in static content.")>]
member val contenthost = "" with get, set
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add documentation for this new setting.
I'm not quite convinced about the flag name and help text.
I also have some slight concerns that invalid input won't be displayed nicely to the end-users.
If you value for this is blah, Suave might trip over it.
It is also not quite clear if http:// or https:// needs to be included in this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say:

Please add documentation for this new setting.

Do you mean just in the HelpText? Or is there somewhere else that I should be adding a more verbose description?

And good point about the invalid input. I added a call to System.Uri in the override to get its built-in parse errors. Do you think that is helpful enough?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Could you take a look if we can show something friendlier than this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah got it! And yes I will look into friendly-ifying the URI error.

@nojaf
Copy link
Collaborator

nojaf commented Jul 1, 2024

Running src\fsdocs-tool\bin\Debug\net6.0\fsdocs.exe watch --contenturlroot http://localhost:5001 --port 6001 is not quite working for me. I think we should not allow both settings at the same time.

launching browser window to open http://localhost:6001/
[09:48:03 INF] Smooth! Suave listener started in 8.653ms with binding 127.0.0.1:6001

but leads to

image

This also makes me wonder if we should go for a breaking change and introduce a --root flag instead? This would capture both the existing port and new contenturlroot flag.

Thoughts @kMutagene @nhirschey?

@tymokvo
Copy link
Author

tymokvo commented Jul 1, 2024

Yes, I noticed that those parameters could become out of sync. But isn't it possible that both --port and --contenturlroot may be desired?

E.g.

  • dev A is pairing with dev B on a branch
  • They want to see how their changes affect the docs
  • dev A starts the watch server bound to localhost:6001 (overriding the default of 8901)
  • dev A opens an ngrok tunnel from dev-a.ngrok.io to localhost:6001
  • dev B navigates to dev-a.ngrok.io and hits the suave server

In this case, the static content should all replace root with http://dev-a.ngrok.io/ and the suave server should still bind on 6001, correct?

@nojaf
Copy link
Collaborator

nojaf commented Jul 2, 2024

In this case, the static content should all replace root with http://dev-a.ngrok.io/ and the suave server should still bind on 6001, correct?

Hmm, yes, that is a plausible scenario. However, it does make me wonder if we should not bet more on making relative paths a thing?

@tymokvo
Copy link
Author

tymokvo commented Jul 2, 2024

Yes, that would also work, I think.

I am not very familiar with Suave, but I have used the rust and python abstractions for "just start an HTTP file server in this directory" before. I assume Suave has something similar or it's easy enough to create.

@nojaf
Copy link
Collaborator

nojaf commented Jul 3, 2024

I don't think this would be very Suave related. The bigger issues is that {{root}} is used all over the place and would need to transformed to the proper relative url.

@nhirschey
Copy link
Collaborator

Thoughts kMutagene nhirschey?

It seems complicated to document or explain contentroot, root, and port simultaneously. It's a bit of an edge case that anybody would use these together, and maybe a suboptimal edge case is ok, but still a bit of a hack.

If I understand the problem correctly, a single --root and then relative paths seems the cleanest way forward if it's possible. More work to get it right, but perhaps a better long-term foundation. I don't know why it's absolute rather than relative paths now.

@kMutagene
Copy link
Contributor

Agreed with everything that has been said so far, but moving PRs to the 'More work to get it right, but perhaps a better long-term foundation' solution tends to stall PRs such as this - see my still-open PR of shame on this repo.

@tymokvo would you be up for escalating the scope of this PR that much? Otherwise i'd suggest publishing a 'good enough' solution only in preview versions, and another maintainer/contributor picking up from there in future PRs.

@tymokvo
Copy link
Author

tymokvo commented Jul 5, 2024

I would be happy to contribute a better long-term solution! But I was hoping to be able to get this merge-ready before the deadline I have at work now. So, I wouldn't be able to resume this for a couple weeks.

As long as you all are ok with this PR going stale for that amount of time, then I can revisit.

And, just so that I'm clear, the direction would be:

  • Support a single root flag to declare the URL root
  • Remove the existing override in the watch --root case
  • Update the path generation in web content to all use relative paths over absolute paths to {{root}}

...correct?

My main concern would be the risk of breaking changes since the scope would be so broad and would affect any project that depends on FSharp.Formatting.

@nojaf
Copy link
Collaborator

nojaf commented Jul 8, 2024

Hi @tymokvo, thanks for the offer. We have a call with the maintainers tomorrow, I'll bring it up over there.

@@ -1406,10 +1404,13 @@ type CoreBuildOptions(watch) =
// Adjust the user substitutions for 'watch' mode root
let userRoot, userParameters =
if watch then
let userRoot = sprintf "http://localhost:%d/" this.port_option
let userRoot =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would

let userRoot =
    match userParametersDict.TryGetValue(ParamKeys.root) with
    | true, root -> root
    | false, _ -> sprintf "http://localhost:%d/" this.port_option

work for you here?
I believe it can be an easy way to make

fsdocs watch --parameters root "/"                                                                                                                                                                               

work and would be the least disruptive for now.

We don't feel good enough about the new setting.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will test and let you know! Thanks for talking it through!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, thanks for your patience!

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.

4 participants