diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-11-04 08:57:11 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2021-11-04 15:40:02 +0100 |
commit | 166862a096e1d920f46b18c600e8dadcb478b839 (patch) | |
tree | 0cb95fc4657ba8c1937aa017d4f4014e8c7d58e6 /create/content.go | |
parent | 82c33c7105a4d0c0e097f0d074c02995cb2b4d20 (diff) |
create: Make sure the build lock is released before we open editor
Fixes #9121
Diffstat (limited to 'create/content.go')
-rw-r--r-- | create/content.go | 51 |
1 files changed, 33 insertions, 18 deletions
diff --git a/create/content.go b/create/content.go index 017f97e3d..a629d6553 100644 --- a/create/content.go +++ b/create/content.go @@ -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,19 +78,39 @@ func NewContent(h *hugolib.HugoSites, kind, targetPath string) error { b.setArcheTypeFilenameToUse(ext) - if b.isDir { - return b.buildDir() + 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() + } + + if ext == "" { + 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 b.buildFile() + } - if ext == "" { - return errors.Errorf("failed to resolve %q to a archetype template", targetPath) + filename, err := withBuildLock() + if err != nil { + return err } - if !files.IsContentFile(b.targetPath) { - return errors.Errorf("target path %q is not a known content format", b.targetPath) + if filename != "" { + return b.openInEditorIfConfigured(filename) } - return b.buildFile() + return nil } @@ -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) { |