Skip to content

Commit

Permalink
Fix some recently introduced error handling issues
Browse files Browse the repository at this point in the history
Updates #10953
  • Loading branch information
bep committed May 18, 2023
1 parent 1155bbc commit 834b3d7
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 37 deletions.
3 changes: 0 additions & 3 deletions commands/commandeer.go
Expand Up @@ -338,9 +338,6 @@ func (r *rootCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, args
defer r.timeTrack(time.Now(), "Built")
}
err := b.build()
if err != nil {
r.Println("Error:", err.Error())
}
return err
}()

Expand Down
82 changes: 59 additions & 23 deletions commands/hugobuilder.go
Expand Up @@ -153,7 +153,11 @@ func (c *hugoBuilder) getDirList() ([]string, error) {
return nil
}

watchFiles := c.hugo().PathSpec.BaseFs.WatchDirs()
h, err := c.hugo()
if err != nil {
return nil, err
}
watchFiles := h.PathSpec.BaseFs.WatchDirs()
for _, fi := range watchFiles {
if !fi.IsDir() {
filenames = append(filenames, fi.Meta().Filename)
Expand Down Expand Up @@ -334,7 +338,11 @@ func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*wa
return nil, err
}

spec := c.hugo().Deps.SourceSpec
h, err := c.hugo()
if err != nil {
return nil, err
}
spec := h.Deps.SourceSpec

for _, d := range dirList {
if d != "" {
Expand All @@ -359,7 +367,7 @@ func (c *hugoBuilder) newWatcher(pollIntervalStr string, dirList ...string) (*wa
for {
select {
case evs := <-watcher.Events:
unlock, err := c.hugo().LockBuild()
unlock, err := h.LockBuild()
if err != nil {
c.r.logger.Errorln("Failed to acquire a build lock: %s", err)
return
Expand Down Expand Up @@ -399,15 +407,23 @@ func (c *hugoBuilder) build() error {

if !c.r.quiet {
c.r.Println()
c.hugo().PrintProcessingStats(os.Stdout)
h, err := c.hugo()
if err != nil {
return err
}
h.PrintProcessingStats(os.Stdout)
c.r.Println()
}

return nil
}

func (c *hugoBuilder) buildSites(noBuildLock bool) (err error) {
return c.hugo().Build(hugolib.BuildCfg{NoBuildLock: noBuildLock})
h, err := c.hugo()
if err != nil {
return err
}
return h.Build(hugolib.BuildCfg{NoBuildLock: noBuildLock})
}

func (c *hugoBuilder) copyStatic() (map[string]uint64, error) {
Expand Down Expand Up @@ -462,7 +478,11 @@ func (c *hugoBuilder) copyStaticTo(sourceFs *filesystems.SourceFilesystem) (uint
func (c *hugoBuilder) doWithPublishDirs(f func(sourceFs *filesystems.SourceFilesystem) (uint64, error)) (map[string]uint64, error) {
langCount := make(map[string]uint64)

staticFilesystems := c.hugo().BaseFs.SourceFilesystems.Static
h, err := c.hugo()
if err != nil {
return nil, err
}
staticFilesystems := h.BaseFs.SourceFilesystems.Static

if len(staticFilesystems) == 0 {
c.r.logger.Infoln("No static directories found to sync")
Expand Down Expand Up @@ -535,16 +555,20 @@ func (c *hugoBuilder) fullBuild(noBuildLock bool) error {
}
}

for _, s := range c.hugo().Sites {
h, err := c.hugo()
if err != nil {
return err
}
for _, s := range h.Sites {
s.ProcessingStats.Static = langCount[s.Language().Lang]
}

if c.r.gc {
count, err := c.hugo().GC()
count, err := h.GC()
if err != nil {
return err
}
for _, s := range c.hugo().Sites {
for _, s := range h.Sites {
// We have no way of knowing what site the garbage belonged to.
s.ProcessingStats.Cleaned = uint64(count)
}
Expand Down Expand Up @@ -691,12 +715,17 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,
dynamicEvents := []fsnotify.Event{}

filtered := []fsnotify.Event{}
h, err := c.hugo()
if err != nil {
c.r.logger.Errorln("Error getting the Hugo object:", err)
return
}
for _, ev := range evs {
if c.hugo().ShouldSkipFileChangeEvent(ev) {
if h.ShouldSkipFileChangeEvent(ev) {
continue
}
// Check the most specific first, i.e. files.
contentMapped := c.hugo().ContentChanges.GetSymbolicLinkMappings(ev.Name)
contentMapped := h.ContentChanges.GetSymbolicLinkMappings(ev.Name)
if len(contentMapped) > 0 {
for _, mapped := range contentMapped {
filtered = append(filtered, fsnotify.Event{Name: mapped, Op: ev.Op})
Expand All @@ -708,7 +737,7 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,

dir, name := filepath.Split(ev.Name)

contentMapped = c.hugo().ContentChanges.GetSymbolicLinkMappings(dir)
contentMapped = h.ContentChanges.GetSymbolicLinkMappings(dir)

if len(contentMapped) == 0 {
filtered = append(filtered, ev)
Expand Down Expand Up @@ -742,7 +771,7 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,
if istemp {
continue
}
if c.hugo().Deps.SourceSpec.IgnoreFile(ev.Name) {
if h.Deps.SourceSpec.IgnoreFile(ev.Name) {
continue
}
// Sometimes during rm -rf operations a '"": REMOVE' is triggered. Just ignore these
Expand Down Expand Up @@ -771,7 +800,7 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,
if err := watcher.Add(path); err != nil {
return err
}
} else if !staticSyncer.isStatic(path) {
} else if !staticSyncer.isStatic(h, path) {
// Hugo's rebuilding logic is entirely file based. When you drop a new folder into
// /content on OSX, the above logic will handle future watching of those files,
// but the initial CREATE is lost.
Expand All @@ -788,7 +817,7 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,
}
}

if staticSyncer.isStatic(ev.Name) {
if staticSyncer.isStatic(h, ev.Name) {
staticEvents = append(staticEvents, ev)
} else {
dynamicEvents = append(dynamicEvents, ev)
Expand Down Expand Up @@ -818,7 +847,11 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,
// force refresh when more than one file
if !c.errState.wasErr() && len(staticEvents) == 1 {
ev := staticEvents[0]
h := c.hugo()
h, err := c.hugo()
if err != nil {
c.r.logger.Errorln("Error getting the Hugo object:", err)
return
}
path := h.BaseFs.SourceFilesystems.MakeStaticPathRelative(ev.Name)
path = h.RelURL(helpers.ToSlashTrimLeading(path), false)

Expand All @@ -831,7 +864,7 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,

if len(dynamicEvents) > 0 {
partitionedEvents := partitionDynamicEvents(
c.hugo().BaseFs.SourceFilesystems,
h.BaseFs.SourceFilesystems,
dynamicEvents)

onePageName := pickOneWriteOrCreatePath(partitionedEvents.ContentEvents)
Expand All @@ -857,7 +890,7 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,
// Nothing has changed.
return
} else if len(changed) == 1 {
pathToRefresh := c.hugo().PathSpec.RelURL(helpers.ToSlashTrimLeading(changed[0]), false)
pathToRefresh := h.PathSpec.RelURL(helpers.ToSlashTrimLeading(changed[0]), false)
livereload.RefreshPath(pathToRefresh)
} else {
livereload.ForceRefresh()
Expand All @@ -872,7 +905,7 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,

if navigate {
if onePageName != "" {
p = c.hugo().GetContentPage(onePageName)
p = h.GetContentPage(onePageName)
}
}

Expand All @@ -886,18 +919,18 @@ func (c *hugoBuilder) handleEvents(watcher *watcher.Batcher,
}
}

func (c *hugoBuilder) hugo() *hugolib.HugoSites {
func (c *hugoBuilder) hugo() (*hugolib.HugoSites, error) {
h, err := c.r.HugFromConfig(c.conf())
if err != nil {
panic(err)
return nil, err
}
if c.s != nil {
// A running server, register the media types.
for _, s := range h.Sites {
s.RegisterMediaTypes()
}
}
return h
return h, nil
}

func (c *hugoBuilder) hugoTry() *hugolib.HugoSites {
Expand Down Expand Up @@ -953,7 +986,10 @@ func (c *hugoBuilder) rebuildSites(events []fsnotify.Event) error {
}
c.errState.setBuildErr(nil)
visited := c.visitedURLs.PeekAllSet()
h := c.hugo()
h, err := c.hugo()
if err != nil {
return err
}
if c.fastRenderMode {
// Make sure we always render the home pages
for _, l := range c.conf().configs.Languages {
Expand Down
21 changes: 11 additions & 10 deletions commands/server.go
Expand Up @@ -234,16 +234,16 @@ func (f *fileServer) createEndpoint(i int) (*http.ServeMux, net.Listener, string
// First check the error state
err := f.c.getErrorWithContext()
if err != nil {
f.c.errState.setWasErr(false)
f.c.errState.setWasErr(true)
w.WriteHeader(500)
r, err := f.errorTemplate(err)
if err != nil {
logger.Errorln(err)
}

port = 1313
if !f.c.errState.isPaused() {
port = conf.configs.Base.Internal.LiveReloadPort
if lrport := conf.configs.GetFirstLanguageConfig().BaseURLLiveReload().Port(); lrport != 0 {
port = lrport
}
lr := *u
lr.Host = fmt.Sprintf("%s:%d", lr.Hostname(), port)
Expand Down Expand Up @@ -432,9 +432,6 @@ func (c *serverCommand) Run(ctx context.Context, cd *simplecobra.Commandeer, arg
err := func() error {
defer c.r.timeTrack(time.Now(), "Built")
err := c.build()
if err != nil {
c.r.Println("Error:", err.Error())
}
return err
}()
if err != nil {
Expand Down Expand Up @@ -703,8 +700,12 @@ func (c *serverCommand) partialReRender(urls ...string) error {
visited[url] = true
}

h, err := c.hugo()
if err != nil {
return err
}
// Note: We do not set NoBuildLock as the file lock is not acquired at this stage.
return c.hugo().Build(hugolib.BuildCfg{NoBuildLock: false, RecentlyVisited: visited, PartialReRender: true, ErrRecovery: c.errState.wasErr()})
return h.Build(hugolib.BuildCfg{NoBuildLock: false, RecentlyVisited: visited, PartialReRender: true, ErrRecovery: c.errState.wasErr()})
}

func (c *serverCommand) serve() error {
Expand Down Expand Up @@ -877,8 +878,8 @@ type staticSyncer struct {
c *hugoBuilder
}

func (s *staticSyncer) isStatic(filename string) bool {
return s.c.hugo().BaseFs.SourceFilesystems.IsStatic(filename)
func (s *staticSyncer) isStatic(h *hugolib.HugoSites, filename string) bool {
return h.BaseFs.SourceFilesystems.IsStatic(filename)
}

func (s *staticSyncer) syncsStaticEvents(staticEvents []fsnotify.Event) error {
Expand Down Expand Up @@ -1013,7 +1014,7 @@ func injectLiveReloadScript(src io.Reader, baseURL url.URL) string {

func partitionDynamicEvents(sourceFs *filesystems.SourceFilesystems, events []fsnotify.Event) (de dynamicEvents) {
for _, e := range events {
if sourceFs.IsAsset(e.Name) {
if !sourceFs.IsContent(e.Name) {
de.AssetEvents = append(de.AssetEvents, e)
} else {
de.ContentEvents = append(de.ContentEvents, e)
Expand Down
3 changes: 2 additions & 1 deletion main.go
Expand Up @@ -21,8 +21,9 @@ import (
)

func main() {
log.SetFlags(0)
err := commands.Execute(os.Args[1:])
if err != nil {
log.Fatal(err)
log.Fatalf("Error: %s", err)
}
}

0 comments on commit 834b3d7

Please sign in to comment.