Skip to content

extract WriteSourceFileStep out from WriteFileStep #14944

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

Closed
Tracked by #14647
andrewrk opened this issue Mar 16, 2023 · 3 comments
Closed
Tracked by #14647

extract WriteSourceFileStep out from WriteFileStep #14944

andrewrk opened this issue Mar 16, 2023 · 3 comments
Labels
breaking Implementing this issue could cause existing code to no longer compile or have different behavior. zig build system std.Build, the build runner, `zig build` subcommand, package management
Milestone

Comments

@andrewrk
Copy link
Member

Extracted from #14647.

//! However, this step has an additional capability of writing data to paths
//! relative to the package root, effectively mutating the package's source
//! files. Be careful with the latter functionality; it should not be used
//! during the normal build process, but as a utility run by a developer with
//! intention to update source files, which will then be committed to version
//! control.

// Writing to source files is kind of an extra capability of this
// WriteFileStep - arguably it should be a different step. But anyway here
// it is, it happens unconditionally and does not interact with the other
// files here.
var any_miss = false;
for (wf.output_source_files.items) |output_source_file| {
if (fs.path.dirname(output_source_file.sub_path)) |dirname| {
b.build_root.handle.makePath(dirname) catch |err| {
return step.fail("unable to make path '{}{s}': {s}", .{
b.build_root, dirname, @errorName(err),
});
};
}
switch (output_source_file.contents) {
.bytes => |bytes| {
b.build_root.handle.writeFile(output_source_file.sub_path, bytes) catch |err| {
return step.fail("unable to write file '{}{s}': {s}", .{
b.build_root, output_source_file.sub_path, @errorName(err),
});
};
any_miss = true;
},
.copy => |file_source| {
const source_path = file_source.getPath(b);
const prev_status = fs.Dir.updateFile(
fs.cwd(),
source_path,
b.build_root.handle,
output_source_file.sub_path,
.{},
) catch |err| {
return step.fail("unable to update file from '{s}' to '{}{s}': {s}", .{
source_path, b.build_root, output_source_file.sub_path, @errorName(err),
});
};
any_miss = any_miss or prev_status == .stale;
},
}
}

This should be a different step, not part of WriteFileStep.

@andrewrk andrewrk added breaking Implementing this issue could cause existing code to no longer compile or have different behavior. zig build system std.Build, the build runner, `zig build` subcommand, package management labels Mar 16, 2023
@andrewrk andrewrk added this to the 0.11.0 milestone Mar 16, 2023
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Jul 22, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Aug 5, 2023
@andrewrk andrewrk modified the milestones: 0.12.0, 0.13.0 Feb 14, 2024
@matklad
Copy link
Contributor

matklad commented Nov 19, 2024

Once WriteSourceFileStep, it would be good to add a mode to it to just check that the files are up-to-date and fail the build if they aren't. So, as developer, you run:

$ vim source-of-truth
$ zig build generate-sources
$ git add .
$ git commit -m "refresh generated source" && git push

and then CI could optionally run

$ zig build generate-sources -Dgenerate-sources-mode=check

which would fail the build if you forgot to commit generated sources.

Concrete use-case for TigerBeetle:

  • We'd love to use WriteSourceFileStep to generate an .h file from Zig's source of truth and to include the generated file in the repository, so that consumers of our pre-compiled C library wouldn't have to run zig build process to get the header
  • But we need to also check that the .h file in the repo is indeed in sync with the Zig sources.

Piggy-backing on the existing issue, as it seems like this could be tacked in the process of extracting the code, if this looks like a good idea

@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Mar 1, 2025
@lishaduck
Copy link

This seems to have been done in #20580.

@alexrp
Copy link
Member

alexrp commented Apr 19, 2025

d1c14f2

@alexrp alexrp closed this as completed Apr 19, 2025
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. zig build system std.Build, the build runner, `zig build` subcommand, package management
Projects
None yet
Development

No branches or pull requests

4 participants