Skip to content

Commit 4007424

Browse files
committed
fix(cli): tagged source builds report the release version
1 parent 4fd40e1 commit 4007424

6 files changed

Lines changed: 124 additions & 6 deletions

File tree

‎apps/server/vite.config.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Build configuration normalizes manifest paths before an Effect runtime exists.
12
import "vite-plus/test/config";
2-
// @effect-diagnostics-next-line nodeBuiltinImport:off -- tsdown needs the asset path when it reads this config.
33
import * as NodePath from "node:path";
44
import { defineConfig, mergeConfig } from "vite-plus";
5+
import * as NodeURL from "node:url";
56

67
import baseConfig from "../../vite.config.ts";
78
import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
9+
import { resolveTaggedSourceVersion } from "../../scripts/lib/tagged-source-version.ts";
810
import rootPackageJson from "../../package.json" with { type: "json" };
911
import packageJson from "./package.json" with { type: "json" };
1012
import { WeightedShardSequencer } from "./src/testUtils/weightedShardSequencer.ts";
@@ -18,9 +20,12 @@ import {
1820
export { shouldBundleCliDependency };
1921

2022
const repoEnv = loadRepoEnv();
21-
const cliBuildChannel = /^[^-+]+-(?:nightly|preview)\./.test(packageJson.version)
22-
? "nightly"
23-
: "latest";
23+
const serverManifestPath = NodeURL.fileURLToPath(new URL("./package.json", import.meta.url));
24+
const buildVersion = resolveTaggedSourceVersion(
25+
NodeURL.fileURLToPath(new URL("../../", import.meta.url)),
26+
packageJson.version,
27+
);
28+
const cliBuildChannel = /^[^-+]+-(?:nightly|preview)\./.test(buildVersion) ? "nightly" : "latest";
2429
// Official release workflows set the telemetry channel; local builds and forks use "dev".
2530
const TELEMETRY_CHANNELS = ["stable", "nightly", "preview", "dev"];
2631
const telemetryChannel = process.env.T3CODE_TELEMETRY_CHANNEL?.trim() || "dev";
@@ -88,6 +93,18 @@ export default mergeConfig(
8893
},
8994
},
9095
pack: {
96+
plugins: [
97+
{
98+
name: "tagged-source-version",
99+
load(id) {
100+
if (NodePath.resolve(id) !== serverManifestPath) return;
101+
return {
102+
code: JSON.stringify({ ...packageJson, version: buildVersion }),
103+
moduleType: "json",
104+
};
105+
},
106+
},
107+
],
91108
// SEA embeds one entry; expose the history worker as a hidden subcommand.
92109
entry: packBootstrap
93110
? ["src/bin.ts"]

‎apps/web/tsconfig.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"scripts/verify-built-boot.ts",
3636
"scripts/warm-dep-cache.ts",
3737
"../../scripts/lib/public-config.ts",
38+
"../../scripts/lib/tagged-source-version.ts",
3839
"../../scripts/lib/third-party-licenses.ts"
3940
]
4041
}

‎apps/web/vite.config.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Vite config runs before any Effect runtime.
22
import * as NodeFSP from "node:fs/promises";
3+
import * as NodeURL from "node:url";
34
import * as NodeZlib from "node:zlib";
45

56
import react from "@vitejs/plugin-react";
@@ -19,6 +20,7 @@ import {
1920
} from "@t3tools/shared/devProxy";
2021

2122
import { loadRepoEnv } from "../../scripts/lib/public-config";
23+
import { resolveTaggedSourceVersion } from "../../scripts/lib/tagged-source-version";
2224
import { thirdPartyLicensesPlugin } from "../../scripts/lib/third-party-licenses";
2325
import { tailwindPlugins } from "./vite/tailwind";
2426
import { warmBundledDevPlugin } from "./vite/warmBundledDev";
@@ -45,7 +47,13 @@ const configuredRelayTracingToken = repoEnv.VITE_RELAY_OTLP_TRACES_TOKEN?.trim()
4547
const configuredHostedAppChannel = process.env.VITE_HOSTED_APP_CHANNEL?.trim() || "";
4648
const configuredReleaseChannel =
4749
process.env.T3CODE_BUILT_DEV === "1" ? "dev" : process.env.VITE_RELEASE_CHANNEL?.trim() || "";
48-
const configuredAppVersion = process.env.APP_VERSION?.trim() || pkg.version;
50+
const configuredAppVersion =
51+
process.env.APP_VERSION?.trim() ||
52+
resolveTaggedSourceVersion(
53+
NodeURL.fileURLToPath(new URL("../../", import.meta.url)),
54+
pkg.version,
55+
["apps/server/package.json", "apps/web/package.json"],
56+
);
4957
const configuredDesktopUpdateRepository =
5058
process.env.T3CODE_DESKTOP_UPDATE_REPOSITORY?.trim() ||
5159
process.env.GITHUB_REPOSITORY?.trim() ||

‎scripts/build-desktop-artifact.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {
4343
import { loadRepoEnv } from "./lib/public-config.ts";
4444
import { selectDesktopRuntimeExternalDependencies } from "./lib/desktop-external-packages.ts";
4545
import { resolveCatalogDependencies } from "./lib/resolve-catalog.ts";
46+
import { resolveTaggedSourceVersion } from "./lib/tagged-source-version.ts";
4647

4748
import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
4849
import * as NodeServices from "@effect/platform-node/NodeServices";
@@ -3679,7 +3680,8 @@ const buildDesktopArtifactUnlocked = Effect.fn("buildDesktopArtifactUnlocked")(f
36793680
}),
36803681
});
36813682

3682-
const appVersion = options.version ?? serverPackageJson.version;
3683+
const appVersion =
3684+
options.version ?? resolveTaggedSourceVersion(repoRoot, serverPackageJson.version);
36833685
const iconAssets = resolveDesktopBuildIconAssets(appVersion);
36843686
const commitHash = yield* resolveGitCommitHash(repoRoot);
36853687
const mkdir = options.keepStage ? fs.makeTempDirectory : fs.makeTempDirectoryScoped;
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Exercises build-time Git discovery against temporary repositories.
2+
import * as NodeChildProcess from "node:child_process";
3+
import * as NodeFS from "node:fs";
4+
import * as NodeOS from "node:os";
5+
import * as NodePath from "node:path";
6+
import { expect, it } from "@effect/vitest";
7+
8+
import { resolveTaggedSourceVersion } from "./tagged-source-version.ts";
9+
10+
it("uses the exact stable tag, preserving release overrides and untagged builds", () => {
11+
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "tagged-source-version-"));
12+
const manifest = NodePath.join(root, "apps/server/package.json");
13+
const webManifest = NodePath.join(root, "apps/web/package.json");
14+
const webManifestPaths = ["apps/server/package.json", "apps/web/package.json"];
15+
const git = (...args: string[]) =>
16+
NodeChildProcess.execFileSync("git", args, { cwd: root, stdio: "pipe" });
17+
const writeVersion = (version: string) =>
18+
NodeFS.writeFileSync(manifest, JSON.stringify({ name: "t3", version }));
19+
20+
try {
21+
NodeFS.mkdirSync(NodePath.dirname(manifest), { recursive: true });
22+
NodeFS.mkdirSync(NodePath.dirname(webManifest), { recursive: true });
23+
NodeFS.writeFileSync(webManifest, JSON.stringify({ version: "0.1.5" }));
24+
writeVersion("0.1.5");
25+
git("init");
26+
git("config", "user.name", "Version Test");
27+
git("config", "user.email", "[email protected]");
28+
git("config", "commit.gpgSign", "false");
29+
git("config", "tag.gpgSign", "false");
30+
git("add", ".");
31+
git("commit", "-m", "source");
32+
git("tag", "v0.1.6-nightly.20261031.3977");
33+
git("tag", "-a", "v0.1.6", "-m", "release");
34+
35+
expect(resolveTaggedSourceVersion(root, "0.1.5")).toBe("0.1.6");
36+
expect(JSON.parse(NodeFS.readFileSync(manifest, "utf8")).version).toBe("0.1.5");
37+
expect(resolveTaggedSourceVersion(root, "0.1.5", webManifestPaths)).toBe("0.1.6");
38+
39+
NodeFS.writeFileSync(webManifest, JSON.stringify({ version: "0.1.7-preview.1" }));
40+
expect(resolveTaggedSourceVersion(root, "0.1.7-preview.1", webManifestPaths)).toBe(
41+
"0.1.7-preview.1",
42+
);
43+
NodeFS.writeFileSync(webManifest, JSON.stringify({ version: "0.1.5" }));
44+
45+
writeVersion("0.1.7-preview.1");
46+
expect(resolveTaggedSourceVersion(root, "0.1.7-preview.1")).toBe("0.1.7-preview.1");
47+
48+
writeVersion("0.1.5");
49+
git("commit", "--allow-empty", "-m", "after release");
50+
expect(resolveTaggedSourceVersion(root, "0.1.5")).toBe("0.1.5");
51+
52+
git("tag", "not-a-release");
53+
expect(resolveTaggedSourceVersion(root, "0.1.5")).toBe("0.1.5");
54+
} finally {
55+
NodeFS.rmSync(root, { recursive: true, force: true });
56+
}
57+
});
58+
59+
it("keeps the package version in a source archive without Git metadata", () => {
60+
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "source-archive-version-"));
61+
try {
62+
expect(resolveTaggedSourceVersion(root, "0.1.5")).toBe("0.1.5");
63+
} finally {
64+
NodeFS.rmSync(root, { recursive: true, force: true });
65+
}
66+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Build configuration resolves tags before an Effect runtime exists.
2+
import * as NodeChildProcess from "node:child_process";
3+
4+
export function resolveTaggedSourceVersion(
5+
repoRoot: string,
6+
packageVersion: string,
7+
manifestPaths: readonly string[] = ["apps/server/package.json"],
8+
): string {
9+
const git = (...args: string[]) =>
10+
NodeChildProcess.execFileSync("git", args, { cwd: repoRoot, encoding: "utf8", stdio: "pipe" });
11+
12+
try {
13+
// Release jobs and local packagers may have already aligned the manifest to an explicit version.
14+
git("diff", "--quiet", "HEAD", "--", ...manifestPaths);
15+
const tags = git("tag", "--points-at", "HEAD", "--list", "v[0-9]*")
16+
.trim()
17+
.split("\n")
18+
.filter((tag) => /^v\d+\.\d+\.\d+$/.test(tag));
19+
if (tags.length === 1) return tags[0]!.slice(1);
20+
} catch {
21+
// Source archives and builds without Git retain their package version.
22+
}
23+
return packageVersion;
24+
}

0 commit comments

Comments
 (0)