(JPL’s answer is correct, but I just thought I’d add a bit of detail here)
As the release notes mention, getEmittedDocs is the new method of accessing generated documentation. If that function is used to get a LazyPath, the build system will automatically trigger documentation generation to the returned directory.
Let’s say you have a std.Build.Step.Compile named lib, and you want to generate documentation for it and put it in the doc/ directory in the install prefix (zig-out/ by default). We’ll do this using an InstallDir step, which copies a directory of files to the installation prefix. Assuming your *std.Build is named b, your code would look like this:
The idea here is that we’re separating documentation generation from installation: the former is handled by getEmittedDocs and the latter by the InstallDir step. source_dir is telling the step what it is we want to copy (in this case the documentation directory); install_dir is the root path we’re going into (.prefix means the installation prefix); and install_subdir is the string subdir we want to go to (in this case "doc"). So this will generate the documentation for lib and copy it into to zig-out/doc/ by default.
Edit: This message isn’t quite correct. Please look at @ianprime0509’s message below.
I believe this is because the static library name needs to match the name of the file (so .name = "main" vs your current .name = "foo") for autodocs to properly generate docs for it. OR you can name your Library’s root file root.zig.
@ianprime0509 did some work on this front a few weeks back to improve the experience. That said, I admit it can be a little confusing if you don’t know these rules. I may also be a little off base if “main.zig” is also intended to work, but I think I remember that not being the case.
There are a few things which I think are important to note for users of the new Autodoc, especially those who may have used the original implementation. Hopefully one of these will help address the issue you’re seeing.
It is required to use a local HTTP server to serve the docs files.
The old Autodoc worked fine by just opening up index.html in a browser, since nothing was being fetched dynamically (just CSS and JS files included in the usual way). But the new implementation has to fetch a Wasm binary and sources.tar containing the source files, which does not work with file:// URLs due to CORS restrictions.
Probably the easiest way to get this set up is to run python -m http.server -d path/to/docs, since many people probably already have Python installed. Having this functionality bundled into Zig would be nice (it’s implemented for the standard library docs in zig std, but not yet exposed for user projects).
Optimal documentation organization requires adhering to some project structure constraints.
This is what @00JCIV00 was referring to: the first thing that shows up when you navigate to the docs is the root source file as determined by looking for a file named your-module-name.zig (in this case, foo.zig) or root.zig. If no such file exists, it’ll use the first file in the sources.tar (which is effectively arbitrary).
However, regardless of this, all the documentation will still be accessible; this only affects what you first see when you load the main page. In this simple example, it doesn’t matter since there’s only one source file anyway. But in a larger project, if you don’t follow this convention, the main page will probably just be some random file in the project and not the root file you’re expecting.
Also, the sources.tar creation bundles everything in the directory of the root source file, which means if you don’t put your source files under a src directory, you’ll end up with build.zig and other stuff included in the tarball.
The URL hash structure has changed.
This is really only relevant for those with existing bookmarks to Autodoc. For example, https://ziglang.org/documentation/master/std/#A;std doesn’t work. The equivalent URL in the new implementation is https://ziglang.org/documentation/master/std/#std
Thanks for the details. Really good to know. Unfortunately, I still get the same results. I renamed src/main.zig to src/foo.zig and made the corresponding change in build.zig.
Stop the press! Now it works. For some weird reason, the URI in the address bar I kept tring to reload was http://localhost:8080/#src/foo/root.zig. After leaving it at just http://localhost:8080/ it works fine. It seems that the previous module name mix-up pointed the browser to that URI, and from that point on I just kept reloading the same address.
One last question: I was expecting to see the doc test for add; why isn’t it part of the docs page?
EDIT: Never mind, I just clicked on the add link and have seen everything gloriously displayed in one place. Awesome! Thanks again guys.
I was trying to generate the documentation for a library, and if I add “type aliases” that won’t work:
Example:
pub const Point = @import("point.zig").Point2d(f32);
I would expect that, when I go to the definition of Point, Point2d would be underlined, but it isn’t.
I need to just import it without doing the specialization part, that is:
pub const Point = @import("point.zig").Point2d;
Or maybe create a docs.zig where I import all types from all files. A shame that a docs.zig containing this doesn’t generate documentation: