mirror of
				https://github.com/gohugoio/hugo.git
				synced 2024-05-11 05:54:58 +00:00 
			
		
		
		
	commands: Fix broken server-reload on config changes
This was accidently broken in Hugo 0.42. Fixes #4878
This commit is contained in:
		@@ -37,23 +37,27 @@ import (
 | 
				
			|||||||
	"github.com/gohugoio/hugo/langs"
 | 
						"github.com/gohugoio/hugo/langs"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type commandeer struct {
 | 
					type commandeerHugoState struct {
 | 
				
			||||||
	*deps.DepsCfg
 | 
						*deps.DepsCfg
 | 
				
			||||||
 | 
						hugo     *hugolib.HugoSites
 | 
				
			||||||
 | 
						fsCreate sync.Once
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	hugo *hugolib.HugoSites
 | 
					type commandeer struct {
 | 
				
			||||||
 | 
						*commandeerHugoState
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// We need to reuse this on server rebuilds.
 | 
				
			||||||
 | 
						destinationFs afero.Fs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	h    *hugoBuilderCommon
 | 
						h    *hugoBuilderCommon
 | 
				
			||||||
	ftch flagsToConfigHandler
 | 
						ftch flagsToConfigHandler
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	visitedURLs *types.EvictingStringQueue
 | 
						visitedURLs *types.EvictingStringQueue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// We watch these for changes.
 | 
					 | 
				
			||||||
	configFiles []string
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	doWithCommandeer func(c *commandeer) error
 | 
						doWithCommandeer func(c *commandeer) error
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// We can do this only once.
 | 
						// We watch these for changes.
 | 
				
			||||||
	fsCreate sync.Once
 | 
						configFiles []string
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Used in cases where we get flooded with events in server mode.
 | 
						// Used in cases where we get flooded with events in server mode.
 | 
				
			||||||
	debounce func(f func())
 | 
						debounce func(f func())
 | 
				
			||||||
@@ -73,6 +77,7 @@ func (c *commandeer) Set(key string, value interface{}) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *commandeer) initFs(fs *hugofs.Fs) error {
 | 
					func (c *commandeer) initFs(fs *hugofs.Fs) error {
 | 
				
			||||||
 | 
						c.destinationFs = fs.Destination
 | 
				
			||||||
	c.DepsCfg.Fs = fs
 | 
						c.DepsCfg.Fs = fs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
@@ -89,11 +94,12 @@ func newCommandeer(mustHaveConfigFile, running bool, h *hugoBuilderCommon, f fla
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	c := &commandeer{
 | 
						c := &commandeer{
 | 
				
			||||||
		h:                h,
 | 
							h:                   h,
 | 
				
			||||||
		ftch:             f,
 | 
							ftch:                f,
 | 
				
			||||||
		doWithCommandeer: doWithCommandeer,
 | 
							commandeerHugoState: &commandeerHugoState{},
 | 
				
			||||||
		visitedURLs:      types.NewEvictingStringQueue(10),
 | 
							doWithCommandeer:    doWithCommandeer,
 | 
				
			||||||
		debounce:         rebuildDebouncer,
 | 
							visitedURLs:         types.NewEvictingStringQueue(10),
 | 
				
			||||||
 | 
							debounce:            rebuildDebouncer,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return c, c.loadConfig(mustHaveConfigFile, running)
 | 
						return c, c.loadConfig(mustHaveConfigFile, running)
 | 
				
			||||||
@@ -188,8 +194,11 @@ func (c *commandeer) loadConfig(mustHaveConfigFile, running bool) error {
 | 
				
			|||||||
	c.fsCreate.Do(func() {
 | 
						c.fsCreate.Do(func() {
 | 
				
			||||||
		fs := hugofs.NewFrom(sourceFs, config)
 | 
							fs := hugofs.NewFrom(sourceFs, config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Hugo writes the output to memory instead of the disk.
 | 
							if c.destinationFs != nil {
 | 
				
			||||||
		if createMemFs {
 | 
								// Need to reuse the destination on server rebuilds.
 | 
				
			||||||
 | 
								fs.Destination = c.destinationFs
 | 
				
			||||||
 | 
							} else if createMemFs {
 | 
				
			||||||
 | 
								// Hugo writes the output to memory instead of the disk.
 | 
				
			||||||
			fs.Destination = new(afero.MemMapFs)
 | 
								fs.Destination = new(afero.MemMapFs)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -596,14 +596,6 @@ func (c *commandeer) getDirList() ([]string, error) {
 | 
				
			|||||||
	return a, nil
 | 
						return a, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *commandeer) recreateAndBuildSites(watching bool) (err error) {
 | 
					 | 
				
			||||||
	defer c.timeTrack(time.Now(), "Total")
 | 
					 | 
				
			||||||
	if !c.h.quiet {
 | 
					 | 
				
			||||||
		c.Logger.FEEDBACK.Println("Started building sites ...")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return c.hugo.Build(hugolib.BuildCfg{CreateSitesFromConfig: true})
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (c *commandeer) resetAndBuildSites() (err error) {
 | 
					func (c *commandeer) resetAndBuildSites() (err error) {
 | 
				
			||||||
	if !c.h.quiet {
 | 
						if !c.h.quiet {
 | 
				
			||||||
		c.Logger.FEEDBACK.Println("Started building sites ...")
 | 
							c.Logger.FEEDBACK.Println("Started building sites ...")
 | 
				
			||||||
@@ -637,9 +629,10 @@ func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *commandeer) fullRebuild() {
 | 
					func (c *commandeer) fullRebuild() {
 | 
				
			||||||
 | 
						c.commandeerHugoState = &commandeerHugoState{}
 | 
				
			||||||
	if err := c.loadConfig(true, true); err != nil {
 | 
						if err := c.loadConfig(true, true); err != nil {
 | 
				
			||||||
		jww.ERROR.Println("Failed to reload config:", err)
 | 
							jww.ERROR.Println("Failed to reload config:", err)
 | 
				
			||||||
	} else if err := c.recreateAndBuildSites(true); err != nil {
 | 
						} else if err := c.buildSites(); err != nil {
 | 
				
			||||||
		jww.ERROR.Println(err)
 | 
							jww.ERROR.Println(err)
 | 
				
			||||||
	} else if !c.h.buildWatch && !c.Cfg.GetBool("disableLiveReload") {
 | 
						} else if !c.h.buildWatch && !c.Cfg.GetBool("disableLiveReload") {
 | 
				
			||||||
		livereload.ForceRefresh()
 | 
							livereload.ForceRefresh()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -298,7 +298,7 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, string, string, erro
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	httpFs := afero.NewHttpFs(f.c.Fs.Destination)
 | 
						httpFs := afero.NewHttpFs(f.c.destinationFs)
 | 
				
			||||||
	fs := filesOnlyFs{httpFs.Dir(absPublishDir)}
 | 
						fs := filesOnlyFs{httpFs.Dir(absPublishDir)}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	doLiveReload := !f.s.buildWatch && !f.c.Cfg.GetBool("disableLiveReload")
 | 
						doLiveReload := !f.s.buildWatch && !f.c.Cfg.GetBool("disableLiveReload")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user