Skip to content

Commit

Permalink
fix: cycle import detection in the interpreter (#1104)
Browse files Browse the repository at this point in the history
* fix: cycle import detection in the interpreter

Signed-off-by: Yilong Li <liyilongko@gmail.com>

* refactor: add comments

Signed-off-by: Yilong Li <liyilongko@gmail.com>

* refactor: return early

Signed-off-by: Yilong Li <liyilongko@gmail.com>

Signed-off-by: Yilong Li <liyilongko@gmail.com>
  • Loading branch information
dragonly authored Oct 28, 2022
1 parent 356a707 commit e831fe9
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/lang/frontend/starlark/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,25 @@ func (s *generalInterpreter) load(thread *starlark.Thread, module string) (starl
}

func (s *generalInterpreter) exec(thread *starlark.Thread, module string) (starlark.StringDict, error) {
// There are two cases:
// 1. module exists
// 2. there's an explicit `nil` placeholder for module in s.cache
// 3. module does not exist in s.cache
e, ok := s.cache[module]

// Case 1.
if e != nil {
return e.globals, e.err
}

// Case 2.
// There is an explicit `nil` for module, which means we are in the middle of exec module.
if ok {
return nil, errors.Newf("Detect cycling import during parsing %s", module)
return nil, errors.Newf("Detected cycle import during parsing %s", module)
}

// Case 3.
// Add a placeholder to indicate "load in progress".
s.cache[module] = nil

if !strings.HasPrefix(module, universe.GitPrefix) {
Expand All @@ -110,6 +121,9 @@ func (s *generalInterpreter) exec(thread *starlark.Thread, module string) (starl
e = &entry{globals, err}
}

// Update the cache.
s.cache[module] = e

return e.globals, e.err
}

Expand Down

0 comments on commit e831fe9

Please sign in to comment.