fix: Imports without a name binding (#543)

Handle imports that don't use the `as` clause to create a binding.
These imports are bound to the last identifier in the imported path.

Fix both interpreter and compiler.
Add tests.

fixes #541

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
Anand Krishnamoorthi
2026-01-15 03:57:52 +05:30
committed by GitHub
parent 740db8a0f5
commit 9426b2ec02
4 changed files with 234 additions and 5 deletions

View File

@@ -335,7 +335,18 @@ impl<'a> Compiler<'a> {
}
}
// No rule found - undefined variable
// No rule found; fall back to module-level imports.
let import_key = format!("{}.{}", &self.current_package, root);
if let Some(import_expr) = self.policy.inner.imports.get(&import_key) {
let import_reg =
self.compile_rego_expr_with_span(import_expr, import_expr.span(), false)?;
if chain.components.is_empty() {
return Ok(import_reg);
}
return self.compile_chain_access(import_reg, &chain.components, span);
}
// No rule or import found - undefined variable
Err(CompilerError::UndefinedVariable {
name: root.to_string(),
}