create: Make sure the build lock is released before we open editor

Fixes #9121
This commit is contained in:
Bjørn Erik Pedersen 2021-11-04 08:57:11 +01:00
parent 82c33c7105
commit 166862a096

View file

@ -56,11 +56,6 @@ func NewContent(h *hugolib.HugoSites, kind, targetPath string) error {
if h.BaseFs.Content.Dirs == nil {
return errors.New("no existing content directory configured for this project")
}
unlock, err := h.BaseFs.LockBuild()
if err != nil {
return fmt.Errorf("failed to acquire a build lock: %s", err)
}
defer unlock()
cf := hugolib.NewContentFactory(h)
@ -83,22 +78,42 @@ func NewContent(h *hugolib.HugoSites, kind, targetPath string) error {
b.setArcheTypeFilenameToUse(ext)
withBuildLock := func() (string, error) {
unlock, err := h.BaseFs.LockBuild()
if err != nil {
return "", fmt.Errorf("failed to acquire a build lock: %s", err)
}
defer unlock()
if b.isDir {
return b.buildDir()
return "", b.buildDir()
}
if ext == "" {
return errors.Errorf("failed to resolve %q to a archetype template", targetPath)
return "", errors.Errorf("failed to resolve %q to a archetype template", targetPath)
}
if !files.IsContentFile(b.targetPath) {
return errors.Errorf("target path %q is not a known content format", b.targetPath)
return "", errors.Errorf("target path %q is not a known content format", b.targetPath)
}
return b.buildFile()
}
filename, err := withBuildLock()
if err != nil {
return err
}
if filename != "" {
return b.openInEditorIfConfigured(filename)
}
return nil
}
type contentBuilder struct {
archeTypeFs afero.Fs
sourceFs afero.Fs
@ -195,15 +210,15 @@ func (b *contentBuilder) buildDir() error {
return nil
}
func (b *contentBuilder) buildFile() error {
func (b *contentBuilder) buildFile() (string, error) {
contentPlaceholderAbsFilename, err := b.cf.CreateContentPlaceHolder(b.targetPath)
if err != nil {
return err
return "", err
}
usesSite, err := b.usesSiteVar(b.archetypeFilename)
if err != nil {
return err
return "", err
}
var contentInclusionFilter *glob.FilenameFilter
@ -216,16 +231,16 @@ func (b *contentBuilder) buildFile() error {
}
if err := b.h.Build(hugolib.BuildCfg{NoBuildLock: true, SkipRender: true, ContentInclusionFilter: contentInclusionFilter}); err != nil {
return err
return "", err
}
if err := b.applyArcheType(contentPlaceholderAbsFilename, b.archetypeFilename); err != nil {
return err
return "", err
}
b.h.Log.Infof("Content %q created", contentPlaceholderAbsFilename)
return b.openInEditorIfConfigured(contentPlaceholderAbsFilename)
return contentPlaceholderAbsFilename, nil
}
func (b *contentBuilder) setArcheTypeFilenameToUse(ext string) {