Skip to content

bug: Risor compiler.WithGlobals with any name other than "ctx" makes every Eval fail #158

Description

@robbyt

Summary

Declaring an extra global on the Risor compiler via WithGlobals([]string{"request"}) makes every Eval fail with missing required globals: [request], even for scripts that never reference the global. This contradicts the option's documentation ("declare additional global identifiers the script may reference at evaluation time").

Root cause

  • engines/risor/compiler/internal/compile/compile.go:39-55 puts custom globals into the compile-time env, so Risor v2 records them in Code.EnvKeys().
  • engines/risor/evaluator/evaluator.go:131 builds the runtime env as only Builtins() + ctx (internal.BuildRisorEnv), so the declared global is absent at run time.
  • Risor v2's RunvalidateGlobals then rejects the run because a declared global key is missing from the runtime env.

Likely a Risor v1→v2 migration regression: v2 added strict global validation that v1 lacked. Starlark's equivalent degrades gracefully — it fails only if the script actually references the missing name.

Reproduction (confirmed)

Compiler built with WithCtxGlobal(), WithGlobals([]string{"request"}); script 1 + 2 (never references request):

exec error: risor execution error: missing required globals: [request] (...)

Impact

High — a public compiler API is unconditionally broken. Any use of WithGlobals with a name other than ctx makes evaluation impossible.

Fix

Plumb the declared globals list through the executable and have the evaluator inject nil/None placeholders for each into the runtime env, satisfying validateGlobals. Alternatively, reject non-ctx globals at compiler construction with a clear error. Add tests: declare a global and don't reference it (assert Eval succeeds); declare, reference, and supply it (assert the value is visible).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions