Skip to content

Commit

Permalink
dartsass: Remove support for v1 of the protocol/binary (note)
Browse files Browse the repository at this point in the history
People who stil use a very old binary named `dart-sass-embedded` need to upgrade.

See https://gohugo.io/functions/css/sass/#dart-sass
  • Loading branch information
bep committed Nov 18, 2024
1 parent bbbfeb3 commit 86d3c28
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 115 deletions.
11 changes: 0 additions & 11 deletions common/herrors/file_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"io"
"path/filepath"

godartsassv1 "github.com/bep/godartsass"

"github.com/bep/godartsass/v2"
"github.com/bep/golibsass/libsass/libsasserrors"
"github.com/gohugoio/hugo/common/paths"
Expand Down Expand Up @@ -153,8 +151,6 @@ func (e *fileError) causeString() string {
// Avoid repeating the file info in the error message.
case godartsass.SassError:
return v.Message
case godartsassv1.SassError:
return v.Message
case libsasserrors.Error:
return v.Message
default:
Expand Down Expand Up @@ -392,13 +388,6 @@ func extractPosition(e error) (pos text.Position) {
pos.Filename = filename
pos.Offset = start.Offset
pos.ColumnNumber = start.Column
case godartsassv1.SassError:
span := v.Span
start := span.Start
filename, _ := paths.UrlToFilename(span.Url)
pos.Filename = filename
pos.Offset = start.Offset
pos.ColumnNumber = start.Column
case libsasserrors.Error:
pos.Filename = v.File
pos.LineNumber = v.Line
Expand Down
24 changes: 9 additions & 15 deletions common/hugo/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (
"sync"
"time"

godartsassv1 "github.com/bep/godartsass"
"github.com/bep/logg"
"github.com/mitchellh/mapstructure"

"github.com/bep/godartsass/v2"
"github.com/gohugoio/hugo/common/hcontext"
Expand Down Expand Up @@ -318,7 +316,7 @@ func GetDependencyListNonGo() []string {

if dartSass := dartSassVersion(); dartSass.ProtocolVersion != "" {
dartSassPath := "github.com/sass/dart-sass-embedded"
if IsDartSassV2() {
if IsDartSassGeV2() {
dartSassPath = "github.com/sass/dart-sass"
}
deps = append(deps,
Expand Down Expand Up @@ -365,22 +363,15 @@ type Dependency struct {
}

func dartSassVersion() godartsass.DartSassVersion {
if DartSassBinaryName == "" {
if DartSassBinaryName == "" || !IsDartSassGeV2() {
return godartsass.DartSassVersion{}
}
if IsDartSassV2() {
v, _ := godartsass.Version(DartSassBinaryName)
return v
}

v, _ := godartsassv1.Version(DartSassBinaryName)
var vv godartsass.DartSassVersion
mapstructure.WeakDecode(v, &vv)
return vv
v, _ := godartsass.Version(DartSassBinaryName)
return v
}

// DartSassBinaryName is the name of the Dart Sass binary to use.
// TODO(beop) find a better place for this.
// TODO(bep) find a better place for this.
var DartSassBinaryName string

func init() {
Expand All @@ -405,7 +396,10 @@ var (
dartSassBinaryNamesV2 = []string{"dart-sass", "sass"}
)

func IsDartSassV2() bool {
// TODO(bep) we eventually want to remove this, but keep it for a while to throw an informative error.
// We stopped supporting the old binary in Hugo 0.139.0.
func IsDartSassGeV2() bool {
// dart-sass-embedded was the first version of the embedded Dart Sass before it was moved into the main project.
return !strings.Contains(DartSassBinaryName, "embedded")
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/bep/debounce v1.2.0
github.com/bep/gitmap v1.6.0
github.com/bep/goat v0.5.0
github.com/bep/godartsass v1.2.0
github.com/bep/godartsass/v2 v2.2.0
github.com/bep/golibsass v1.2.0
github.com/bep/gowebp v0.3.0
Expand Down
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,6 @@ github.com/bep/gitmap v1.6.0 h1:sDuQMm9HoTL0LtlrfxjbjgAg2wHQd4nkMup2FInYzhA=
github.com/bep/gitmap v1.6.0/go.mod h1:n+3W1f/rot2hynsqEGxGMErPRgT41n9CkGuzPvz9cIw=
github.com/bep/goat v0.5.0 h1:S8jLXHCVy/EHIoCY+btKkmcxcXFd34a0Q63/0D4TKeA=
github.com/bep/goat v0.5.0/go.mod h1:Md9x7gRxiWKs85yHlVTvHQw9rg86Bm+Y4SuYE8CTH7c=
github.com/bep/godartsass v1.2.0 h1:E2VvQrxAHAFwbjyOIExAMmogTItSKodoKuijNrGm5yU=
github.com/bep/godartsass v1.2.0/go.mod h1:6LvK9RftsXMxGfsA0LDV12AGc4Jylnu6NgHL+Q5/pE8=
github.com/bep/godartsass/v2 v2.2.0 h1:3hO9Dt4BOnxkKmRxc+OpoKVFrDvBycpSCXEdElVAMVI=
github.com/bep/godartsass/v2 v2.2.0/go.mod h1:AcP8QgC+OwOXEq6im0WgDRYK7scDsmZCEW62o1prQLo=
github.com/bep/golibsass v1.2.0 h1:nyZUkKP/0psr8nT6GR2cnmt99xS93Ji82ZD9AgOK6VI=
Expand All @@ -139,8 +137,6 @@ github.com/bep/gowebp v0.3.0 h1:MhmMrcf88pUY7/PsEhMgEP0T6fDUnRTMpN8OclDrbrY=
github.com/bep/gowebp v0.3.0/go.mod h1:ZhFodwdiFp8ehGJpF4LdPl6unxZm9lLFjxD3z2h2AgI=
github.com/bep/helpers v0.5.0 h1:rneezhnG7GzLFlsEWO/EnleaBRuluBDGFimalO6Y50o=
github.com/bep/helpers v0.5.0/go.mod h1:dSqCzIvHbzsk5YOesp1M7sKAq5xUcvANsRoKdawxH4Q=
github.com/bep/imagemeta v0.8.1 h1:tjZLPRftjxU7PTI87o5e5WKOFQ4S9S0engiP1OTpJTI=
github.com/bep/imagemeta v0.8.1/go.mod h1:5piPAq5Qomh07m/dPPCLN3mDJyFusvUG7VwdRD/vX0s=
github.com/bep/imagemeta v0.8.3 h1:68XqpYXjWW9mFjdGurutDmAKBJa9y2aknEBHwY/+3zw=
github.com/bep/imagemeta v0.8.3/go.mod h1:5piPAq5Qomh07m/dPPCLN3mDJyFusvUG7VwdRD/vX0s=
github.com/bep/lazycache v0.7.0 h1:VM257SkkjcR9z55eslXTkUIX8QMNKoqQRNKV/4xIkCY=
Expand All @@ -165,7 +161,6 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/clbanning/mxj/v2 v2.7.0 h1:WA/La7UGCanFe5NpHF0Q3DNtnCsVoxbPKuyBNHWRyME=
github.com/clbanning/mxj/v2 v2.7.0/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
github.com/cli/safeexec v1.0.1 h1:e/C79PbXF4yYTN/wauC4tviMxEV13BwljGj0N9j+N00=
github.com/cli/safeexec v1.0.1/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
Expand Down Expand Up @@ -202,7 +197,6 @@ github.com/fortytw2/leaktest v1.3.0/go.mod h1:jDsjWgpAGjm2CA7WthBh/CdZYEPF31XHqu
github.com/frankban/quicktest v1.4.1/go.mod h1:36zfPVQyHxymz4cH7wlDmVwDrJuljRB60qkgn7rorfQ=
github.com/frankban/quicktest v1.7.2/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03DbaAcR7Ks/o=
github.com/frankban/quicktest v1.13.0/go.mod h1:qLE0fzW0VuyUAJgPU19zByoIr0HtCHN/r/VLSOOIySU=
github.com/frankban/quicktest v1.14.2/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand Down Expand Up @@ -289,7 +283,6 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
Expand Down Expand Up @@ -360,7 +353,6 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -419,7 +411,6 @@ github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
Expand Down
95 changes: 26 additions & 69 deletions resources/resource_transformers/tocss/dartsass/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"io"
"strings"

godartsassv1 "github.com/bep/godartsass"
"github.com/bep/godartsass/v2"
"github.com/bep/logg"
"github.com/gohugoio/hugo/common/herrors"
Expand Down Expand Up @@ -52,54 +51,39 @@ func New(fs *filesystems.SourceFilesystem, rs *resources.Spec) (*Client, error)
return nil, fmt.Errorf("no Dart Sass binary found in $PATH")
}

if !hugo.IsDartSassGeV2() {
return nil, fmt.Errorf("Unsupported Dart Sass version detected, please upgrade to Dart Sass 2.0 or later, see https://gohugo.io/functions/css/sass/#dart-sass")

Check failure on line 55 in resources/resource_transformers/tocss/dartsass/client.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

error strings should not be capitalized (ST1005)
}

if err := rs.ExecHelper.Sec().CheckAllowedExec(hugo.DartSassBinaryName); err != nil {
return nil, err
}

var (
transpiler *godartsass.Transpiler
transpilerv1 *godartsassv1.Transpiler
err error
infol = rs.Logger.InfoCommand("Dart Sass")
warnl = rs.Logger.WarnCommand("Dart Sass")
transpiler *godartsass.Transpiler
err error
infol = rs.Logger.InfoCommand("Dart Sass")
warnl = rs.Logger.WarnCommand("Dart Sass")
)

if hugo.IsDartSassV2() {
transpiler, err = godartsass.Start(godartsass.Options{
DartSassEmbeddedFilename: hugo.DartSassBinaryName,
LogEventHandler: func(event godartsass.LogEvent) {
message := strings.ReplaceAll(event.Message, dartSassStdinPrefix, "")
switch event.Type {
case godartsass.LogEventTypeDebug:
// Log as Info for now, we may adjust this if it gets too chatty.
infol.Log(logg.String(message))
default:
// The rest are either deprecations or @warn statements.
warnl.Log(logg.String(message))
}
},
})
} else {
transpilerv1, err = godartsassv1.Start(godartsassv1.Options{
DartSassEmbeddedFilename: hugo.DartSassBinaryName,
LogEventHandler: func(event godartsassv1.LogEvent) {
message := strings.ReplaceAll(event.Message, dartSassStdinPrefix, "")
switch event.Type {
case godartsassv1.LogEventTypeDebug:
// Log as Info for now, we may adjust this if it gets too chatty.
infol.Log(logg.String(message))
default:
// The rest are either deprecations or @warn statements.
warnl.Log(logg.String(message))
}
},
})
}

transpiler, err = godartsass.Start(godartsass.Options{
DartSassEmbeddedFilename: hugo.DartSassBinaryName,
LogEventHandler: func(event godartsass.LogEvent) {
message := strings.ReplaceAll(event.Message, dartSassStdinPrefix, "")
switch event.Type {
case godartsass.LogEventTypeDebug:
// Log as Info for now, we may adjust this if it gets too chatty.
infol.Log(logg.String(message))
default:
// The rest are either deprecations or @warn statements.
warnl.Log(logg.String(message))
}
},
})
if err != nil {
return nil, err
}
return &Client{sfs: fs, workFs: rs.BaseFs.Work, rs: rs, transpiler: transpiler, transpilerV1: transpilerv1}, nil
return &Client{sfs: fs, workFs: rs.BaseFs.Work, rs: rs, transpiler: transpiler}, nil
}

type Client struct {
Expand All @@ -108,9 +92,7 @@ type Client struct {
sfs *filesystems.SourceFilesystem
workFs afero.Fs

// One of these are non-nil.
transpiler *godartsass.Transpiler
transpilerV1 *godartsassv1.Transpiler
transpiler *godartsass.Transpiler
}

func (c *Client) ToCSS(res resources.ResourceTransformer, args map[string]any) (resource.Resource, error) {
Expand All @@ -121,40 +103,15 @@ func (c *Client) ToCSS(res resources.ResourceTransformer, args map[string]any) (
}

func (c *Client) Close() error {
if c.transpilerV1 != nil {
return c.transpilerV1.Close()
}
if c.transpiler != nil {
return c.transpiler.Close()
}
return nil
return c.transpiler.Close()
}

func (c *Client) toCSS(args godartsass.Args, src io.Reader) (godartsass.Result, error) {
in := helpers.ReaderToString(src)

args.Source = in

var (
err error
res godartsass.Result
)

if c.transpilerV1 != nil {
var resv1 godartsassv1.Result
var argsv1 godartsassv1.Args
mapstructure.Decode(args, &argsv1)
if args.ImportResolver != nil {
argsv1.ImportResolver = importResolverV1{args.ImportResolver}
}
resv1, err = c.transpilerV1.Execute(argsv1)
if err == nil {
mapstructure.Decode(resv1, &res)
}
} else {
res, err = c.transpiler.Execute(args)
}

res, err := c.transpiler.Execute(args)
if err != nil {
if err.Error() == "unexpected EOF" {
//lint:ignore ST1005 end user message.
Expand Down
10 changes: 0 additions & 10 deletions resources/resource_transformers/tocss/dartsass/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (

"github.com/gohugoio/hugo/hugofs"

godartsassv1 "github.com/bep/godartsass"
"github.com/bep/godartsass/v2"
)

Expand Down Expand Up @@ -206,12 +205,3 @@ func (t importResolver) Load(url string) (godartsass.Import, error) {

return godartsass.Import{Content: string(b), SourceSyntax: sourceSyntax}, err
}

type importResolverV1 struct {
godartsass.ImportResolver
}

func (t importResolverV1) Load(url string) (godartsassv1.Import, error) {
res, err := t.ImportResolver.Load(url)
return godartsassv1.Import{Content: res.Content, SourceSyntax: godartsassv1.SourceSyntax(res.SourceSyntax)}, err
}

0 comments on commit 86d3c28

Please sign in to comment.