Skip to content

Commit 4e524ff

Browse files
committed
commands: Fix server without watch
This was broken in Hugo 0.30. Fixes #4275
1 parent 64f0e9d commit 4e524ff

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

‎commands/hugo.go‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"path/filepath"
3131
"runtime"
3232
"strings"
33-
"sync"
3433
"time"
3534

3635
src "github.com/gohugoio/hugo/source"
@@ -633,7 +632,7 @@ func (c *commandeer) build(watches ...bool) error {
633632
}
634633
c.Logger.FEEDBACK.Println("Watching for changes in", c.PathSpec().AbsPathify(c.Cfg.GetString("contentDir")))
635634
c.Logger.FEEDBACK.Println("Press Ctrl+C to stop")
636-
utils.CheckErr(c.Logger, c.newWatcher(false, watchDirs...))
635+
utils.CheckErr(c.Logger, c.newWatcher(watchDirs...))
637636
}
638637

639638
return nil
@@ -969,9 +968,7 @@ func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
969968
}
970969

971970
// newWatcher creates a new watcher to watch filesystem events.
972-
// if serve is set it will also start one or more HTTP servers to serve those
973-
// files.
974-
func (c *commandeer) newWatcher(serve bool, dirList ...string) error {
971+
func (c *commandeer) newWatcher(dirList ...string) error {
975972
if runtime.GOOS == "darwin" {
976973
tweakLimit()
977974
}
@@ -982,16 +979,13 @@ func (c *commandeer) newWatcher(serve bool, dirList ...string) error {
982979
}
983980

984981
watcher, err := watcher.New(1 * time.Second)
985-
var wg sync.WaitGroup
986982

987983
if err != nil {
988984
return err
989985
}
990986

991987
defer watcher.Close()
992988

993-
wg.Add(1)
994-
995989
for _, d := range dirList {
996990
if d != "" {
997991
_ = watcher.Add(d)
@@ -1189,11 +1183,6 @@ func (c *commandeer) newWatcher(serve bool, dirList ...string) error {
11891183
}
11901184
}()
11911185

1192-
if serve {
1193-
go c.serve()
1194-
}
1195-
1196-
wg.Wait()
11971186
return nil
11981187
}
11991188

‎commands/server.go‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ import (
1919
"net/http"
2020
"net/url"
2121
"os"
22+
"os/signal"
2223
"path/filepath"
2324
"runtime"
2425
"strconv"
2526
"strings"
27+
"syscall"
2628
"time"
2729

2830
"github.com/gohugoio/hugo/livereload"
@@ -229,14 +231,16 @@ func server(cmd *cobra.Command, args []string) error {
229231
rootWatchDirs := strings.Join(helpers.UniqueStrings(helpers.ExtractRootPaths(relWatchDirs)), ",")
230232

231233
jww.FEEDBACK.Printf("Watching for changes in %s%s{%s}\n", baseWatchDir, helpers.FilePathSeparator, rootWatchDirs)
232-
err = c.newWatcher(true, watchDirs...)
234+
err = c.newWatcher(watchDirs...)
233235

234236
if err != nil {
235237
return err
236238
}
239+
237240
}
238241

239-
return nil
242+
return c.serve()
243+
240244
}
241245

242246
type fileServer struct {
@@ -313,7 +317,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
313317
return mu, u.String(), endpoint, nil
314318
}
315319

316-
func (c *commandeer) serve() {
320+
func (c *commandeer) serve() error {
317321

318322
isMultiHost := Hugo.IsMultihost()
319323

@@ -345,6 +349,9 @@ func (c *commandeer) serve() {
345349
livereload.Initialize()
346350
}
347351

352+
var sigs = make(chan os.Signal)
353+
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
354+
348355
for i, _ := range baseURLs {
349356
mu, serverURL, endpoint, err := srv.createEndpoint(i)
350357

@@ -363,6 +370,10 @@ func (c *commandeer) serve() {
363370
}
364371

365372
jww.FEEDBACK.Println("Press Ctrl+C to stop")
373+
374+
<-sigs
375+
376+
return nil
366377
}
367378

368379
// fixURL massages the baseURL into a form needed for serving

0 commit comments

Comments
 (0)