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

ability to construct a LazyPath out of any file from a dependency package #16643

Closed
andrewrk opened this issue Aug 1, 2023 · 3 comments · Fixed by #16667
Closed

ability to construct a LazyPath out of any file from a dependency package #16643

andrewrk opened this issue Aug 1, 2023 · 3 comments · Fixed by #16667
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. zig build system std.Build, the build runner, `zig build` subcommand, package management
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Aug 1, 2023

The idea here is that you can put an arbitrary file contents in your build.zig.zon, like this:

.{
    .dependencies = .{
        .foo = .{
            .url = "https://example.com/foobar.tar.gz",
            .hash = "12209e851f7e2c6ba2f01de3e11b1771f03e49666065320abd8414aac152bfa75fae",
        },
    },
}

Normally, at this point, one would activate the build.zig script of the dependency like this:

    const foo_dep = b.dependency("foo", .{
        .target = target,
        .optimize = optimize,
        // ...other options...
    });

However, this issue is about the use case where the dependency is not a zig package, but a bundle of arbitrary files.

The idea is that you can access arbitrary files as std.Build.LazyPath objects, like this:

pub fn build(b: *std.Build) !void {
    // ...

    const lazy_path = b.dependencyPath("foo", "path/inside/dependency.txt");

    // now you can pass it anywhere a lazy path is accepted:
    run_step.addFileArg(lazy_path);

    // or you can access the file directly from build script logic:
    const contents = try std.fs.readFileAlloc(lazy_path.resolve());

    // ...
}

Using it via the LazyPath API would help with #14597, because resolution of the path would force the package to be downloaded.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. zig build system std.Build, the build runner, `zig build` subcommand, package management labels Aug 1, 2023
@andrewrk andrewrk added this to the 0.12.0 milestone Aug 1, 2023
@mitchellh
Copy link
Contributor

mitchellh commented Aug 3, 2023

This feature would also allow build-only projects to depend on their upstream source blobs using zon files instead of having to fork/vendor. This may be obvious, but pointing it out as a solid use case to enable the Zig build system to be used with any project.

@pfgithub
Copy link
Contributor

pfgithub commented Aug 5, 2023

Right now, this is possible using @import("root").dependencies.build_root.$PACKAGE_NAME, but packages require a build.zig file so an empty build.zig needs to be added:

const std = @import("std");

pub fn build(_: *std.Build) !void {}

I didn't notice the PR, it looks like it fixes this

@mitchellh
Copy link
Contributor

but packages require a build.zig file so an empty build.zig needs to be added:

Thanks for pointing this out, but this is a key issue that I think this issue proposes to address! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Solving this issue will likely involve adding new logic or components to the codebase. zig build system std.Build, the build runner, `zig build` subcommand, package management
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants