Skip to content

Commit d54edd8

Browse files
committed
postCSS: Improve validation of option 'config'
1 parent 635cc34 commit d54edd8

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

Diff for: hugolib/filesystems/basefs.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,19 @@ func (b *BaseFs) AbsProjectContentDir(filename string) (string, string, error) {
188188

189189
// ResolveJSConfigFile resolves the JS-related config file to a absolute
190190
// filename. One example of such would be postcss.config.js.
191-
func (fs *BaseFs) ResolveJSConfigFile(name string) string {
191+
func (fs *BaseFs) ResolveJSConfigFile(name string) (string, bool) {
192192
// First look in assets/_jsconfig
193193
fi, err := fs.Assets.Fs.Stat(filepath.Join(files.FolderJSConfig, name))
194194
if err == nil {
195-
return fi.(hugofs.FileMetaInfo).Meta().Filename
195+
return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir()
196196
}
197197
// Fall back to the work dir.
198198
fi, err = fs.Work.Stat(name)
199199
if err == nil {
200-
return fi.(hugofs.FileMetaInfo).Meta().Filename
200+
return fi.(hugofs.FileMetaInfo).Meta().Filename, fi.IsDir()
201201
}
202202

203-
return ""
203+
return "", false
204204
}
205205

206206
// MakePathRelative creates a relative path from the given filename.

Diff for: resources/resource_transformers/babel/babel.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,19 @@ func (t *babelTransformation) Transform(ctx *resources.ResourceTransformationCtx
134134
}
135135

136136
configFile = filepath.Clean(configFile)
137+
isConfigFileDir := false
137138

138139
// We need an absolute filename to the config file.
139140
if !filepath.IsAbs(configFile) {
140-
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
141-
if configFile == "" && t.options.Config != "" {
142-
// Only fail if the user specified config file is not found.
143-
return fmt.Errorf("babel config %q not found:", configFile)
141+
configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile)
142+
if t.options.Config != "" {
143+
if configFile == "" {
144+
// Only fail if the user specified config file is not found.
145+
return fmt.Errorf("babel config file %q not found", configFile)
146+
}
147+
if isConfigFileDir {
148+
loggers.Log().Warnf("babel config %q must be a file, not a directory", configFile)
149+
}
144150
}
145151
}
146152

Diff for: resources/resource_transformers/postcss/postcss.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,19 @@ func (t *postcssTransformation) Transform(ctx *resources.ResourceTransformationC
173173
}
174174

175175
configFile = filepath.Clean(configFile)
176+
isConfigFileDir := false
176177

177178
// We need an absolute filename to the config file.
178179
if !filepath.IsAbs(configFile) {
179-
configFile = t.rs.BaseFs.ResolveJSConfigFile(configFile)
180-
if configFile == "" && options.Config != "" {
181-
// Only fail if the user specified config file is not found.
182-
return fmt.Errorf("postcss config %q not found:", options.Config)
180+
configFile, isConfigFileDir = t.rs.BaseFs.ResolveJSConfigFile(configFile)
181+
if options.Config != "" {
182+
if configFile == "" {
183+
// Only fail if the user specified config file is not found.
184+
return fmt.Errorf("postcss config directory %q not found", options.Config)
185+
}
186+
if !isConfigFileDir {
187+
loggers.Log().Warnf("postcss config %q must be a directory", options.Config)
188+
}
183189
}
184190
}
185191

0 commit comments

Comments
 (0)