From 9a0370e8eb71fed3ac04984020b6aa95c43f22ab Mon Sep 17 00:00:00 2001 From: Andreas Deininger Date: Mon, 22 May 2023 18:14:10 +0200 Subject: [PATCH] postcss: Improve validation of option 'config' --- hugolib/filesystems/basefs.go | 8 ++++---- resources/resource_transformers/babel/babel.go | 8 ++++++-- resources/resource_transformers/postcss/postcss.go | 8 ++++++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/hugolib/filesystems/basefs.go b/hugolib/filesystems/basefs.go index ed0d36de1..8882ed3fe 100644 --- a/hugolib/filesystems/basefs.go +++ b/hugolib/filesystems/basefs.go @@ -188,19 +188,19 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string, error) { // ResolveJSConfigFile resolves the JS-related config file to a absolute // filename. One example of such would be postcss.config.js. -func (fs *BaseFs) ResolveJSConfigFile(name string) string { +func (fs *BaseFs) ResolveJSConfigFile(name string) (string, bool) { // First look in assets/_jsconfig fi, err := fs.Assets.Fs.Stat(filepath.Join(files.FolderJSConfig, name)) if err == nil { - return fi.(hugofs.FileMetaInfo).Meta().Filename + return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir() } // Fall back to the work dir. fi, err = fs.Work.Stat(name) if err == nil { - return fi.(hugofs.FileMetaInfo).Meta().Filename + return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir() } - return "" + return "", false } // MakePathRelative creates a relative path from the given filename. diff --git a/resources/resource_transformers/babel/babel.go b/resources/resource_transformers/babel/babel.go index ff19d9dda..5ed09d200 100644 --- a/resources/resource_transformers/babel/babel.go +++ b/resources/resource_transformers/babel/babel.go @@ -134,13 +134,17 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx } configFile = filepath.Clean(configFile) + isConfigFileDir := false // We need an absolute filename to the config file. if !filepath.IsAbs(configFile) { - configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile) + configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile) + if isConfigFileDir { + logger.Warnf("babel config %q must be a file, not a directory", configFile) + } if configFile == "" && t.options.Config != "" { // Only fail if the user specified config file is not found. - return fmt.Errorf("babel config %q not found:", configFile) + return fmt.Errorf("babel config file %q not found", configFile) } } diff --git a/resources/resource_transformers/postcss/postcss.go b/resources/resource_transformers/postcss/postcss.go index 376d72182..ee3f39be1 100644 --- a/resources/resource_transformers/postcss/postcss.go +++ b/resources/resource_transformers/postcss/postcss.go @@ -172,13 +172,17 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC } configFile = filepath.Clean(configFile) + isConfigFileDir := false // We need an absolute filename to the config file. if !filepath.IsAbs(configFile) { - configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile) + configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile) if configFile == "" && options.Config != "" { // Only fail if the user specified config file is not found. - return fmt.Errorf("postcss config %q not found:", options.Config) + return fmt.Errorf("postcss config directory %q not found", options.Config) + } + if !isConfigFileDir { + logger.Warnf("postcss config %q must be a directory", options.Config) } }