Skip to content

Output zig targets as ZON instead of JSON #22939

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

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 -- -i build.zig.zon -r .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 -r .libc

@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.

@190n
Copy link
Contributor

190n commented Mar 8, 2025

Looks like this didn't make it into the release notes.

@tsujp
Copy link

tsujp commented Mar 24, 2025

But second, because JSON is valuable as a data interoperability format.

JSON sucks for anything larger than a trivial-sized file. A good example that is easy to find is most package.json files in JavaScript projects. Thing's like LSP (Language Server Protocol) using JSON for data interchange that isn't intended to be read by humans add a dependency using a bad serialisation language and result in huge bottlenecks in LSP clients (and text editors) that must now optimise for JSON de(serialisation) or be forced to take on a dependency.

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.

"Even if (any systems-level language after C) is nicer to work with from the perspective of (said new systems-level language), I don't see it ever competing with C as a ubiquitous systems-level language".

We shouldn't stop trying to improve (sensibly) and exploring new ideas just because the new thing won't be ubiquitous. Yes, don't let perfect be the enemy of good enough but also don't let good enough halt any and all attempts at any exploration and progression.

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.

Then those other tools can add support themselves or via their users contributing it. Consider the opposite, Zig must be forced to add a dependency on JSON because... JSON is ubiquitous? Is that the only criterion for acceptance? XML was around before JSON and YAML which were both around before TOML and so forth.

I imagine your text editor can do more than open just ASCII files, perhaps we should dissolve Unicode and require all other non-ASCII formats to conform.

I don't want to start an argument here but I felt the need to comment because things like "it's popular so just use X" and "we shouldn't try new things" are bad mindsets that I think cause more harm than good and stifle progress that can potentially benefit everyone within the Zig ecosystem and outside of it.

T1nk3r1 pushed a commit to T1nk3r1/zig-frictionless that referenced this pull request Apr 5, 2025
* Adds startTupleField/startStructField, makes pattern in print targets less verbose

* Makes some enums into strings

* Start/finish renamed to begin/end

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.
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.

10 participants