Skip to content

zig init: allow specifying project path #24298

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4621,10 +4621,11 @@ fn cmdTranslateC(
}

const usage_init =
\\Usage: zig init
\\Usage: zig init [path]
\\
\\ Initializes a `zig build` project in the current working
\\ directory.
\\ Initializes a `zig build` project in the specified
\\ path. If path is not provided, initialize in the
\\ current working directory.
\\
\\Options:
\\ -s, --strip Generate files without comments
Expand All @@ -4637,6 +4638,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
dev.check(.init_command);

var strip = false;
var path: ?[]const u8 = "";
{
var i: usize = 0;
while (i < args.len) : (i += 1) {
Expand All @@ -4651,7 +4653,10 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
fatal("unrecognized parameter: '{s}'", .{arg});
}
} else {
fatal("unexpected extra parameter: '{s}'", .{arg});
if (path != null) {
fatal("more than one path specified", .{});
}
Comment on lines +4656 to +4658
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail before the first assignment because path starts as an empty slice. Did you test these changes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I have a problem with building zig on my computer so I couldn't test it. I'll convert this to a draft and request reviews once the problem is resolved.

path = arg;
}
}
}
Expand All @@ -4663,6 +4668,19 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
const cwd_basename = fs.path.basename(cwd_path);
const sanitized_root_name = try sanitizeExampleName(arena, cwd_basename);

var out_dir = fs.cwd();
if (path != null) {
fs.cwd().makePath(path.?) catch |e|
switch (e) {
error.PathAlreadyExists => return,
Comment on lines +4674 to +4675
Copy link

@siarie siarie Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to be an error instead. Someone might not want to accidentally writing any Zig files in their existing directory

error.NotDir => {
fatal("specified path contains non-directory", .{});
},
else => return e,
};
out_dir = try fs.cwd().openDir(path.?, .{});
}

const s = fs.path.sep_str;
const template_paths = [_][]const u8{
Package.build_zig_basename,
Expand All @@ -4675,7 +4693,7 @@ fn cmdInit(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
const fingerprint: Package.Fingerprint = .generate(sanitized_root_name);

for (template_paths) |template_path| {
if (templates.write(arena, fs.cwd(), sanitized_root_name, template_path, fingerprint)) |_| {
if (templates.write(arena, out_dir, sanitized_root_name, template_path, fingerprint)) |_| {
std.log.info("created {s}", .{template_path});
ok_count += 1;
} else |err| switch (err) {
Expand Down
Loading