Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output zig targets as ZON instead of JSON #22939

Merged
merged 5 commits into from
Feb 22, 2025

Conversation

MasonRemaley
Copy link
Contributor

@MasonRemaley MasonRemaley commented Feb 19, 2025

As well as outputting ZON instead of JSON, this PR:

  • Adds a few convenience functions to stringify to make this nicer
  • Renames start*/finish* to begin*/end* in stringify
    • This is more consistent with the JSON API, and with other APIs out in the wild that follow this pattern
    • I didn't mark the old names as deprecated since they were live for such a short period, let me know if I should

I verified the output by diffing it with the JSON and glancing over it, and checking that it parses as ZON with the runtime parser.

The compile time parser on #22907 crashes on it right now, this is a bug there looking into it on that branch. (fixed)

@alexrp
Copy link
Member

alexrp commented Feb 19, 2025

I guess this makes sense, though I'll be a little sad to see zig targets | jq -r .libc[] go... anyone want to implement zq real quick?

I feel bad changing this, but I don't know why I named them this way in the first place.
Begin/end is consistent with the json API, and with other APIs in the wild that follow this pattern.
Better to change now than later.
@MasonRemaley
Copy link
Contributor Author

MasonRemaley commented Feb 19, 2025

Hmm. If this reduces the utility of zig targets, it might make sense to wait--alternatively maybe it's good motivation for more tooling to be developed. I don't personally feel strongly either way.

(The changes to stringify.zig should probably make it in for 0.14 regardless, if we decide not to make this change I'm happy to pull them out.)

@Rexicon226
Copy link
Contributor

Why not just have both? Put json behind a --json flag.

@tensorush
Copy link
Contributor

tensorush commented Feb 19, 2025

I'll be a little sad to see zig targets | jq -r .libc[] go... anyone want to implement zq real quick?

Implemented very basic querying: https://codeberg.org/tensorush/zq

Examples (when run in zq repo):

$ zig build exe -- -f build.zig.zon .name
"zq"
$ cat build.zig.zon | zig build exe -- .dependencies.clap
.url = "git+https://github.com/Hejsil/zig-clap#e730233775300c0a8fe3137af880c7c58340930f"
.hash = "12205ef27929fba18101750677ff579c0b6aff670755cacbbe6f2bc5f073622e1783"

zig targets would potentially work like this:

zig targets | zq .libc

UPD: I decided to preserve original ZON value formatting, in order to tell values apart (e.g. enum_literal and string -> .enum_literal and "string"). And also it now prints struct literals' immediate values.

@andrewrk andrewrk merged commit 339b628 into ziglang:master Feb 22, 2025
9 checks passed
@MasonRemaley
Copy link
Contributor Author

@tensorush nice, this looks very useful!

@perillo
Copy link
Contributor

perillo commented Feb 22, 2025

What is the rationale for using ZON instead of JSON?

Thanks.

@mlugg mlugg added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. release notes This PR should be mentioned in the release notes. labels Feb 22, 2025
@MatthiasPortzel
Copy link

MatthiasPortzel commented Feb 22, 2025

This change grates on me for two reasons. As mentioned, this is a breaking change with no rational, other than "Not invented here."

But second, because JSON is valuable as a data interoperability format. Even if ZON is suitable for serialization and deserialization, and even if it's nicer to work with from the perspective of the Zig programmer, I don't see it ever competing with JSON as a ubiquitous data exchange language. And I didn't think that was a goal. For programmatic output that is meant to be consumed by other tools, it makes sense to use a data format that is easy for those tools to understand.

Edit: This is a drive-by critical comment in an unimportant part of the compiler, so I just want to also say thanks to the Zig team for their work towards 0.14 and thanks to @MasonRemaley for their work on ZON.

@andrewrk
Copy link
Member

The rationale is to promote zon as a data interchange format.

It's also nice to remove json dependency from the compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. release notes This PR should be mentioned in the release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants