diff --git a/engine/text.go b/engine/text.go index 9e00b065..0ff37199 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) {