URI: 
       server: Fix SIGINT handling after loading bad configuration - hugo - [fork] hugo port for 9front
  HTML git clone git@git.drkhsh.at/hugo.git
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
   DIR commit 87a22eb6d609a65471ccf4de35a558e9669a4600
   DIR parent fc9f315d86e1fe51c3d1eec3b60680113b2e3aa6
  HTML Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
       Date:   Sun, 15 May 2022 21:01:36 +0200
       
       server: Fix SIGINT handling after loading bad configuration
       
       Also fix the config error messages.
       
       Fixes #9664
       
       Diffstat:
         M commands/commandeer.go              |       9 +++++++++
         M commands/server.go                  |      47 +++++++++++++++++++++++--------
         M config/configLoader.go              |       7 +++++++
         M hugolib/config.go                   |      11 +++++++++--
       
       4 files changed, 60 insertions(+), 14 deletions(-)
       ---
   DIR diff --git a/commands/commandeer.go b/commands/commandeer.go
       @@ -130,6 +130,15 @@ func (c *commandeerHugoState) hugo() *hugolib.HugoSites {
                return c.hugoSites
        }
        
       +func (c *commandeerHugoState) hugoTry() *hugolib.HugoSites {
       +        select {
       +        case <-c.created:
       +                return c.hugoSites
       +        case <-time.After(time.Millisecond * 100):
       +                return nil
       +        }
       +}
       +
        func (c *commandeer) errCount() int {
                return int(c.logger.LogCounters().ErrorCounter.Count())
        }
   DIR diff --git a/commands/server.go b/commands/server.go
       @@ -522,18 +522,20 @@ func (c *commandeer) serve(s *serverCmd) error {
                        roots = []string{""}
                }
        
       +        templHandler := c.hugo().Tmpl()
       +        errTempl, found := templHandler.Lookup("_server/error.html")
       +        if !found {
       +                panic("template server/error.html not found")
       +        }
       +
                srv := &fileServer{
                        baseURLs: baseURLs,
                        roots:    roots,
                        c:        c,
                        s:        s,
                        errorTemplate: func(ctx any) (io.Reader, error) {
       -                        templ, found := c.hugo().Tmpl().Lookup("_server/error.html")
       -                        if !found {
       -                                panic("template server/error.html not found")
       -                        }
                                b := &bytes.Buffer{}
       -                        err := c.hugo().Tmpl().Execute(templ, b, ctx)
       +                        err := templHandler.Execute(errTempl, b, ctx)
                                return b, err
                        },
                }
       @@ -579,16 +581,37 @@ func (c *commandeer) serve(s *serverCmd) error {
        
                jww.FEEDBACK.Println("Press Ctrl+C to stop")
        
       -        if s.stop != nil {
       -                select {
       -                case <-sigs:
       -                case <-s.stop:
       +        err := func() error {
       +                if s.stop != nil {
       +                        for {
       +                                select {
       +                                case <-sigs:
       +                                        return nil
       +                                case <-s.stop:
       +                                        return nil
       +                                case <-ctx.Done():
       +                                        return ctx.Err()
       +                                }
       +                        }
       +                } else {
       +                        for {
       +                                select {
       +                                case <-sigs:
       +                                        return nil
       +                                case <-ctx.Done():
       +                                        return ctx.Err()
       +                                }
       +                        }
                        }
       -        } else {
       -                <-sigs
       +        }()
       +
       +        if err != nil {
       +                jww.ERROR.Println("Error:", err)
                }
        
       -        c.hugo().Close()
       +        if h := c.hugoTry(); h != nil {
       +                h.Close()
       +        }
        
                ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
                defer cancel()
   DIR diff --git a/config/configLoader.go b/config/configLoader.go
       @@ -59,6 +59,13 @@ func FromConfigString(config, configType string) (Provider, error) {
        func FromFile(fs afero.Fs, filename string) (Provider, error) {
                m, err := loadConfigFromFile(fs, filename)
                if err != nil {
       +                fe := herrors.UnwrapFileError(err)
       +                if fe != nil {
       +                        pos := fe.Position()
       +                        pos.Filename = filename
       +                        fe.UpdatePosition(pos)
       +                        return nil, err
       +                }
                        return nil, herrors.NewFileErrorFromFile(err, filename, fs, nil)
                }
                return NewFrom(m), nil
   DIR diff --git a/hugolib/config.go b/hugolib/config.go
       @@ -75,7 +75,7 @@ func LoadConfig(d ConfigSourceDescriptor, doWithConfig ...func(cfg config.Provid
                        if err == nil {
                                configFiles = append(configFiles, filename)
                        } else if err != ErrNoConfigFile {
       -                        return nil, nil, err
       +                        return nil, nil, l.wrapFileError(err, filename)
                        }
                }
        
       @@ -463,7 +463,7 @@ func (l configLoader) loadConfig(configName string) (string, error) {
        
                m, err := config.FromFileToMap(l.Fs, filename)
                if err != nil {
       -                return "", l.wrapFileError(err, filename)
       +                return filename, err
                }
        
                // Set overwrites keys of the same name, recursively.
       @@ -511,5 +511,12 @@ func (configLoader) loadSiteConfig(cfg config.Provider) (scfg SiteConfig, err er
        }
        
        func (l configLoader) wrapFileError(err error, filename string) error {
       +        fe := herrors.UnwrapFileError(err)
       +        if fe != nil {
       +                pos := fe.Position()
       +                pos.Filename = filename
       +                fe.UpdatePosition(pos)
       +                return err
       +        }
                return herrors.NewFileErrorFromFile(err, filename, l.Fs, nil)
        }