Zig single file scripts?

Based on:

It seems like there are a lot of people (@gonzo @Luke @ngrilly) who want some way to have minimal little files, that can use some set of dependencies and be run with dependencies automatically hooked up to them.

When running zig build --help these options seem interesting:

Advanced Options:
  --build-file [file]          Override path to build.zig
  --build-runner [file]        Override path to build runner

I wonder whether it would be possible to write some custom build-runner, that extracts dependencies from a comment at the start of the file, fetches them, configures them as imports and then compiles the file as an executable.

I am currently busy with other stuff, but maybe somebody wants to explore that idea and see whether it is possible? I think I will explore that idea eventually, but it could take a while.


Or maybe some variant of zig run, that can extract dependencies from a build.zig.zon that is embedded in a comment at the beginning of the file?
Imagining something like zig run --allow-fetch script.zig

1 Like

A post was split to a new topic: Can I avoid repetitive names while using the build system?

I’m sure there are many ways to skin this cat… The one that seems obvious to me is, having a build.zig.zon file which lists external dependencies could be taken to mean that if you do @import("foo") in a zig file in that directory, you trigger the behaviour of having a build.zig file that lists that dependency as a module.

1 Like

This was explicitly rejected in a different form Remove shebang support · Issue #2165 · ziglang/zig · GitHub