Files
BetterDesk/web-nodejs/lib/productVersion.js
T
UNITRONIX 850c0f42a0 fix(update): build the Go server with the version from its own source tree
applyUpdate() writes the root VERSION file only AFTER buildGoServer() runs, so
at build time the root VERSION still held the previous release. That value went
into `-X main.Version=`, producing a freshly compiled server that reported the
old version on /api/health while the panel showed the new one (e.g. panel
3.5.218, server 3.5.214).

betterdesk-server/internal/productversion/VERSION is the file `//go:embed`
actually ships in the binary, and ensureServerSource() already refreshes it as
part of the 287-file server tree sync that happens before the build. Read it
first via readEmbeddedServerVersion(), falling back to the previous logic when
absent or malformed.

Covers the ordering bug with tests that pin the stale-root-VERSION scenario.

Thanks: INSOLVE (Honorary); Marco Jakobs (@jacotec); MyNameisStitch (@MyNameisStitch); Redspin (@playerumpknow); muopc (@muopc)
2026-11-08 15:47:35 +01:00

85 lines
2.7 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const SEMVER_RE = /^(\d+)\.(\d+)\.(\d+)$/;
function getProjectRoot() {
return path.join(__dirname, '..', '..');
}
function readPackageJsonVersion(consoleDir) {
try {
const pkgPath = path.join(consoleDir || path.join(__dirname, '..'), 'package.json');
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'));
const v = (pkg.version || '').trim();
return SEMVER_RE.test(v) ? v : null;
} catch (_e) {
return null;
}
}
/**
* Read canonical BetterDesk product version.
* Prefers repo-root VERSION, then web-nodejs/package.json, then fallback.
*/
function readProductVersion({ rootDir, consoleDir, fallback = '0.0.0' } = {}) {
const projectRoot = rootDir || getProjectRoot();
const versionFile = path.join(projectRoot, 'VERSION');
try {
if (fs.existsSync(versionFile)) {
const v = fs.readFileSync(versionFile, 'utf8').trim();
if (SEMVER_RE.test(v)) return v;
}
} catch (_e) { /* fall through */ }
const resolvedConsoleDir = consoleDir || path.join(projectRoot, 'web-nodejs');
const consoleVersionFile = path.join(resolvedConsoleDir, 'VERSION');
try {
if (fs.existsSync(consoleVersionFile)) {
const v = fs.readFileSync(consoleVersionFile, 'utf8').trim();
if (SEMVER_RE.test(v)) return v;
}
} catch (_e) { /* fall through */ }
const fromPkg = readPackageJsonVersion(resolvedConsoleDir);
if (fromPkg) return fromPkg;
return fallback;
}
/**
* Read the version that `betterdesk-server` embeds through
* `betterdesk-server/internal/productversion/VERSION` (`//go:embed`).
*
* This file is part of the server source tree, so it is already refreshed by
* `ensureServerSource()` before the Go build runs. `applyUpdate()` writes the
* root `VERSION` only *after* the build, so `readProductVersion()` can still
* return the previous release at build time — that mismatch made a freshly
* compiled server report the old version on `/api/health` while the panel
* showed the new one.
*
* @returns {string|null} semver from the embedded file, or null when absent
*/
function readEmbeddedServerVersion(serverDir) {
if (!serverDir) return null;
try {
const embedded = path.join(serverDir, 'internal', 'productversion', 'VERSION');
if (!fs.existsSync(embedded)) return null;
const v = fs.readFileSync(embedded, 'utf8').trim();
return SEMVER_RE.test(v) ? v : null;
} catch (_e) {
return null;
}
}
module.exports = {
SEMVER_RE,
getProjectRoot,
readProductVersion,
readPackageJsonVersion,
readEmbeddedServerVersion,
};