Skip to content

fix(render): align render.k imports with the rest of konfig - #36

Merged
Peefy merged 5 commits into
kcl-lang:mainfrom
Peefy:fix/render-issues
Aug 24, 2026
Merged

Peefy merged 5 commits into
kcl-lang:mainfrom
Peefy:fix/render-issues

Conversation

@Peefy

@Peefy Peefy commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Closes the second-half of https://github.com/orgs/kcl-lang/discussions/2127 — once PR kcl-lang/modules#392 lands, konfig users on k8s 1.14 - 1.31 are unblocked on the import aliasing side, but adding render.k to the k_filename_list still fails with two new errors that live entirely in this repo.

What fails today

$ kcl run examples/appops/nginx-example/base/base.k \
        examples/appops/nginx-example/dev/main.k \
        models/kube/render/render.k

error[E2L23]: the name 'frontend' is defined multiple times
 --> models/kube/render/render.k:1:1
1 | import models.kube.frontend
  | ^

error[E2G22]: expected [Volume], got [Volume]
 --> models/kube/backend/job_backend.k:40:44
40 | mainContainer = utils.volume_patch(config.volumes, ...)
   |                       ^
 --> models/kube/utils/volume_patch.k:5:23
5 | volume_patch = lambda volumes: [volume.Volume], ...

Why

crates/sema/src/resolver/import.rs:139 does a literal-string comparison on the import path — there is no normalization. The example files all write:

import konfig.models.kube.frontend

while render.k writes:

import models.kube.frontend

Both resolve to the same module, but the literal strings differ, so the check raises E2L23. Worse, the duplicate frontend symbol with two different pkgpaths keeps the renderer from consolidating Volume types — KCL compares types by full pkgpath, so models.kube.frontend.volume.Volume (reached via render.k) and konfig.models.kube.frontend.volume.Volume (reached via base.k → frontend.Job.volumes) become distinct identities, and E2G22 fires on volume_patch.

Fix

Align render.k with the rest of the repo:

-import models.kube.frontend
-import models.kube.frontend.rbac
-import models.kube.backend
-import models.kube.resource
+import konfig.models.kube.frontend
+import konfig.models.kube.frontend.rbac
+import konfig.models.kube.backend
+import konfig.models.kube.resource

import manifests is left alone — it is a KCL built-in pseudo-module (manifests.yaml_stream is registered as a built-in in crates/sema/src/builtin/system_module.rs), not a real package on disk.

Verification

Reproduced with kcl 0.12.3 against the konfig checkout (the latest version where the example harness still passes):

$ kcl run examples/appops/nginx-example/base/base.k \
          examples/appops/nginx-example/dev/main.k \
          models/kube/render/render.k

apiVersion: apps/v1
kind: Deployment
metadata: ...
---
apiVersion: v1
kind: Namespace
metadata: ...
---
apiVersion: v1
kind: Service
metadata: ...

guestbook/dev, guestbook/prod, guestbook/test all render multi-doc YAML as well.

About the CI Example tests failure

The Example tests job fails on every single example directory on both main and this branch when running on kcl 0.12.8:

EvaluationError
  --> models/kube/backend/server_backend.k:67:1
67 |             replicas = config.replicas
   | invalid value 'UndefinedType' to load attribute 'replicas'

Verified locally with kcl v0.12.8 downloaded from kcl-lang/cli:

  • git checkout main and run ./scripts/test.sh → all 20 examples FAIL with the same UndefinedType error
  • git checkout fix/render-issues → identical failure
  • The same ./scripts/test.sh passes all 20 examples on kcl v0.12.3

Root cause is in the KCL compiler, not konfig — frontend.Server.replicas is declared as replicas: int = option(\"replicas\") or 1, and kcl 0.12.8 is no longer evaluating the or 1 default when config.replicas is accessed through the backend rule pipeline. Fixing that is a separate concern that belongs in kcl-lang/kcl, not in this PR. Happy to file an upstream issue / PR if helpful, or to open a follow-up PR here that pins CI to kcl 0.12.3 until the regression is fixed upstream.

@Peefy
Peefy force-pushed the fix/render-issues branch from 2f75a33 to 2425879 Compare August 24, 2026 09:29
Peefy added 2 commits August 24, 2026 17:31
Two changes:

1. Convert render.k and server_backend.k to use the
   `konfig.models.kube.frontend` package-qualified import path
   so that the Server / Volume types referenced inside konfig carry
   the same pkgpath that user code sees when it imports the package.
   Without this, the same short name (`frontend`) or schema name
   (`Server` / `Volume`) registers under two different pkgpaths
   depending on whether the importer uses the relative or the
   absolute form, and KCL's import-name dedup and structural type
   checker raise E2L23 / E2G22.

2. Pre-compute `config.replicas` into `_replicas` before the
   `workloadAttributes` dict literal. Reading `config.replicas`
   inside the same dict literal that calls
   `utils.MetadataBuilder(config) | {...}` (the metadata key is set
   first via a lambda+merge) triggers an evaluator quirk where the
   attribute is resolved to `Undefined`. Pre-computing into a local
   variable avoids the bad read.

Tested locally: `kcl run` succeeds for the nginx-example stack
(Deployment / Namespace / Service emitted as YAML).

Closes #2127 (konfig-side half).

Signed-off-by: Peefy <xpf6677@163.com>
The previous commit fixed `replicas` by pre-computing it to `_replicas`.
But the same evaluator quirk hits every `config.X` read inside a dict
literal that also contains `MetadataBuilder(config) | {...}` — CI
failed on `config.useBuiltInSelector` next.

Extract the whole spec sub-dict into its own `_spec` variable so all
`config.X` reads sit outside the dict-with-merge pattern. Then
`workloadAttributes` only references the already-built `_spec`.

Signed-off-by: Peefy <xpf6677@163.com>
@Peefy
Peefy force-pushed the fix/render-issues branch from 8716155 to 555800b Compare August 24, 2026 09:31
Peefy added 3 commits August 24, 2026 17:34
The bare `config?.labels` (no `or` fallback) returns Undefined when
the attribute is absent. With kcl 0.12.x, an Undefined value leaking
out of a lambda call placed inside a dict literal breaks sibling
key reads: any subsequent `something.X` in the same dict literal
resolves to Undefined.

Example failure surface: server_backend.k:67 (config.replicas),
service_mixin.k:15 (len(config.services)), and other call sites of
`MetadataBuilder(_x) | {...}`.

Return `{}` instead of Undefined for labels/annotations. This matches
K8s semantics (labels/annotations should be a map, never None) and
removes the root cause for every caller, so individual workarounds
in server_backend.k become unnecessary.

Signed-off-by: Peefy <xpf6677@163.com>
Apply targeted workarounds for the evaluator regression where a
dict literal that contains both `MetadataBuilder(_) | {...}` and
sibling-key `config.X` reads (or `something?.X or (...config.Y...)`)
resolves the bare attribute access to `Undefined`.

- service_mixin.k: pre-compute `_singleService` outside the
  metadata override so `len(config.services)` isn't evaluated
  in the broken pattern.
- job_backend.k: convert relative imports to package-qualified
  `konfig.*` (same E2G22/E2L23 fix as server_backend.k) and extract
  the `spec` dict to its own statement so `config.X` reads are not
  co-located with the `MetadataBuilder(config) | {...}` key.

Server-side fix at MetadataBuilder level alone was insufficient
because the override-dict's value expression also triggers the bug.
A complete root-cause fix needs the KCL evaluator; this commit
keeps konfig green across both kcl 0.11.x and 0.12.x.

Signed-off-by: Peefy <xpf6677@163.com>
…dict

In kcl 0.12.x, evaluating `_s?.selector or kubernetes?[config.workloadType]?...`
inside the Service spec dict (which is a sibling key of
`metadata = MetadataBuilder(_) | {...}`) resolves `config` itself
to Undefined. Pre-computing `config.workloadType` into `_workloadType`
before the dict avoids the bad read.

Signed-off-by: Peefy <xpf6677@163.com>
@Peefy
Peefy merged commit 29910fc into kcl-lang:main Aug 24, 2026
5 checks passed
Peefy added a commit to Peefy/konfig-1 that referenced this pull request Aug 24, 2026
Now that kcl-lang/kcl#2130 is fixed upstream (commit 703b8ead:
"fix(evaluator): restore local_vars after call, don't extend"), the
workarounds introduced in PR kcl-lang#36 to keep the runtime usable against
that bug are no longer required.

Removes:
- the inline `{}` fallback on labels/annotations in
  `models.kube/utils/metadata_builder.k`
- the pre-computed `_singleService`/`_workloadType` locals in
  `models/kube/mixins/service_mixin.k`
- the isolated `_spec` dict extraction in
  `models/kube/backend/server_backend.k`

This restores the simpler, native dict-literal/merge-operator code
that the library was originally written against.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: Peefy <xpf6677@163.com>
Peefy added a commit that referenced this pull request Aug 29, 2026
Now that kcl-lang/kcl#2130 is fixed upstream (commit 703b8ead:
"fix(evaluator): restore local_vars after call, don't extend"), the
workarounds introduced in PR #36 to keep the runtime usable against
that bug are no longer required.

Removes:
- the inline `{}` fallback on labels/annotations in
  `models.kube/utils/metadata_builder.k`
- the pre-computed `_singleService`/`_workloadType` locals in
  `models/kube/mixins/service_mixin.k`
- the isolated `_spec` dict extraction in
  `models/kube/backend/server_backend.k`

This restores the simpler, native dict-literal/merge-operator code
that the library was originally written against.

Signed-off-by: Peefy <xpf6677@163.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant