Skip to content

ship even more of the compiler in source form #19063

Open

Description

@andrewrk

You know what's really pleasant to work on? The build system. The zig build system is shipped in source form, and when you run zig build, zig compiles the build runner from source, and then runs it. In order to do that, the compiler needs only 2 capabilities: compiling source into a native executable, and executing child processes.

Here are some components that could be shipped in source form rather than compiled into the zig binary:

  • zig fmt
  • zig reduce
  • translate-c
  • aro
  • package fetching
  • Autodoc
  • building glibc stubs
  • building libc++, libcxxabi, libtsan, libunwind
  • building musl, mingw, and wasi-libc
  • libc installation detection
  • msvc installation detection
  • objcopy
  • resinator
  • all the machine code backends other than the one used to produce native executables separate proposal

All of these components have simple interfaces that could be communicated over IPC or the file system. I admit however that last one is a bit spicy.

Implementing this issue would accomplish the following things:

  • Significantly decrease compiler build times
  • Make it easier to contribute to zig because no compiler rebuild would be needed in order to test edits to any of these components.
  • Smaller installation size. Source code is surprisingly compact and compresses extraordinarily well. For example, the xz-compressed zig compiler executable built with -Denable-llvm=false -Doptimize=ReleaseSmall on x86_64 is 2.9 MiB (9.9 MiB uncompressed), while the compiler source code catted together into an xz stream is 1.6 MiB.
  • The lazily built executables can take full advantage of native CPU features. We can have AOT cake and eat JIT too!

Activity

  1. added
    proposal
    on Feb 24, 2024
  2. added this to the 0.13.0 milestone on Feb 24, 2024
  3. VisenDev commented on Feb 24, 2024

    @VisenDev

    Possibly related proposal

    #18834.

    This proposal would allow other zig projects to ship zig source code rather than compiled binaries as well.

  4. der-teufel-programming commented on Feb 24, 2024

    @der-teufel-programming
    Contributor

    Shipping current version of Autodoc in source form would require also shipping ZIR and parts of InternPool

  5. mikdusan commented on Feb 25, 2024

    @mikdusan
    Member

    I like this. It's always irked me that we have things like src/musl.zig that has sort of poor-mans build logic when the build system already exists.

  6. nektro commented on Feb 25, 2024

    @nektro
    Contributor

    because that logic uses the cache but needs to be callable by build-exe etc. making them use the actual build system is a good idea imo but will require a medium amount of work.

  7. nektro commented on Feb 25, 2024

    @nektro
    Contributor

    does this proposal being implemented mean for example that zig reduce would start shipping as zig-reduce and/or the zig binary should check PATH for binaries to ensure the space version still works?

  8. andrewrk commented on Feb 26, 2024

    @andrewrk
    MemberAuthor

    does this proposal being implemented mean for example that zig reduce would start shipping as zig-reduce and/or the zig binary should check PATH for binaries to ensure the space version still works?

    No. It would be the same CLI.

  9. jayrod246 commented on Feb 26, 2024

    @jayrod246
    Contributor

    So same CLI, and it would re-build the relevant source files if necessary - like how zig build works. Nice!

  10. added a commit that references this issue on Feb 27, 2024
  11. added
    accepted
    on Feb 27, 2024
  12. 10 remaining items

  13. tecanec commented on Apr 3, 2024

    @tecanec
    Contributor

    You know what's really pleasant to work on? The build system.

    Well, that explains it...

    The lazily built executables can take full advantage of native CPU features. We can have AOT cake and eat JIT too!

    This is just speculation, but I feel like JIT would synergize really well with comptime. Especially for things like std.simd.suggestVectorLength, whose goal is specifically to automatically optimize for the given hardware.

    all the machine code backends other than the one used to produce native executables

    This could be very interesting, I imagine specifying a 3rd party package containing a machine backend I need through my build.zig.zon, which could be very good for embedded projects.

    I was thinking something similar. Custom backends sound like a big deal for non-mainstream platforms, including proprietary, niche, or even in-development architectures, including experiments and pet projects.

    Overall, I am a bit concerned about compile times, though. I know caching is a thing, but it's not perfect.

  14. added a commit that references this issue on Nov 14, 2024
  15. modified the milestones: 0.14.0, 0.15.0 on Feb 9, 2025
  16. moosichu commented on Oct 12, 2025

    @moosichu
    Contributor

    I was having a think just now about zig fmt being un-jitted due to the worse user experience it creates, and was wondering if it could be still be shipped as source without make the user experience of running zig fmt for the first time itself bad.

    One idea that could work would be to eagerly compile all the code that ships with zig instead of lazily compiling it.

    I think this could work as a "one time" setup step for anyone downloading zig - because at that point you are already going through a process of waiting it to download and decompress, so having the very first invokation of zig compile at least all the code needed to run the frequently used subcommands would be quite reasonable?

    It also keeps the benefit that if you are going to be modifying that code (triggering a recompile) during compiler development, it's probably because you are testing that feature anyway. So I think it's only a boon for those working on the compiler itself?

    The only downside is that the very first time you run zig it will be a little bit slower as it will always be building zig fmt even if you end up never using it, but:

    • most users probalby will run zig fmt at some point, so compiling it eagerly just improves the overall experience for them
    • all users benefit from the smaller resulting zig download size
    • all users benefit from the improved workflow for those working on the compiler (as then they get to enjoy better zig release faster)

    TBH thinking about it more, I don't really see much of a benefit to JITing any of the subcommands (as-in, waiting to compile them until they are invoked), as I think across the board it's a better user experience to know that you can "warm up" the whole zig compiler by invoking it just once when you are already accepting a wait for parts of it to compile anyway. However, for fmt in particular the eager compilation strategy could work well even if it's decided it doesn't make sense for less-frequently used subcommands.

  17. added
    enhancement
    and removed
    proposal
    accepted
    on Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions