getnikola / nikola

A static website and blog generator
https://getnikola.com/
MIT License
2.62k stars 448 forks source link

"nikola serve -b" hangs when a terminal browser is used #3536

Closed ChrisJefferson closed 3 years ago

ChrisJefferson commented 3 years ago

Environment

Python Version: 3.8.5 niko Nikola Version: 8.1.3 Operating System: Linux WSL2 in Windows

Description:

I am using WSL2 (Linux emulation) in Windows. I think the only important part of this is that this means my Linux cannot open GUI applications.

nikola serve -b seems to hang. While it opens a web browser (lynx in my case), it never loads the page. If I run nikola serve, then run lynx in another window, it works fine.

I think the problem is described in the webbrowser documentation: https://docs.python.org/3/library/webbrowser.html -- when a terminal web browser is opened, the original application is paused until the browser finishes. This means nikola can't actually serve anything!

Annoyingly, there seems to be no way to know if webbrowser is going to halt the program, or tell it not to.

One possible fix would be to just fork before calling webbrowser, then letting that forked python exit, but there might be some issue with that I am not thinking of?

Kwpolska commented 3 years ago

You probably don’t want to open it in a text browser. You want this:

BROWSER='/mnt/c/Program Files/Mozilla Firefox/firefox.exe' nikola serve -b

Or you could use something like my open.exe tool. Whichever way you choose, this will launch a graphical browser on the Windows side and will Just Work™ thanks to the ability to run .exes in WSL2 and automatic port forwarding.

I'm reluctant to add a special case for text browsers, since that would also require knowing it’s a text browser and silencing the logs. Users of text browsers can use their favorite way of getting two terminals (new tab/window, tmux, screen) or nikola serve -d; elinks http://localhost:8000/.

ChrisJefferson commented 3 years ago

So, I agree I don't want it in a text browser, I have my own "open.exe" like script, and as you say I can use just nikola serve, then open it in a web browser myself.

However, the guidance at https://www.getnikola.com/handbook.html says do"init, new_page, build, serve -b", then says I shouldn't need to read the rest of the manual. Ecept, I believe on any default WSL2 install what that actually does is just hangs. If WSL2 gets more popular, more people might hit this hanging bug.

I'll be honest, this was my very first experience of using nikola, and I very nearly left at that point.

ChrisJefferson commented 3 years ago

I may have poorly explained my suggested fix, I'm going to make a PR for it, just so you can see it.