kcl icon indicating copy to clipboard operation
kcl copied to clipboard

generate sourcemaps

Open dansrogers opened this issue 1 year ago • 4 comments

Feature Request

Is your feature request related to a problem? Please describe:

kcl can be used to trivially generate yaml files and other structured data. For any given yaml file that lives as part of another ecosystem (e.g. kubernetes manifests, or ansible playbooks), there exist linters that are able to describe errors with the output yaml. While it's straightforward to 'kcl run' then run the output through the linter, to resolve linter errors, I then must then determine the kcl file line number that generated a particular output file line number. Additionally, if I'm using an IDE, the IDE won't be able to display any errors on the line number of the generated source file.

Describe the feature you'd like:

While an end to end solution requires more ecosystem changes, the first step is to have kcl be able to output sourcemaps in addition to it's normal output. Sourcemaps, while part of the ES standard, are language and platform agnostic ways of describing original source locations from generated code.

This could potentially look like:

$ kcl run -o outputfile.yaml --sourcemap outputfile.map

Or using the file api:

file.writemap("outputfile.yaml", yaml.encode(fooboject), "outputfile.map")

After sourcemaps are available, upstream tools can be modified to accept map files, which can be used to print errors that reference the kcl line numbers, or highlight generated file linter errors. This approach is common in the transpiled javascript world.

Describe alternatives you've considered:

The alternative is to generate the files, run tools over the generated files, and map the line number messages back to the generated kcl code manually.

Teachability, Documentation, Adoption, Migration Strategy:

dansrogers avatar Sep 06 '24 20:09 dansrogers

Thank you for opening the issue. I believe this is an important improvement for configuring the ecosystem. 👍

Peefy avatar Sep 09 '24 02:09 Peefy

Hey @Peefy , let me give this a shot! I will first try a CLI flag implementation for this, lets see how that goes!

SkySingh04 avatar Jan 15 '25 17:01 SkySingh04

I have opened a draft PR as I iteratively build the solution for this. Will ping for a review once I am ready!

SkySingh04 avatar Jan 15 '25 18:01 SkySingh04

Hi @Peefy @zong-zhe and team,

I’ve been working on implementing the ability to generate sourcemaps via the CLI, as outlined in this issue. The current implementation tracks and writes the mapping between the source lines and the generated YAML lines. This has taken over 12 hours of work so far, and I’d greatly appreciate any feedback on the approach and implementation details before proceeding further to write tests for it.

Since this feature is part of the v1.0 release milestone (due January 31st), I want to ensure we’re on the right track early on. Let me know your thoughts!

SkySingh04 avatar Jan 18 '25 12:01 SkySingh04