shithub: hugo

Download patch

ref: ea9261e856c13c1d4ae05fcca08766d410b4b65c
parent: 37f592980315de1890363d4234274a16567a6da0
author: HyeonGyu Lee <vazrupe@naver.com>
date: Thu Aug 15 12:33:47 EDT 2019

commands: Make sure the hugo field is always initialized before it's used

Wrap the field to make it accessible after initialization.

Fixes #6193

--- a/commands/commandeer.go
+++ b/commands/commandeer.go
@@ -52,8 +52,9 @@
 
 type commandeerHugoState struct {
 	*deps.DepsCfg
-	hugo     *hugolib.HugoSites
-	fsCreate sync.Once
+	hugoSites *hugolib.HugoSites
+	fsCreate  sync.Once
+	created   chan struct{}
 }
 
 type commandeer struct {
@@ -97,6 +98,17 @@
 	buildErr error
 }
 
+func newCommandeerHugoState() *commandeerHugoState {
+	return &commandeerHugoState{
+		created: make(chan struct{}),
+	}
+}
+
+func (c *commandeerHugoState) hugo() *hugolib.HugoSites {
+	<-c.created
+	return c.hugoSites
+}
+
 func (c *commandeer) errCount() int {
 	return int(c.logger.ErrorCounter.Count())
 }
@@ -154,7 +166,7 @@
 	c := &commandeer{
 		h:                   h,
 		ftch:                f,
-		commandeerHugoState: &commandeerHugoState{},
+		commandeerHugoState: newCommandeerHugoState(),
 		doWithCommandeer:    doWithCommandeer,
 		visitedURLs:         types.NewEvictingStringQueue(10),
 		debounce:            rebuildDebouncer,
@@ -373,6 +385,7 @@
 
 		err = c.initFs(fs)
 		if err != nil {
+			close(c.created)
 			return
 		}
 
@@ -379,7 +392,8 @@
 		var h *hugolib.HugoSites
 
 		h, err = hugolib.NewHugoSites(*c.DepsCfg)
-		c.hugo = h
+		c.hugoSites = h
+		close(c.created)
 
 	})
 
--- a/commands/deploy.go
+++ b/commands/deploy.go
@@ -58,7 +58,7 @@
 			if err != nil {
 				return err
 			}
-			deployer, err := deploy.New(comm.Cfg, comm.hugo.PathSpec.PublishFs)
+			deployer, err := deploy.New(comm.Cfg, comm.hugo().PathSpec.PublishFs)
 			if err != nil {
 				return err
 			}
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -92,7 +92,7 @@
 
 	if c == cmd && hugoCmd.c != nil {
 		// Root command executed
-		resp.Result = hugoCmd.c.hugo
+		resp.Result = hugoCmd.c.hugo()
 	}
 
 	if err == nil {
@@ -338,16 +338,16 @@
 		}
 	}
 
-	for _, s := range c.hugo.Sites {
+	for _, s := range c.hugo().Sites {
 		s.ProcessingStats.Static = langCount[s.Language().Lang]
 	}
 
 	if c.h.gc {
-		count, err := c.hugo.GC()
+		count, err := c.hugo().GC()
 		if err != nil {
 			return err
 		}
-		for _, s := range c.hugo.Sites {
+		for _, s := range c.hugo().Sites {
 			// We have no way of knowing what site the garbage belonged to.
 			s.ProcessingStats.Cleaned = uint64(count)
 		}
@@ -483,7 +483,7 @@
 	// TODO(bep) Feedback?
 	if !c.h.quiet {
 		fmt.Println()
-		c.hugo.PrintProcessingStats(os.Stdout)
+		c.hugo().PrintProcessingStats(os.Stdout)
 		fmt.Println()
 
 		if createCounter, ok := c.destinationFs.(hugofs.DuplicatesReporter); ok {
@@ -539,7 +539,7 @@
 	// TODO(bep) Feedback?
 	if !c.h.quiet {
 		fmt.Println()
-		c.hugo.PrintProcessingStats(os.Stdout)
+		c.hugo().PrintProcessingStats(os.Stdout)
 		fmt.Println()
 	}
 
@@ -558,7 +558,7 @@
 
 	langCount := make(map[string]uint64)
 
-	staticFilesystems := c.hugo.BaseFs.SourceFilesystems.Static
+	staticFilesystems := c.hugo().BaseFs.SourceFilesystems.Static
 
 	if len(staticFilesystems) == 0 {
 		c.logger.INFO.Println("No static directories found to sync")
@@ -610,7 +610,7 @@
 }
 
 func (c *commandeer) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint64, error) {
-	publishDir := c.hugo.PathSpec.PublishDir
+	publishDir := c.hugo().PathSpec.PublishDir
 	// If root, remove the second '/'
 	if publishDir == "//" {
 		publishDir = helpers.FilePathSeparator
@@ -655,7 +655,7 @@
 }
 
 func (c *commandeer) firstPathSpec() *helpers.PathSpec {
-	return c.hugo.Sites[0].PathSpec
+	return c.hugo().Sites[0].PathSpec
 }
 
 func (c *commandeer) timeTrack(start time.Time, name string) {
@@ -689,7 +689,7 @@
 
 	}
 
-	watchDirs := c.hugo.PathSpec.BaseFs.WatchDirs()
+	watchDirs := c.hugo().PathSpec.BaseFs.WatchDirs()
 	for _, watchDir := range watchDirs {
 
 		w := hugofs.NewWalkway(hugofs.WalkwayConfig{Logger: c.logger, Info: watchDir, WalkFn: walkFn})
@@ -704,7 +704,7 @@
 }
 
 func (c *commandeer) buildSites() (err error) {
-	return c.hugo.Build(hugolib.BuildCfg{})
+	return c.hugo().Build(hugolib.BuildCfg{})
 }
 
 func (c *commandeer) handleBuildErr(err error, msg string) {
@@ -726,16 +726,16 @@
 
 		// Make sure we always render the home pages
 		for _, l := range c.languages {
-			langPath := c.hugo.PathSpec.GetLangSubDir(l.Lang)
+			langPath := c.hugo().PathSpec.GetLangSubDir(l.Lang)
 			if langPath != "" {
 				langPath = langPath + "/"
 			}
-			home := c.hugo.PathSpec.PrependBasePath("/"+langPath, false)
+			home := c.hugo().PathSpec.PrependBasePath("/"+langPath, false)
 			visited[home] = true
 		}
 
 	}
-	return c.hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited}, events...)
+	return c.hugo().Build(hugolib.BuildCfg{RecentlyVisited: visited}, events...)
 }
 
 func (c *commandeer) partialReRender(urls ...string) error {
@@ -744,7 +744,7 @@
 	for _, url := range urls {
 		visited[url] = true
 	}
-	return c.hugo.Build(hugolib.BuildCfg{RecentlyVisited: visited, PartialReRender: true})
+	return c.hugo().Build(hugolib.BuildCfg{RecentlyVisited: visited, PartialReRender: true})
 }
 
 func (c *commandeer) fullRebuild(changeType string) {
@@ -775,7 +775,7 @@
 
 		defer c.timeTrack(time.Now(), "Total")
 
-		c.commandeerHugoState = &commandeerHugoState{}
+		c.commandeerHugoState = newCommandeerHugoState()
 		err := c.loadConfig(true, true)
 		if err != nil {
 			// Set the processing on pause until the state is recovered.
@@ -951,7 +951,7 @@
 	filtered := []fsnotify.Event{}
 	for _, ev := range evs {
 		// Check the most specific first, i.e. files.
-		contentMapped := c.hugo.ContentChanges.GetSymbolicLinkMappings(ev.Name)
+		contentMapped := c.hugo().ContentChanges.GetSymbolicLinkMappings(ev.Name)
 		if len(contentMapped) > 0 {
 			for _, mapped := range contentMapped {
 				filtered = append(filtered, fsnotify.Event{Name: mapped, Op: ev.Op})
@@ -963,7 +963,7 @@
 
 		dir, name := filepath.Split(ev.Name)
 
-		contentMapped = c.hugo.ContentChanges.GetSymbolicLinkMappings(dir)
+		contentMapped = c.hugo().ContentChanges.GetSymbolicLinkMappings(dir)
 
 		if len(contentMapped) == 0 {
 			filtered = append(filtered, ev)
@@ -997,7 +997,7 @@
 		if istemp {
 			continue
 		}
-		if c.hugo.Deps.SourceSpec.IgnoreFile(ev.Name) {
+		if c.hugo().Deps.SourceSpec.IgnoreFile(ev.Name) {
 			continue
 		}
 		// Sometimes during rm -rf operations a '"": REMOVE' is triggered. Just ignore these
@@ -1073,7 +1073,7 @@
 			// force refresh when more than one file
 			if len(staticEvents) == 1 {
 				ev := staticEvents[0]
-				path := c.hugo.BaseFs.SourceFilesystems.MakeStaticPathRelative(ev.Name)
+				path := c.hugo().BaseFs.SourceFilesystems.MakeStaticPathRelative(ev.Name)
 				path = c.firstPathSpec().RelURL(helpers.ToSlashTrimLeading(path), false)
 				livereload.RefreshPath(path)
 			} else {
@@ -1119,7 +1119,7 @@
 
 				if navigate {
 					if onePageName != "" {
-						p = c.hugo.GetContentPage(onePageName)
+						p = c.hugo().GetContentPage(onePageName)
 					}
 				}
 
--- a/commands/mod.go
+++ b/commands/mod.go
@@ -157,7 +157,7 @@
 					return err
 				}
 
-				_, err = com.hugo.FileCaches.ModulesCache().Prune(true)
+				_, err = com.hugo().FileCaches.ModulesCache().Prune(true)
 				return err
 
 			},
@@ -176,7 +176,7 @@
 		return err
 	}
 
-	return f(com.hugo.ModulesClient)
+	return f(com.hugo().ModulesClient)
 }
 
 func (c *modCmd) initConfig(failOnNoConfig bool) (*commandeer, error) {
--- a/commands/new.go
+++ b/commands/new.go
@@ -85,13 +85,13 @@
 
 	var kind string
 
-	createPath, kind = newContentPathSection(c.hugo, createPath)
+	createPath, kind = newContentPathSection(c.hugo(), createPath)
 
 	if n.contentType != "" {
 		kind = n.contentType
 	}
 
-	return create.NewContent(c.hugo, kind, createPath)
+	return create.NewContent(c.hugo(), kind, createPath)
 }
 
 func mkdir(x ...string) {
--- a/commands/new_theme.go
+++ b/commands/new_theme.go
@@ -63,7 +63,7 @@
 		return newUserError("theme name needs to be provided")
 	}
 
-	createpath := c.hugo.PathSpec.AbsPathify(filepath.Join(c.Cfg.GetString("themesDir"), args[0]))
+	createpath := c.hugo().PathSpec.AbsPathify(filepath.Join(c.Cfg.GetString("themesDir"), args[0]))
 	jww.FEEDBACK.Println("Creating theme at", createpath)
 
 	cfg := c.DepsCfg
--- a/commands/server.go
+++ b/commands/server.go
@@ -244,7 +244,7 @@
 		return err
 	}
 
-	for _, s := range c.hugo.Sites {
+	for _, s := range c.hugo().Sites {
 		s.RegisterMediaTypes()
 	}
 
@@ -303,9 +303,9 @@
 		publishDir = filepath.Join(publishDir, root)
 	}
 
-	absPublishDir := f.c.hugo.PathSpec.AbsPathify(publishDir)
+	absPublishDir := f.c.hugo().PathSpec.AbsPathify(publishDir)
 
-	jww.FEEDBACK.Printf("Environment: %q", f.c.hugo.Deps.Site.Hugo().Environment)
+	jww.FEEDBACK.Printf("Environment: %q", f.c.hugo().Deps.Site.Hugo().Environment)
 
 	if i == 0 {
 		if f.s.renderToDisk {
@@ -398,7 +398,7 @@
 }
 func (c *commandeer) serve(s *serverCmd) error {
 
-	isMultiHost := c.hugo.IsMultihost()
+	isMultiHost := c.hugo().IsMultihost()
 
 	var (
 		baseURLs []string
@@ -406,17 +406,17 @@
 	)
 
 	if isMultiHost {
-		for _, s := range c.hugo.Sites {
+		for _, s := range c.hugo().Sites {
 			baseURLs = append(baseURLs, s.BaseURL.String())
 			roots = append(roots, s.Language().Lang)
 		}
 	} else {
-		s := c.hugo.Sites[0]
+		s := c.hugo().Sites[0]
 		baseURLs = []string{s.BaseURL.String()}
 		roots = []string{""}
 	}
 
-	templ, err := c.hugo.TextTmpl.Parse("__default_server_error", buildErrorTemplate)
+	templ, err := c.hugo().TextTmpl.Parse("__default_server_error", buildErrorTemplate)
 	if err != nil {
 		return err
 	}
--- a/commands/static_syncer.go
+++ b/commands/static_syncer.go
@@ -33,7 +33,7 @@
 }
 
 func (s *staticSyncer) isStatic(filename string) bool {
-	return s.c.hugo.BaseFs.SourceFilesystems.IsStatic(filename)
+	return s.c.hugo().BaseFs.SourceFilesystems.IsStatic(filename)
 }
 
 func (s *staticSyncer) syncsStaticEvents(staticEvents []fsnotify.Event) error {
@@ -40,7 +40,7 @@
 	c := s.c
 
 	syncFn := func(sourceFs *filesystems.SourceFilesystem) (uint64, error) {
-		publishDir := c.hugo.PathSpec.PublishDir
+		publishDir := c.hugo().PathSpec.PublishDir
 		// If root, remove the second '/'
 		if publishDir == "//" {
 			publishDir = helpers.FilePathSeparator