Skip to content

Commit 9f94a87

Browse files
fix(cli): tagged source builds report the release version (#36350)
Co-authored-by: Lakshmi Tanmay <[email protected]>
1 parent aab388d commit 9f94a87

7 files changed

Lines changed: 126 additions & 12 deletions

File tree

‎apps/server/vite.config.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { defineConfig, mergeConfig } from "vite-plus";
55

66
import baseConfig from "../../vite.config.ts";
77
import { loadRepoEnv } from "../../scripts/lib/public-config.ts";
8+
import { resolveTaggedSourceVersion } from "../../scripts/lib/tagged-source-version.ts";
89
import rootPackageJson from "../../package.json" with { type: "json" };
910
import packageJson from "./package.json" with { type: "json" };
1011
import { WeightedShardSequencer } from "./src/testUtils/weightedShardSequencer.ts";
@@ -18,9 +19,12 @@ import {
1819
export { shouldBundleCliDependency };
1920

2021
const repoEnv = loadRepoEnv();
21-
const cliBuildChannel = /^[^-+]+-(?:nightly|preview)\./.test(packageJson.version)
22-
? "nightly"
23-
: "latest";
22+
const serverManifestPath = NodePath.join(import.meta.dirname, "package.json");
23+
// The desktop packager passes APP_VERSION; it can equal the manifest at a commit tagged otherwise.
24+
const buildVersion =
25+
process.env.APP_VERSION?.trim() ||
26+
resolveTaggedSourceVersion(NodePath.join(import.meta.dirname, "../.."), packageJson.version);
27+
const cliBuildChannel = /^[^-+]+-(?:nightly|preview)\./.test(buildVersion) ? "nightly" : "latest";
2428
// Official release workflows set the telemetry channel; local builds and forks use "dev".
2529
const TELEMETRY_CHANNELS = ["stable", "nightly", "preview", "dev"];
2630
const telemetryChannel = process.env.T3CODE_TELEMETRY_CHANNEL?.trim() || "dev";
@@ -88,6 +92,19 @@ export default mergeConfig(
8892
},
8993
},
9094
pack: {
95+
plugins: [
96+
{
97+
// Every server module reads its version from this manifest import.
98+
name: "t3code:build-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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from "@t3tools/shared/devProxy";
2020

2121
import { loadRepoEnv } from "../../scripts/lib/public-config";
22+
import { resolveTaggedSourceVersion } from "../../scripts/lib/tagged-source-version";
2223
import { thirdPartyLicensesPlugin } from "../../scripts/lib/third-party-licenses";
2324
import { tailwindPlugins } from "./vite/tailwind";
2425
import { warmBundledDevPlugin } from "./vite/warmBundledDev";
@@ -45,7 +46,6 @@ const configuredRelayTracingToken = repoEnv.VITE_RELAY_OTLP_TRACES_TOKEN?.trim()
4546
const configuredHostedAppChannel = process.env.VITE_HOSTED_APP_CHANNEL?.trim() || "";
4647
const configuredReleaseChannel =
4748
process.env.T3CODE_BUILT_DEV === "1" ? "dev" : process.env.VITE_RELEASE_CHANNEL?.trim() || "";
48-
const configuredAppVersion = process.env.APP_VERSION?.trim() || pkg.version;
4949
const configuredDesktopUpdateRepository =
5050
process.env.T3CODE_DESKTOP_UPDATE_REPOSITORY?.trim() ||
5151
process.env.GITHUB_REPOSITORY?.trim() ||
@@ -295,7 +295,14 @@ const configuredAllowedHosts = (process.env.T3CODE_DEV_ALLOWED_HOSTS ?? "")
295295
.filter((entry) => entry.length > 0);
296296
const allowedHosts = [".ts.net", ...configuredAllowedHosts];
297297

298-
export default defineConfig(() => {
298+
export default defineConfig(({ command }) => {
299+
// `vp dev` and `--built` dev pair this client with a server run from source, which reports the
300+
// manifest version. Only a production build is paired with a packed server.
301+
const configuredAppVersion =
302+
process.env.APP_VERSION?.trim() ||
303+
(command === "build" && !builtDev
304+
? resolveTaggedSourceVersion(`${import.meta.dirname}/../..`, pkg.version)
305+
: pkg.version);
299306
return {
300307
run: {
301308
tasks: {

‎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";
@@ -3686,7 +3687,8 @@ const buildDesktopArtifactUnlocked = Effect.fn("buildDesktopArtifactUnlocked")(f
36863687
}),
36873688
});
36883689

3689-
const appVersion = options.version ?? serverPackageJson.version;
3690+
const appVersion =
3691+
options.version ?? resolveTaggedSourceVersion(repoRoot, serverPackageJson.version);
36903692
const iconAssets = resolveDesktopBuildIconAssets(appVersion);
36913693
const commitHash = yield* resolveGitCommitHash(repoRoot);
36923694
const mkdir = options.keepStage ? fs.makeTempDirectory : fs.makeTempDirectoryScoped;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Exercises Git tag 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 "vite-plus/test";
7+
8+
import { releasePackageFiles, resolveTaggedSourceVersion } from "./tagged-source-version.ts";
9+
10+
function makeRoot() {
11+
const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "tagged-source-version-"));
12+
return {
13+
root,
14+
writeManifest: (relativePath: string, version: string) => {
15+
const filePath = NodePath.join(root, relativePath);
16+
NodeFS.mkdirSync(NodePath.dirname(filePath), { recursive: true });
17+
NodeFS.writeFileSync(filePath, JSON.stringify({ version }));
18+
},
19+
git: (...args: ReadonlyArray<string>) =>
20+
NodeChildProcess.execFileSync(
21+
"git",
22+
["-c", "user.name=Test", "-c", "[email protected]", ...args],
23+
{ cwd: root, stdio: "pipe" },
24+
),
25+
[Symbol.dispose]: () => NodeFS.rmSync(root, { recursive: true, force: true }),
26+
};
27+
}
28+
29+
it("reports the single stable tag on HEAD unless a release manifest was edited", () => {
30+
using repo = makeRoot();
31+
for (const relativePath of releasePackageFiles) repo.writeManifest(relativePath, "0.1.5");
32+
repo.git("init", "--quiet");
33+
repo.git("add", ".");
34+
repo.git("commit", "--quiet", "--no-gpg-sign", "-m", "source");
35+
const resolve = () => resolveTaggedSourceVersion(repo.root, "0.1.5");
36+
37+
repo.git("tag", "v0.1.6-nightly.20261031.3977");
38+
expect(resolve()).toBe("0.1.5");
39+
40+
repo.git("tag", "--no-sign", "-a", "v0.1.6", "-m", "release");
41+
expect(resolve()).toBe("0.1.6");
42+
43+
// A nightly or preview job on the tagged commit aligns the manifests to its own version.
44+
repo.writeManifest("apps/web/package.json", "0.1.7-preview.1");
45+
expect(resolveTaggedSourceVersion(repo.root, "0.1.7-preview.1")).toBe("0.1.7-preview.1");
46+
repo.writeManifest("apps/web/package.json", "0.1.5");
47+
expect(resolve()).toBe("0.1.6");
48+
49+
repo.git("tag", "v0.1.7");
50+
expect(resolve()).toBe("0.1.5");
51+
52+
repo.git("commit", "--quiet", "--no-gpg-sign", "--allow-empty", "-m", "after release");
53+
expect(resolve()).toBe("0.1.5");
54+
});
55+
56+
it("keeps the manifest version in a source archive without Git metadata", () => {
57+
using repo = makeRoot();
58+
expect(resolveTaggedSourceVersion(repo.root, "0.1.5")).toBe("0.1.5");
59+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @effect-diagnostics-next-line nodeBuiltinImport:off -- Build configs resolve the tag before an Effect runtime exists.
2+
import * as NodeChildProcess from "node:child_process";
3+
4+
export const releasePackageFiles = [
5+
"apps/server/package.json",
6+
"apps/desktop/package.json",
7+
"apps/web/package.json",
8+
"packages/contracts/package.json",
9+
] as const;
10+
11+
/**
12+
* A release is tagged on the commit it was built from, and the manifest bump lands on main
13+
* afterwards, so source at `vX.Y.Z` still carries the previous version. Server, web and desktop
14+
* builds must all resolve through here, or the client reports a different version than its server.
15+
*/
16+
export function resolveTaggedSourceVersion(repoRoot: string, manifestVersion: string): string {
17+
const git = (...args: ReadonlyArray<string>) =>
18+
NodeChildProcess.execFileSync("git", args, { cwd: repoRoot, encoding: "utf8", stdio: "pipe" });
19+
20+
try {
21+
// Release jobs and the desktop packager write the version they build into the manifests.
22+
git("diff", "--quiet", "HEAD", "--", ...releasePackageFiles);
23+
const stableTags = git("tag", "--points-at", "HEAD")
24+
.split("\n")
25+
.filter((tag) => /^v\d+\.\d+\.\d+$/.test(tag));
26+
if (stableTags.length === 1) return stableTags[0]!.slice(1);
27+
} catch {
28+
// No Git, a source archive, or edited manifests: the manifest is the version.
29+
}
30+
return manifestVersion;
31+
}

‎scripts/update-release-package-versions.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import * as Schema from "effect/Schema";
1212
import { Argument, Command, Flag } from "effect/cli";
1313
import { fromJsonStringPretty } from "@t3tools/shared/schemaJson";
1414

15+
import { releasePackageFiles } from "./lib/tagged-source-version.ts";
16+
1517
export class ReleasePackageManifestError extends Schema.TaggedError<ReleasePackageManifestError>()(
1618
"ReleasePackageManifestError",
1719
{
@@ -46,12 +48,7 @@ export class ReleaseGitHubOutputWriteError extends Schema.TaggedError<ReleaseGit
4648
}
4749
}
4850

49-
export const releasePackageFiles = [
50-
"apps/server/package.json",
51-
"apps/desktop/package.json",
52-
"apps/web/package.json",
53-
"packages/contracts/package.json",
54-
] as const;
51+
export { releasePackageFiles };
5552

5653
interface UpdateReleasePackageVersionsOptions {
5754
readonly rootDir?: string | undefined;

0 commit comments

Comments
 (0)