shithub: hugo

Download patch

ref: beabc8d998249ecc5dd522d696dc6233a29131c2
parent: 332b65e4ccb6ac0d606de2a1b23f5189c72542be
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Fri Oct 30 06:14:08 EDT 2020

modules: Allow absolute paths for project imports

Fixes #7910

--- a/modules/client.go
+++ b/modules/client.go
@@ -614,10 +614,19 @@
 }
 
 func (c *Client) createThemeDirname(modulePath string, isProjectMod bool) (string, error) {
+	invalid := errors.Errorf("invalid module path %q; must be relative to themesDir when defined outside of the project", modulePath)
+
 	modulePath = filepath.Clean(modulePath)
+	if filepath.IsAbs(modulePath) {
+		if isProjectMod {
+			return modulePath, nil
+		}
+		return "", invalid
+	}
+
 	moduleDir := filepath.Join(c.ccfg.ThemesDir, modulePath)
 	if !isProjectMod && !strings.HasPrefix(moduleDir, c.ccfg.ThemesDir) {
-		return "", errors.Errorf("invalid module path %q; must be relative to themesDir when defined outside of the project", modulePath)
+		return "", invalid
 	}
 	return moduleDir, nil
 }
--- a/modules/client_test.go
+++ b/modules/client_test.go
@@ -15,6 +15,7 @@
 
 import (
 	"bytes"
+	"fmt"
 	"os"
 	"path/filepath"
 	"testing"
@@ -155,6 +156,14 @@
 		c.Assert(dirname, qt.Equals, filepath.Join(client.ccfg.ThemesDir, "../../foo"))
 
 		dirname, err = client.createThemeDirname("../../foo", false)
+		c.Assert(err, qt.Not(qt.IsNil))
+
+		absDir := filepath.Join(client.ccfg.WorkingDir, "..", "..")
+		dirname, err = client.createThemeDirname(absDir, true)
+		c.Assert(err, qt.IsNil)
+		c.Assert(dirname, qt.Equals, absDir)
+		dirname, err = client.createThemeDirname(absDir, false)
+		fmt.Println(dirname)
 		c.Assert(err, qt.Not(qt.IsNil))
 
 	})