hugo

Unnamed repository; edit this file 'description' to name the repository.

git clone git://git.shimmy1996.com/hugo.git
commit e8537e6dd0ae41427bd03d98477d4a6a1f71a2cc
parent 2fbdee726809af7352353bb39a2aa31153b2214d
Author: Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Date:   Thu, 12 May 2022 10:09:15 +0200

postcss: Fix line numbers in error messages

Fixes #9880

Diffstat:
Mresources/resource_transformers/postcss/integration_test.go | 92++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mresources/resource_transformers/postcss/postcss.go | 18++++++++++++++----
2 files changed, 61 insertions(+), 49 deletions(-)
diff --git a/resources/resource_transformers/postcss/integration_test.go b/resources/resource_transformers/postcss/integration_test.go
@@ -22,19 +22,15 @@ import (
 	jww "github.com/spf13/jwalterweatherman"
 
 	qt "github.com/frankban/quicktest"
+	"github.com/gohugoio/hugo/common/herrors"
 	"github.com/gohugoio/hugo/htesting"
 	"github.com/gohugoio/hugo/hugolib"
 )
 
-func TestTransformPostCSS(t *testing.T) {
-	if !htesting.IsCI() {
-		t.Skip("Skip long running test when running locally")
-	}
-
-	c := qt.New(t)
-
-	files := `
+const postCSSIntegrationTestFiles = `
 -- assets/css/components/a.css --
+/* A comment. */
+/* Another comment. */
 class-in-a {
 	color: blue;
 }
@@ -98,51 +94,57 @@ module.exports = {
 
 `
 
-	c.Run("Success", func(c *qt.C) {
-		b := hugolib.NewIntegrationTestBuilder(
-			hugolib.IntegrationTestConfig{
-				T:               c,
-				NeedsOsFS:       true,
-				NeedsNpmInstall: true,
-				LogLevel:        jww.LevelInfo,
-				TxtarString:     files,
-			}).Build()
-
-		b.AssertLogContains("Hugo Environment: production")
-		b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("PostCSS Config File: %s/postcss.config.js", b.Cfg.WorkingDir)))
-		b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("package.json: %s/package.json", b.Cfg.WorkingDir)))
-
-		b.AssertFileContent("public/index.html", `
+func TestTransformPostCSS(t *testing.T) {
+	if !htesting.IsCI() {
+		t.Skip("Skip long running test when running locally")
+	}
+
+	c := qt.New(t)
+
+	b := hugolib.NewIntegrationTestBuilder(
+		hugolib.IntegrationTestConfig{
+			T:               c,
+			NeedsOsFS:       true,
+			NeedsNpmInstall: true,
+			LogLevel:        jww.LevelInfo,
+			TxtarString:     postCSSIntegrationTestFiles,
+		}).Build()
+
+	b.AssertLogContains("Hugo Environment: production")
+	b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("PostCSS Config File: %s/postcss.config.js", b.Cfg.WorkingDir)))
+	b.AssertLogContains(filepath.FromSlash(fmt.Sprintf("package.json: %s/package.json", b.Cfg.WorkingDir)))
+
+	b.AssertFileContent("public/index.html", `
 Styles RelPermalink: /css/styles.css
 Styles Content: Len: 770875|
 `)
-	})
-
-	c.Run("Error", func(c *qt.C) {
-		s, err := hugolib.NewIntegrationTestBuilder(
-			hugolib.IntegrationTestConfig{
-				T:               c,
-				NeedsOsFS:       true,
-				NeedsNpmInstall: true,
-				TxtarString:     strings.ReplaceAll(files, "color: blue;", "@apply foo;"), // Syntax error
-			}).BuildE()
-		s.AssertIsFileError(err)
-	})
+
 }
 
-// bookmark2
-func TestIntegrationTestTemplate(t *testing.T) {
-	c := qt.New(t)
+// 9880
+func TestTransformPostCSSError(t *testing.T) {
+	if !htesting.IsCI() {
+		t.Skip("Skip long running test when running locally")
+	}
 
-	files := ``
+	c := qt.New(t)
 
-	b := hugolib.NewIntegrationTestBuilder(
+	s, err := hugolib.NewIntegrationTestBuilder(
 		hugolib.IntegrationTestConfig{
 			T:               c,
-			NeedsOsFS:       false,
-			NeedsNpmInstall: false,
-			TxtarString:     files,
-		}).Build()
+			NeedsOsFS:       true,
+			NeedsNpmInstall: true,
+			TxtarString:     strings.ReplaceAll(postCSSIntegrationTestFiles, "color: blue;", "@apply foo;"), // Syntax error
+		}).BuildE()
+
+	s.AssertIsFileError(err)
+	fe := herrors.UnwrapFileError(err)
+	pos := fe.Position()
+	c.Assert(strings.TrimPrefix(pos.Filename, s.H.WorkingDir), qt.Equals, filepath.FromSlash("/assets/css/components/a.css"))
+	c.Assert(pos.LineNumber, qt.Equals, 4)
+	errctx := fe.ErrorContext()
+	c.Assert(errctx, qt.IsNotNil)
+	c.Assert(errctx.Lines, qt.DeepEquals, []string{"/* Another comment. */", "class-in-a {", "\t@apply foo;", "}", ""})
+	c.Assert(errctx.ChromaLexer, qt.Equals, "css")
 
-	b.Assert(true, qt.IsTrue)
 }
diff --git a/resources/resource_transformers/postcss/postcss.go b/resources/resource_transformers/postcss/postcss.go
@@ -28,6 +28,7 @@ import (
 
 	"github.com/gohugoio/hugo/common/collections"
 	"github.com/gohugoio/hugo/common/hexec"
+	"github.com/gohugoio/hugo/hugofs"
 
 	"github.com/gohugoio/hugo/common/hugo"
 
@@ -39,8 +40,6 @@ import (
 
 	"errors"
 
-	"github.com/gohugoio/hugo/hugofs"
-
 	"github.com/mitchellh/mapstructure"
 
 	"github.com/gohugoio/hugo/common/herrors"
@@ -391,8 +390,19 @@ func (imp *importResolver) toFileError(output string) error {
 		return inErr
 	}
 
-	realFilename := fi.(hugofs.FileMetaInfo).Meta().Filename
+	meta := fi.(hugofs.FileMetaInfo).Meta()
+	realFilename := meta.Filename
+	f, err := meta.Open()
+	if err != nil {
+		return inErr
+	}
+	defer f.Close()
+
+	ferr := herrors.NewFileError(realFilename, inErr)
+	pos := ferr.Position()
+	pos.LineNumber = file.Offset + 1
+	return ferr.UpdatePosition(pos).UpdateContent(f, nil)
 
-	return herrors.NewFileErrorFromFile(inErr, file.Filename, realFilename, hugofs.Os, herrors.SimpleLineMatcher)
+	//return herrors.NewFileErrorFromFile(inErr, file.Filename, realFilename, hugofs.Os, herrors.SimpleLineMatcher)
 
 }