ref: df298558a5a5b747288d9656402af85e0ac75a43
parent: b1106f8715cac3544b8ea662b969336fe56fa047
author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
date: Tue Mar 10 14:12:11 EDT 2020
Improve Tailwind/PostCSS error messages Fixes #7041 Fixes #7042
--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -722,9 +722,6 @@
func (c *commandeer) rebuildSites(events []fsnotify.Event) error {
defer c.timeTrack(time.Now(), "Total")
- defer func() {
- c.wasError = false
- }()
c.buildErr = nil
visited := c.visitedURLs.PeekAllSet()
@@ -886,6 +883,10 @@
evs []fsnotify.Event,
configSet map[string]bool) {
+ defer func() {
+ c.wasError = false
+ }()
+
var isHandled bool
for _, ev := range evs {
@@ -1080,10 +1081,11 @@
// Will block forever trying to write to a channel that nobody is reading if livereload isn't initialized
// force refresh when more than one file
- if len(staticEvents) == 1 {
+ if !c.wasError && len(staticEvents) == 1 {
ev := staticEvents[0]
path := c.hugo().BaseFs.SourceFilesystems.MakeStaticPathRelative(ev.Name)
path = c.firstPathSpec().RelURL(helpers.ToSlashTrimLeading(path), false)
+
livereload.RefreshPath(path)
} else {
livereload.ForceRefresh()
@@ -1107,6 +1109,10 @@
if doLiveReload {
if len(partitionedEvents.ContentEvents) == 0 && len(partitionedEvents.AssetEvents) > 0 {
+ if c.wasError {
+ livereload.ForceRefresh()
+ return
+ }
changed := c.changeDetector.changed()
if c.changeDetector != nil && len(changed) == 0 {
// Nothing has changed.
--- a/commands/server.go
+++ b/commands/server.go
@@ -340,6 +340,7 @@
if err != nil {
f.c.logger.ERROR.Println(err)
}
+
port = 1313
if !f.c.paused {
port = f.c.Cfg.GetInt("liveReloadPort")
--- a/commands/server_errors.go
+++ b/commands/server_errors.go
@@ -30,8 +30,7 @@
body {
font-family: "Muli",avenir, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 16px;
- background-color: black;
- color: rgba(255, 255, 255, 0.9);
+ background-color: #2f1e2e;
}
main {
margin: auto;
@@ -43,7 +42,7 @@
padding: 1rem 0;
}
.stack {
- margin-top: 6rem;
+ margin-top: 4rem;
}
pre {
white-space: pre-wrap;
@@ -54,10 +53,7 @@
}
.highlight {
overflow-x: auto;
- padding: 0.75rem;
margin-bottom: 1rem;
- background-color: #272822;
- border: 1px solid black;
}
a {
color: #0594cb;
@@ -70,14 +66,14 @@
</head>
<body>
<main>
- {{ highlight .Error "apl" "noclasses=true,style=monokai" }}
+ {{ highlight .Error "apl" "linenos=false,noclasses=true,style=paraiso-dark" }}
{{ with .File }}
- {{ $params := printf "noclasses=true,style=monokai,linenos=table,hl_lines=%d,linenostart=%d" (add .LinesPos 1) (sub .Position.LineNumber .LinesPos) }}
+ {{ $params := printf "noclasses=true,style=paraiso-dark,linenos=table,hl_lines=%d,linenostart=%d" (add .LinesPos 1) (sub .Position.LineNumber .LinesPos) }}
{{ $lexer := .ChromaLexer | default "go-html-template" }}
{{ highlight (delimit .Lines "\n") $lexer $params }}
{{ end }}
{{ with .StackTrace }}
- {{ highlight . "apl" "noclasses=true,style=monokai" }}
+ {{ highlight . "apl" "noclasses=true,style=paraiso-dark" }}
{{ end }}
<p class="version">{{ .Version }}</p>
<a href="">Reload Page</a>
--- a/deps/deps.go
+++ b/deps/deps.go
@@ -235,7 +235,9 @@
return nil, errors.WithMessage(err, "failed to create file caches from configuration")
}
- resourceSpec, err := resources.NewSpec(ps, fileCaches, logger, cfg.OutputFormats, cfg.MediaTypes)
+ errorHandler := &globalErrHandler{}
+
+ resourceSpec, err := resources.NewSpec(ps, fileCaches, logger, errorHandler, cfg.OutputFormats, cfg.MediaTypes)
if err != nil {
return nil, err
}
@@ -275,7 +277,7 @@
BuildStartListeners: &Listeners{},
BuildFlags: &BuildFlags{},
Timeout: time.Duration(timeoutms) * time.Millisecond,
- globalErrHandler: &globalErrHandler{},
+ globalErrHandler: errorHandler,
}
if cfg.Cfg.GetBool("templateMetrics") {
@@ -306,7 +308,7 @@
// The resource cache is global so reuse.
// TODO(bep) clean up these inits.
resourceCache := d.ResourceSpec.ResourceCache
- d.ResourceSpec, err = resources.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.Log, cfg.OutputFormats, cfg.MediaTypes)
+ d.ResourceSpec, err = resources.NewSpec(d.PathSpec, d.ResourceSpec.FileCaches, d.Log, d.globalErrHandler, cfg.OutputFormats, cfg.MediaTypes)
if err != nil {
return nil, err
}
--- a/hugolib/resource_chain_test.go
+++ b/hugolib/resource_chain_test.go
@@ -22,6 +22,8 @@
"strings"
"testing"
+ "github.com/gohugoio/hugo/common/herrors"
+
"github.com/gohugoio/hugo/htesting"
"github.com/spf13/viper"
@@ -717,11 +719,10 @@
packageJSON := `{
"scripts": {},
- "dependencies": {
- "tailwindcss": "^1.2"
- },
+
"devDependencies": {
- "postcss-cli": "^7.1.0"
+ "postcss-cli": "^7.1.0",
+ "tailwindcss": "^1.2"
}
}
`
@@ -826,7 +827,7 @@
assertCss(b)
- build := func(s string, shouldFail bool) {
+ build := func(s string, shouldFail bool) error {
b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)
v := viper.New()
@@ -837,18 +838,36 @@
b = newTestBuilder(v)
b.Assert(os.RemoveAll(filepath.Join(workDir, "public")), qt.IsNil)
- b.Assert(os.RemoveAll(filepath.Join(workDir, "node_modules")), qt.IsNil)
+ err := b.BuildE(BuildCfg{})
if shouldFail {
- b.BuildFail(BuildCfg{})
+ b.Assert(err, qt.Not(qt.IsNil))
} else {
- b.Build(BuildCfg{})
+ b.Assert(err, qt.IsNil)
assertCss(b)
}
+
+ return err
}
build("always", false)
build("fallback", false)
+
+ // Introduce a syntax error in an import
+ b.WithSourceFile("assets/css/components/b.css", `@import "a.css";
+
+class-in-b {
+ @apply asdf;
+}
+`)
+
+ err = build("newer", true)
+
+ err = herrors.UnwrapErrorWithFileContext(err)
+ fe, ok := err.(*herrors.ErrorWithFileContext)
+ b.Assert(ok, qt.Equals, true)
+ b.Assert(fe.Position().LineNumber, qt.Equals, 4)
+ b.Assert(fe.Error(), qt.Contains, filepath.Join(workDir, "assets/css/components/b.css:4:1"))
// Remove PostCSS
b.Assert(os.RemoveAll(filepath.Join(workDir, "node_modules")), qt.IsNil)
--- a/resources/resource_spec.go
+++ b/resources/resource_spec.go
@@ -22,6 +22,8 @@
"path/filepath"
"strings"
+ "github.com/gohugoio/hugo/common/herrors"
+
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/hugofs"
@@ -43,6 +45,7 @@
s *helpers.PathSpec,
fileCaches filecache.Caches,
logger *loggers.Logger,
+ errorHandler herrors.ErrorSender,
outputFormats output.Formats,
mimeTypes media.Types) (*Spec, error) {
@@ -67,6 +70,7 @@
rs := &Spec{PathSpec: s,
Logger: logger,
+ ErrorSender: errorHandler,
imaging: imaging,
MediaTypes: mimeTypes,
OutputFormats: outputFormats,
@@ -91,7 +95,8 @@
MediaTypes media.Types
OutputFormats output.Formats
- Logger *loggers.Logger
+ Logger *loggers.Logger
+ ErrorSender herrors.ErrorSender
TextTemplates tpl.TemplateParseFinder
--- a/resources/resource_transformers/htesting/testhelpers.go
+++ b/resources/resource_transformers/htesting/testhelpers.go
@@ -51,7 +51,7 @@
return nil, err
}
- spec, err := resources.NewSpec(s, filecaches, nil, output.DefaultFormats, media.DefaultTypes)
+ spec, err := resources.NewSpec(s, filecaches, nil, nil, output.DefaultFormats, media.DefaultTypes)
return spec, err
}
--- a/resources/resource_transformers/postcss/postcss.go
+++ b/resources/resource_transformers/postcss/postcss.go
@@ -14,6 +14,7 @@
package postcss
import (
+ "bytes"
"crypto/sha256"
"encoding/hex"
"io"
@@ -21,13 +22,15 @@
"path"
"path/filepath"
"regexp"
+ "strconv"
"strings"
+ "github.com/gohugoio/hugo/common/loggers"
+
"github.com/gohugoio/hugo/config"
- "github.com/spf13/afero"
-
"github.com/gohugoio/hugo/resources/internal"
+ "github.com/spf13/afero"
"github.com/spf13/cast"
"github.com/gohugoio/hugo/hugofs"
@@ -45,6 +48,41 @@
const importIdentifier = "@import"
+var cssSyntaxErrorRe = regexp.MustCompile(`> (\d+) \|`)
+
+var shouldImportRe = regexp.MustCompile(`^@import ["'].*["'];?\s*(/\*.*\*/)?$`)
+
+// New creates a new Client with the given specification.
+func New(rs *resources.Spec) *Client {
+ return &Client{rs: rs}
+}
+
+func DecodeOptions(m map[string]interface{}) (opts Options, err error) {
+ if m == nil {
+ return
+ }
+ err = mapstructure.WeakDecode(m, &opts)
+
+ if !opts.NoMap {
+ // There was for a long time a discrepancy between documentation and
+ // implementation for the noMap property, so we need to support both
+ // camel and snake case.
+ opts.NoMap = cast.ToBool(m["no-map"])
+ }
+
+ return
+}
+
+// Client is the client used to do PostCSS transformations.
+type Client struct {
+ rs *resources.Spec
+}
+
+// Process transforms the given Resource with the PostCSS processor.
+func (c *Client) Process(res resources.ResourceTransformer, options Options) (resource.Resource, error) {
+ return res.Transform(&postcssTransformation{rs: c.rs, options: options})
+}
+
// Some of the options from https://github.com/postcss/postcss-cli
type Options struct {
@@ -68,22 +106,6 @@
Syntax string // Custom postcss syntax
}
-func DecodeOptions(m map[string]interface{}) (opts Options, err error) {
- if m == nil {
- return
- }
- err = mapstructure.WeakDecode(m, &opts)
-
- if !opts.NoMap {
- // There was for a long time a discrepancy between documentation and
- // implementation for the noMap property, so we need to support both
- // camel and snake case.
- opts.NoMap = cast.ToBool(m["no-map"])
- }
-
- return
-}
-
func (opts Options) toArgs() []string {
var args []string
if opts.NoMap {
@@ -104,16 +126,6 @@
return args
}
-// Client is the client used to do PostCSS transformations.
-type Client struct {
- rs *resources.Spec
-}
-
-// New creates a new Client with the given specification.
-func New(rs *resources.Spec) *Client {
- return &Client{rs: rs}
-}
-
type postcssTransformation struct {
options Options
rs *resources.Spec
@@ -186,8 +198,10 @@
cmd := exec.Command(binary, cmdArgs...)
+ var errBuf bytes.Buffer
+
cmd.Stdout = ctx.To
- cmd.Stderr = os.Stderr
+ cmd.Stderr = io.MultiWriter(os.Stderr, &errBuf)
// TODO(bep) somehow generalize this to other external helpers that may need this.
env := os.Environ()
config.SetEnvVars(&env, "HUGO_ENVIRONMENT", t.rs.Cfg.GetString("environment"))
@@ -199,9 +213,16 @@
}
src := ctx.From
+
+ imp := newImportResolver(
+ ctx.From,
+ ctx.InPath,
+ t.rs.Assets.Fs, t.rs.Logger,
+ )
+
if t.options.InlineImports {
var err error
- src, err = t.inlineImports(ctx)
+ src, err = imp.resolve()
if err != nil {
return err
}
@@ -214,39 +235,50 @@
err = cmd.Run()
if err != nil {
- return err
+ return imp.toFileError(errBuf.String())
}
return nil
}
-func (t *postcssTransformation) inlineImports(ctx *resources.ResourceTransformationCtx) (io.Reader, error) {
+type fileOffset struct {
+ Filename string
+ Offset int
+}
- const importIdentifier = "@import"
+type importResolver struct {
+ r io.Reader
+ inPath string
- // Set of content hashes.
- contentSeen := make(map[string]bool)
+ contentSeen map[string]bool
+ linemap map[int]fileOffset
+ fs afero.Fs
+ logger *loggers.Logger
+}
- content, err := ioutil.ReadAll(ctx.From)
- if err != nil {
- return nil, err
+func newImportResolver(r io.Reader, inPath string, fs afero.Fs, logger *loggers.Logger) *importResolver {
+ return &importResolver{
+ r: r,
+ inPath: inPath,
+ fs: fs, logger: logger,
+ linemap: make(map[int]fileOffset), contentSeen: make(map[string]bool),
}
+}
- contents := string(content)
-
- newContent, err := t.importRecursive(contentSeen, contents, ctx.InPath)
+func (imp *importResolver) contentHash(filename string) ([]byte, string) {
+ b, err := afero.ReadFile(imp.fs, filename)
if err != nil {
- return nil, err
+ return nil, ""
}
-
- return strings.NewReader(newContent), nil
-
+ h := sha256.New()
+ h.Write(b)
+ return b, hex.EncodeToString(h.Sum(nil))
}
-func (t *postcssTransformation) importRecursive(
- contentSeen map[string]bool,
+func (imp *importResolver) importRecursive(
+ lineNum int,
content string,
- inPath string) (string, error) {
+ inPath string) (int, string, error) {
basePath := path.Dir(inPath)
@@ -253,30 +285,49 @@
var replacements []string
lines := strings.Split(content, "\n")
- for _, line := range lines {
+ trackLine := func(i, offset int, line string) {
+ // TODO(bep) this is not very efficient.
+ imp.linemap[i+lineNum] = fileOffset{Filename: inPath, Offset: offset}
+ }
+
+ i := 0
+ for offset, line := range lines {
+ i++
line = strings.TrimSpace(line)
- if shouldImport(line) {
+
+ if !imp.shouldImport(line) {
+ trackLine(i, offset, line)
+ } else {
+ i--
path := strings.Trim(strings.TrimPrefix(line, importIdentifier), " \"';")
filename := filepath.Join(basePath, path)
- importContent, hash := t.contentHash(filename)
+ importContent, hash := imp.contentHash(filename)
if importContent == nil {
- t.rs.Logger.WARN.Printf("postcss: Failed to resolve CSS @import in %q for path %q", inPath, filename)
+ trackLine(i, offset, "ERROR")
+ imp.logger.WARN.Printf("postcss: Failed to resolve CSS @import in %q for path %q", inPath, filename)
continue
}
- if contentSeen[hash] {
+ if imp.contentSeen[hash] {
+ i++
// Just replace the line with an empty string.
replacements = append(replacements, []string{line, ""}...)
+ trackLine(i, offset, "IMPORT")
continue
}
- contentSeen[hash] = true
+ imp.contentSeen[hash] = true
// Handle recursive imports.
- nested, err := t.importRecursive(contentSeen, string(importContent), filepath.ToSlash(filename))
+ l, nested, err := imp.importRecursive(i+lineNum, string(importContent), filepath.ToSlash(filename))
if err != nil {
- return "", err
+ return 0, "", err
}
+
+ trackLine(i, offset, line)
+
+ i += l
+
importContent = []byte(nested)
replacements = append(replacements, []string{line, string(importContent)}...)
@@ -288,26 +339,28 @@
content = repl.Replace(content)
}
- return content, nil
+ return i, content, nil
}
-func (t *postcssTransformation) contentHash(filename string) ([]byte, string) {
- b, err := afero.ReadFile(t.rs.Assets.Fs, filename)
+func (imp *importResolver) resolve() (io.Reader, error) {
+ const importIdentifier = "@import"
+
+ content, err := ioutil.ReadAll(imp.r)
if err != nil {
- return nil, ""
+ return nil, err
}
- h := sha256.New()
- h.Write(b)
- return b, hex.EncodeToString(h.Sum(nil))
-}
-// Process transforms the given Resource with the PostCSS processor.
-func (c *Client) Process(res resources.ResourceTransformer, options Options) (resource.Resource, error) {
- return res.Transform(&postcssTransformation{rs: c.rs, options: options})
-}
+ contents := string(content)
-var shouldImportRe = regexp.MustCompile(`^@import ["'].*["'];?\s*(/\*.*\*/)?$`)
+ _, newContent, err := imp.importRecursive(0, contents, imp.inPath)
+ if err != nil {
+ return nil, err
+ }
+ return strings.NewReader(newContent), nil
+
+}
+
// See https://www.w3schools.com/cssref/pr_import_rule.asp
// We currently only support simple file imports, no urls, no media queries.
// So this is OK:
@@ -315,7 +368,7 @@
// This is not:
// @import url("navigation.css");
// @import "mobstyle.css" screen and (max-width: 768px);
-func shouldImport(s string) bool {
+func (imp *importResolver) shouldImport(s string) bool {
if !strings.HasPrefix(s, importIdentifier) {
return false
}
@@ -324,4 +377,39 @@
}
return shouldImportRe.MatchString(s)
+}
+
+func (imp *importResolver) toFileError(output string) error {
+ inErr := errors.New(strings.TrimSpace(output))
+
+ match := cssSyntaxErrorRe.FindStringSubmatch(output)
+ if match == nil {
+ return inErr
+ }
+
+ lineNum, err := strconv.Atoi(match[1])
+ if err != nil {
+ return inErr
+ }
+
+ file, ok := imp.linemap[lineNum]
+ if !ok {
+ return inErr
+ }
+
+ fi, err := imp.fs.Stat(file.Filename)
+ if err != nil {
+ return inErr
+ }
+ realFilename := fi.(hugofs.FileMetaInfo).Meta().Filename()
+
+ ferr := herrors.NewFileError("css", -1, file.Offset+1, 1, inErr)
+
+ werr, ok := herrors.WithFileContextForFile(ferr, realFilename, file.Filename, imp.fs, herrors.SimpleLineMatcher)
+
+ if !ok {
+ return ferr
+ }
+
+ return werr
}
--- a/resources/resource_transformers/postcss/postcss_test.go
+++ b/resources/resource_transformers/postcss/postcss_test.go
@@ -14,8 +14,17 @@
package postcss
import (
+ "regexp"
+ "strings"
"testing"
+ "github.com/gohugoio/hugo/htesting/hqt"
+
+ "github.com/gohugoio/hugo/common/loggers"
+ "github.com/gohugoio/hugo/helpers"
+
+ "github.com/spf13/afero"
+
qt "github.com/frankban/quicktest"
)
@@ -40,6 +49,7 @@
func TestShouldImport(t *testing.T) {
c := qt.New(t)
+ var imp *importResolver
for _, test := range []struct {
input string
@@ -52,6 +62,106 @@
{input: `@import url("navigation.css");`, expect: false},
{input: `@import url('https://fonts.googleapis.com/css?family=Open+Sans:400,400i,800,800i&display=swap');`, expect: false},
} {
- c.Assert(shouldImport(test.input), qt.Equals, test.expect)
+ c.Assert(imp.shouldImport(test.input), qt.Equals, test.expect)
+ }
+}
+
+func TestImportResolver(t *testing.T) {
+ c := qt.New(t)
+ fs := afero.NewMemMapFs()
+
+ writeFile := func(name, content string) {
+ c.Assert(afero.WriteFile(fs, name, []byte(content), 0777), qt.IsNil)
+ }
+
+ writeFile("a.css", `@import "b.css";
+@import "c.css";
+A_STYLE1
+A_STYLE2
+`)
+
+ writeFile("b.css", `B_STYLE`)
+ writeFile("c.css", "@import \"d.css\"\nC_STYLE")
+ writeFile("d.css", "@import \"a.css\"\n\nD_STYLE")
+ writeFile("e.css", "E_STYLE")
+
+ mainStyles := strings.NewReader(`@import "a.css";
+@import "b.css";
+LOCAL_STYLE
+@import "c.css";
+@import "e.css";
+@import "missing.css";`)
+
+ imp := newImportResolver(
+ mainStyles,
+ "styles.css",
+ fs, loggers.NewErrorLogger(),
+ )
+
+ r, err := imp.resolve()
+ c.Assert(err, qt.IsNil)
+ rs := helpers.ReaderToString(r)
+ result := regexp.MustCompile(`\n+`).ReplaceAllString(rs, "\n")
+
+ c.Assert(result, hqt.IsSameString, `B_STYLE
+D_STYLE
+C_STYLE
+A_STYLE1
+A_STYLE2
+LOCAL_STYLE
+E_STYLE
+@import "missing.css";`)
+
+ dline := imp.linemap[3]
+ c.Assert(dline, qt.DeepEquals, fileOffset{
+ Offset: 1,
+ Filename: "d.css",
+ })
+
+}
+
+func BenchmarkImportResolver(b *testing.B) {
+ c := qt.New(b)
+ fs := afero.NewMemMapFs()
+
+ writeFile := func(name, content string) {
+ c.Assert(afero.WriteFile(fs, name, []byte(content), 0777), qt.IsNil)
+ }
+
+ writeFile("a.css", `@import "b.css";
+@import "c.css";
+A_STYLE1
+A_STYLE2
+`)
+
+ writeFile("b.css", `B_STYLE`)
+ writeFile("c.css", "@import \"d.css\"\nC_STYLE"+strings.Repeat("\nSTYLE", 12))
+ writeFile("d.css", "@import \"a.css\"\n\nD_STYLE"+strings.Repeat("\nSTYLE", 55))
+ writeFile("e.css", "E_STYLE")
+
+ mainStyles := `@import "a.css";
+@import "b.css";
+LOCAL_STYLE
+@import "c.css";
+@import "e.css";
+@import "missing.css";`
+
+ logger := loggers.NewErrorLogger()
+
+ for i := 0; i < b.N; i++ {
+ b.StopTimer()
+ imp := newImportResolver(
+ strings.NewReader(mainStyles),
+ "styles.css",
+ fs, logger,
+ )
+
+ b.StartTimer()
+
+ _, err := imp.resolve()
+ if err != nil {
+ b.Fatal(err)
+ }
+
}
}
--- a/resources/testhelpers_test.go
+++ b/resources/testhelpers_test.go
@@ -90,7 +90,7 @@
filecaches, err := filecache.NewCaches(s)
c.Assert(err, qt.IsNil)
- spec, err := NewSpec(s, filecaches, nil, output.DefaultFormats, media.DefaultTypes)
+ spec, err := NewSpec(s, filecaches, nil, nil, output.DefaultFormats, media.DefaultTypes)
c.Assert(err, qt.IsNil)
return spec
}
@@ -129,7 +129,7 @@
filecaches, err := filecache.NewCaches(s)
c.Assert(err, qt.IsNil)
- spec, err := NewSpec(s, filecaches, nil, output.DefaultFormats, media.DefaultTypes)
+ spec, err := NewSpec(s, filecaches, nil, nil, output.DefaultFormats, media.DefaultTypes)
c.Assert(err, qt.IsNil)
return spec, workDir
--- a/resources/transform.go
+++ b/resources/transform.go
@@ -28,6 +28,7 @@
bp "github.com/gohugoio/hugo/bufferpool"
+ "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/helpers"
@@ -392,17 +393,25 @@
}
}
- notAvailableErr := func(err error) error {
- errMsg := err.Error()
- if tr.Key().Name == "postcss" {
- // This transformation is not available in this
- // Most likely because PostCSS is not installed.
- errMsg += ". Check your PostCSS installation; install with \"npm install postcss-cli\". See https://gohugo.io/hugo-pipes/postcss/"
- } else if tr.Key().Name == "tocss" {
- errMsg += ". Check your Hugo installation; you need the extended version to build SCSS/SASS."
+ newErr := func(err error) error {
+
+ msg := fmt.Sprintf("%s: failed to transform %q (%s)", strings.ToUpper(tr.Key().Name), tctx.InPath, tctx.InMediaType.Type())
+
+ if err == herrors.ErrFeatureNotAvailable {
+ var errMsg string
+ if tr.Key().Name == "postcss" {
+ // This transformation is not available in this
+ // Most likely because PostCSS is not installed.
+ errMsg = ". Check your PostCSS installation; install with \"npm install postcss-cli\". See https://gohugo.io/hugo-pipes/postcss/"
+ } else if tr.Key().Name == "tocss" {
+ errMsg = ". Check your Hugo installation; you need the extended version to build SCSS/SASS."
+ }
+
+ return errors.New(msg + errMsg)
}
- return fmt.Errorf("%s: failed to transform %q (%s): %s", strings.ToUpper(tr.Key().Name), tctx.InPath, tctx.InMediaType.Type(), errMsg)
+ return errors.Wrap(err, msg)
+
}
var tryFileCache bool
@@ -411,11 +420,15 @@
tryFileCache = true
} else {
err = tr.Transform(tctx)
+ if err != nil && err != herrors.ErrFeatureNotAvailable {
+ return newErr(err)
+ }
+
if mayBeCachedOnDisk {
tryFileCache = r.spec.BuildConfig.UseResourceCache(err)
}
if err != nil && !tryFileCache {
- return notAvailableErr(err)
+ return newErr(err)
}
}
@@ -422,7 +435,7 @@
if tryFileCache {
f := r.target.tryTransformedFileCache(key, updates)
if f == nil {
- return notAvailableErr(errors.Errorf("resource %q not found in file cache", key))
+ return newErr(errors.Errorf("resource %q not found in file cache", key))
}
transformedContentr = f
updates.sourceFs = cache.fileCache.Fs
@@ -525,7 +538,11 @@
r.transformationsErr = r.transform(publish, setContent)
if r.transformationsErr != nil {
- r.spec.Logger.ERROR.Printf("Transformation failed: %s", r.transformationsErr)
+ if r.spec.ErrorSender != nil {
+ r.spec.ErrorSender.SendError(r.transformationsErr)
+ } else {
+ r.spec.Logger.ERROR.Printf("Transformation failed: %s", r.transformationsErr)
+ }
}
})