Skip to content

fix: wire up with_meta to emit __start_line__/__end_line__#2

Open
bejaratommy wants to merge 1 commit into
mainfrom
fix/with-meta-line-numbers
Open

fix: wire up with_meta to emit __start_line__/__end_line__#2
bejaratommy wants to merge 1 commit into
mainfrom
fix/with-meta-line-numbers

Conversation

@bejaratommy

Copy link
Copy Markdown
Owner

Summary

The with_meta serialization option — and the hcl2tojson --with-meta CLI flag — has been a no-op since the v8 architecture overhaul. The flag is accepted and documented ("Add __start_line__ and __end_line__ metadata to each block/attribute"), but no serialize() path ever reads options.with_meta, so the output never contains line metadata.

$ echo 'x = 1' | hcl2tojson - --with-meta
{"x": 1}                      # before
{"x": 1, "__start_line__": 1, "__end_line__": 1}   # after

Change

Lark already runs with propagate_positions=True and the transformer attaches a populated Meta to every rule, so the line numbers are available — they were just never written out.

BodyRule.serialize now emits __start_line__ / __end_line__ on each body dict (the document body and each block body) when with_meta is set, sourced from the rule's propagated Meta. This is a single, uniform injection point that covers both the top-level-attribute case above and block bodies. The keys are added as constants in hcl2/const.py.

The metadata is only emitted when with_meta=True (default False), so default output is unchanged. The empty-Meta case (trees built without positions, e.g. via the deserializer) is guarded, so nothing is emitted there.

Tests

Added test/unit/test_api.py cases asserting the metadata appears with with_meta=True (top-level attribute and block body) and is absent by default.

Fixes amplify-education#291

The with_meta serialization option (and the --with-meta CLI flag) was
accepted but had no effect since the v8 overhaul: no serialize path
ever read options.with_meta, so output never contained line metadata.

Emit __start_line__/__end_line__ keys on each body dict (document body
and block bodies) from the positions Lark already propagates, matching
the option's documented behavior. Only active when with_meta is set, so
default output is unchanged.

Fixes amplify-education#291
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.

hcl2tojson --with-meta flag accepted but produces no metadata

1 participant