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

Conversation

fel1x-developer
Copy link
Contributor

@fel1x-developer fel1x-developer commented Jun 30, 2025

This PR allows zig init to initialize project when path is specified.

Usage:

$ zig init # path not provided, initialize in the current directory
$ zig init foo # create dir named foo if it does not exists, then initialize under ./foo
$ zig init foo/bar # create dir tree foo/bar if it does not exists, then initialize under ./foo/bar

When specified path already exists as non-dir, the cli throws fatal()

Closes: #24290

@thep1geon
Copy link

I need to preface this with the fact that I am not involved in any development of Zig in any way. I am looking at this PR from the perspective of just a programmer.

Just by looking over your changes, I wonder if it be more readable to make the 'path' variable an optional u8 slice instead of relying on a separate 'path_specified' boolean variable and the u8 slice for the path? My thought process here is that it is less separate variables to have to be thinking about, and the path is either preset and valid or null and not specified.

Further more, looking over the code to make the directory, if one was provided, I believe you should return an error if the directory already exists. By not returning an error, the user would assume nothing went wrong and the project correctly initialized in the directory they specified. I suppose you could implement further logic to check if the directory is empty and still initialize the project, but I'm not sure about that one.

Just some food for thought.

Comment on lines +4656 to +4658
if (path != null) {
fatal("more than one path specified", .{});
}
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.

@fel1x-developer fel1x-developer marked this pull request as draft July 10, 2025 11:03
Comment on lines +4674 to +4675
switch (e) {
error.PathAlreadyExists => return,
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

zig init: allow specifying project directory
4 participants