commands: Make the error handling for the mod commands more lenient
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 17 Sep 2021 15:16:34 +0000 (17:16 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 17 Sep 2021 15:16:34 +0000 (17:16 +0200)
So it at least is possible to do a `hugo mod clean --all` if the cache is in a funky state.

commands/commandeer.go
commands/mod.go

index 349838a7e199c39b9bf389655219741bbed0b03a..7706360abb01650774f361637b3028b7b4a76ec5 100644 (file)
@@ -323,8 +323,11 @@ func (c *commandeer) loadConfig() error {
                // We should improve the error handling here,
                // but with hugo mod init and similar there is a chicken and egg situation
                // with modules already configured in config.toml, so ignore those errors.
-               if c.mustHaveConfigFile || !moduleNotFoundRe.MatchString(err.Error()) {
+               if c.mustHaveConfigFile || (c.failOnInitErr && !moduleNotFoundRe.MatchString(err.Error())) {
                        return err
+               } else {
+                       // Just make it a warning.
+                       c.logger.Warnln(err)
                }
        } else if c.mustHaveConfigFile && len(configFiles) == 0 {
                return hugolib.ErrNoConfigFile
index 0f8239a4c6ac8867b2d44c4e9cf384a9ecfe842c..eb6b1a4404241df20de3022e8b89029e3091fd6e 100644 (file)
@@ -71,11 +71,12 @@ Also note that if you configure a positive maxAge for the "modules" file cache,
                        if all {
                                com, err := c.initConfig(false)
 
-                               if err != nil && !moduleNotFoundRe.MatchString(err.Error()) {
+                               if err != nil && com == nil {
                                        return err
                                }
 
-                               _, err = com.hugo().FileCaches.ModulesCache().Prune(true)
+                               count, err := com.hugo().FileCaches.ModulesCache().Prune(true)
+                               com.logger.Printf("Deleted %d files from module cache.", count)
                                return err
                        }
                        return c.withModsClient(true, func(c *modules.Client) error {