build: skip git version detection when used as dependency (#9850)

When ghostty is used as a Zig dependency, detect this by comparing the
build root with ghostty's source directory. Skip git detection entirely
and use the version from build.zig.zon.

This fixes build failures when downstream projects have git tags that
don't match ghostty's version format. Previously, ghostty would read the
downstream project's git tags and panic at Config.zig:246 with:

\`\`\`
tagged releases must be in vX.Y.Z format matching build.zig
\`\`\`

**Reproduction:**
1. Create a project that uses ghostty as a Zig dependency
2. Tag the project with a version like \`v0.2.0\`
3. Run \`zig build\` → panic

**Fix:**
Use \`@src().file\` to get ghostty's source directory and compare it
with \`b.build_root\`. When they differ, ghostty is a dependency and we
skip git detection.

Thread:
https://ampcode.com/threads/T-197e6c33-b8f8-4b23-8fc8-7f6b6edd9f35
pull/9864/head
Mitchell Hashimoto 2025-12-10 12:46:10 -08:00 committed by GitHub
commit b66e4dc2cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 0 deletions

View File

@ -2,6 +2,7 @@ const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const buildpkg = @import("src/build/main.zig");
const appVersion = @import("build.zig.zon").version;
const minimumZigVersion = @import("build.zig.zon").minimum_zig_version;
@ -317,3 +318,8 @@ pub fn build(b: *std.Build) !void {
try translations_step.addError("cannot update translations when i18n is disabled", .{});
}
}
/// Marker used by Config.zig to detect if ghostty is the build root.
/// This avoids running logic such as Git tag checking when Ghostty
/// is used as a dependency.
pub const _ghostty_build_root = true;

View File

@ -218,6 +218,22 @@ pub fn init(b: *std.Build, appVersion: []const u8) !Config {
try std.SemanticVersion.parse(v)
else version: {
const app_version = try std.SemanticVersion.parse(appVersion);
// Detect if ghostty is being built as a dependency by checking if the
// build root has our marker. When used as a dependency, we skip git
// detection entirely to avoid reading the downstream project's git state.
const is_dependency = !@hasDecl(
@import("root"),
"_ghostty_build_root",
);
if (is_dependency) {
break :version .{
.major = app_version.major,
.minor = app_version.minor,
.patch = app_version.patch,
};
}
// If no explicit version is given, we try to detect it from git.
const vsn = GitVersion.detect(b) catch |err| switch (err) {
// If Git isn't available we just make an unknown dev version.