From c33bef07164a89a8ed923beb83cc9f863872dad9 Mon Sep 17 00:00:00 2001 From: Yutaka Ichibangase Date: Fri, 4 Oct 2024 10:12:23 +0900 Subject: [PATCH] avoid recursive file load. --- engine/text.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/engine/text.go b/engine/text.go index 9e00b06..0ff3719 100644 --- a/engine/text.go +++ b/engine/text.go @@ -192,11 +192,16 @@ func (vm *VM) ensureLoaded(ctx context.Context, file Term, env *Env) error { if _, ok := vm.loaded[f]; ok { return nil } - defer func() { - vm.loaded[f] = struct{}{} - }() - return vm.Compile(ctx, string(b)) + // It's too early to say it's fully loaded. Yet this avoids recursive load of the same file. + vm.loaded[f] = struct{}{} + + if err := vm.Compile(ctx, string(b)); err != nil { + delete(vm.loaded, f) // It wasn't fully loaded after all. + return err + } + + return nil } func (vm *VM) open(file Term, env *Env) (string, []byte, error) {