prql icon indicating copy to clipboard operation
prql copied to clipboard

Weird module resolution issues

Open tantmd opened this issue 6 months ago • 1 comments

Was reading through the docs and playing with the module X { ... } syntax in the playground. Two things didn't match what I expected from the spec. Flagging in case they're real, not sure if they're known.

Bare names from an enclosing module aren't visible inside a nested module:

module a {
  module b {
    let bar = 3
    module c {
      let scaled = bar * 2
    }
  }
}
from u | derive {x = a.b.c.scaled}

Errors with Unknown name `bar` on line 5. Works if I qualify as a.b.bar. The spec page (web/book/src/reference/spec/modules.md) reads:

If an identifier cannot be resolved relative to the current module, it tries to resolve relative to the parent module. This is repeated, stepping up the module hierarchy until a match is found or root of the module structure is reached.

which sounds like the bare reference should find bar in a.b.

Second one, this is the one that caught me off guard. Bare names can pick up an unrelated root-level module if its name happens to match a path segment of the current location:

module b {
  module c {
    let bar = 99
  }
}
module a {
  module b {
    module c {
      let scaled = bar * 2
    }
  }
}
from u | derive {x = a.b.c.scaled}

Compiles fine, output has 99 * 2 AS x. The bar inside a.b.c silently resolved to b.c.bar at the root (a sibling of a with no logical relation).

I looked at prqlc/prqlc/src/semantic/resolver/names.rs to see what was going on. The lookup loop looks like:

let mut ident = ident.clone().prepend(self.current_module_path.clone());
let mut res = self.resolve_ident_core(&ident, None);
for _ in 0..self.current_module_path.len() {
    if res.is_ok() { break; }
    ident = ident.pop_front().1.unwrap();    // peels OUTER segment
    res = self.resolve_ident_core(&ident, None);
}

pop_front strips the outermost path segment, so for bar inside a.b.c the walk is a.b.c.bar → b.c.bar → c.bar → bar. If the intent really is "step up the parent module" as the spec says, I'd expect peeling from the back instead — a.b.c.bar → a.b.bar → a.bar → bar.

I see that the spec page already says "the module facility is in discussion" and the module name { ... } block syntax isn't really part of the language, so probably not that important but report here so you guys know.

tantmd avatar Jun 02 '26 10:06 tantmd

Thanks for the careful report — your root-cause analysis is correct. The pop_front walk strips the outermost segment instead of the innermost ancestor segment, so for a bare bar inside a.b.c it walks a.b.c.bar → b.c.bar → c.bar → bar rather than the spec-mandated a.b.c.bar → a.b.bar → a.bar → bar. That explains both surprises — the false negative on ancestor lookup and the false positive where an unrelated root sibling silently won.

I've opened #5976 with the fix and two snapshot tests covering both cases. A maintainer will review it.

prql-bot avatar Jun 02 '26 10:06 prql-bot