Skip to content

Evaluator regression (0.12.x): sibling-key reads inside a dict literal resolve to Undefined when the dict also contains a lambda-call + merge key #2130

Closed

Description

@Peefy

Evaluator regression: sibling-key reads inside a dict literal resolve to Undefined when the dict also contains a lambda-call + merge key

Summary

In KCL 0.12.x, evaluating certain expressions inside a dict literal
silently (or loudly) resolves to Undefined even though the referenced
variable is bound. The trigger pattern is a dict literal whose one
key is a lambda call followed by | and an override dict
, and
whose sibling key reads an attribute from the surrounding scope
— most commonly config.X where config is a schema instance.

This is a regression: the same code renders correctly under KCL 0.11.x.

The konfig project (kcl-lang/konfig#36) hit this and had to ship a
multi-file workaround; reporting here so it can be fixed at the
evaluator.

Minimal reproducer (kcl 0.12.8)

schema Server:
    name: str = "x"
    workloadType: str = "Deployment"

MetaBuilder = lambda c -> {str:} {
    {
        name: c?.name or "default"
        labels: c?.labels  # bare optional access, no fallback
    }
}

config_dict = {
    name = "x"
    workloadType = "Deployment"
}

my_dict = {
    metadata = MetaBuilder(config_dict) | {name = "override"}
    spec = {
        # reading config_dict.workloadType silently returns Undefined
        selector = config_dict?.services or config_dict[config_dict.workloadType]
    }
}

print("spec.selector:", my_dict.spec.selector)

Observed (kcl 0.12.8):

spec.selector: Undefined

Expected (kcl 0.11.x):

spec.selector: Undefined   # config_dict has no "Deployment" key
                            # so config_dict[config_dict.workloadType]
                            # is Undefined, and the `or` short-circuits
                            # to the same Undefined — this matches.

But it gets worse in the actual mixin pattern

Once a sibling-key read inside the same dict literal goes through an
optional-access + or chain with config.X on the right-hand side,
the bug also produces the loud
invalid value 'UndefinedType' to load attribute 'X' panic from
crates/runtime/src/value/val_attr.rs:82. Example from
models/kube/mixins/service_mixin.k:34 (which broke CI):

metadata: utils.MetadataBuilder(_s) | {
    name = _s?.name or (...)
    namespace = _s?.namespace or (...)
}
spec: {
    ...
    # bug: `config.workloadType` returns Undefined because the metadata
    # key above (lambda call + merge) poisons the surrounding context
    selector = _s?.selector or kubernetes?[config.workloadType]?[0]?.spec?.selector?.matchLabels
    ...
}

Observed failure (kcl 0.12.8)

EvaluationError
  --> models/kube/mixins/service_mixin.k:34:1
   |
34 |                     selector = _s?.selector or kubernetes?[config.workloadType]?[0]?.spec?.selector?.matchLabels
   | invalid value 'UndefinedType' to load attribute 'workloadType'

Trigger conditions

Both of the following must hold:

  1. The dict literal contains a key whose value is LambdaCall(args) | {...}
    (or any other call expression whose evaluation can return Undefined).
  2. A sibling key value reads an attribute through the outer scope
    (config.X, something.workloadType, len(config.services), …).

Order matters: putting the lambda+merge key AFTER the attribute read
sidesteps the bug. Pre-computing the attribute into a local variable
before the dict also sidesteps it. Removing the ?.labels /
?.annotations optional accesses from the lambda body (so the lambda
never returns Undefined) avoids the silent-Undefined case but not
always the explicit panic case.

Impacted surface

  • konfig v0.11+ on kcl 0.12.x: every example under examples/appops/,
    examples/server/, examples/job/ rendered an
    invalid value 'UndefinedType' to load attribute 'X' until PR fix: ast printer on comment and lambda nodes because of the parser is… #36
    shipped per-call-site workarounds.
  • Any KCL code that combines a MetadataBuilder(config) | {...}
    pattern with a sibling-key attribute read inside a dict literal in
    the same schema, mixin, or dict-comprehension.

Workaround applied in konfig (PR #36)

  1. Use konfig.models.kube.frontend as frontend / konfig.* package-qualified
    imports in models/kube/render/render.k and models/kube/backend/{server,job}_backend.k
    so the schema pkgpaths match what user code sees (separate E2L23
    / E2G22 issue, also tracked).
  2. Extract the spec sub-dict to its own _spec variable in
    server_backend.k / job_backend.k so the sibling config.X
    reads sit outside the dict that contains
    MetadataBuilder(config) | {...}.
  3. Pre-compute len(config.services), config.workloadType, etc. in
    each affected mixin (service_mixin.k, ...) before the inner
    list-comprehension dict.
  4. Provide an or {} fallback for labels / annotations in
    models/kube/utils/metadata_builder.k so the lambda never returns
    Undefined.

After all four, the 20-example test suite under kcl 0.12.8 passes.

KCL version

  • 0.12.4 (kcl_lib 0.12.4) — broken (silent Undefined + loud panic)
  • 0.12.8 (latest cli release as of 2026-08-24) — broken
  • 0.11.2 — works correctly

Suggested area to investigate

crates/runtime/src/value/val_attr.rs:82 is where the loud panic
originates (invalid value 'UndefinedType' to load attribute 'X').
The actual root cause is likely upstream in the evaluator's dict /
merge handling — probably in
crates/evaluator/src/{node,scope,lazy,schema}.rs. My best guess:
the lambda-call result (which can be Undefined for missing keys)
leaks into the surrounding dict's scope/snapshot and breaks subsequent
attribute loads.

Happy to bisect or supply more minimal cases if it helps.

Activity

  1. added
    bug
    on Aug 24, 2026
  2. added a commit that references this issue on Aug 24, 2026
    703b8ea
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions