Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
fix(version): derive development CalVer from HEAD commit date
boojack8f6e6a7ae619
diff --git a/.github/workflows/build-canary-image.yml b/.github/workflows/build-canary-image.ymlindex a944cad04..108d7fb39 100644--- a/.github/workflows/build-canary-image.yml+++ b/.github/workflows/build-canary-image.yml@@ -62,14 +62,12 @@ jobs:- linux/arm64steps:- uses: actions/checkout@v6- with:- fetch-depth: 0- - name: Resolve version from latest tag+ - name: Resolve version from commit dateid: versionrun: |- version="$(git describe --tags --abbrev=0 2>/dev/null || echo dev)"- echo "version=${version#v}" >> "$GITHUB_OUTPUT"+ version="$(bash scripts/release_version.sh development-version)"+ echo "version=$version" >> "$GITHUB_OUTPUT"- name: Download frontend artifactsuses: actions/download-artifact@v7diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 7c6ec2811..6b4c9510b 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -52,7 +52,8 @@ jobs:run: |if [ "$EVENT_NAME" = "workflow_dispatch" ]; thenecho "tag=" >> "$GITHUB_OUTPUT"- echo "version=manual-${GITHUB_SHA::7}" >> "$GITHUB_OUTPUT"+ version="$(bash scripts/release_version.sh development-version)"+ echo "version=$version" >> "$GITHUB_OUTPUT"echo "series=" >> "$GITHUB_OUTPUT"echo "is_prerelease=false" >> "$GITHUB_OUTPUT"exit 0diff --git a/AGENTS.md b/AGENTS.mdindex 5fa10f208..108b1a74f 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -35,7 +35,7 @@ Run from the repository root unless a command starts with `cd`.```bash# Backend-go run ./cmd/memos --port 8081 # Start backend dev server+go run -buildvcs=true ./cmd/memos --port 8081 # Start backend with commit-derived CalVergo test ./... # Run all Go testsgo test -v ./store/... # Store tests, including DB drivers via TestContainersgo test -v -race ./server/... # Server tests with race detector@@ -139,6 +139,12 @@ Black-box service tests live in `server/api/v1/test/`, unit tests next to the co## CI Reference+- Development builds use the HEAD committer date in UTC as `YY.MM`; release builds inject the CalVer tag.+- `go build` embeds Git metadata automatically; `go run` needs `-buildvcs=true`. Builds without Git metadata must inject+ `github.com/usememos/memos/internal/version.Version` through `-ldflags`.+- Docker excludes `.git`; pass `--build-arg VERSION="$(bash scripts/release_version.sh development-version)"` and+ `--build-arg COMMIT="$(git rev-parse HEAD)"` when building locally (build frontend assets first).+- Backend CI: Go 1.27.0, `go mod tidy -go=1.27.0`, golangci-lint v2.13.1, test groups `store`, `server`, `internal`, `other`(`cmd`, `core`, `markdown`, `filter`, `provider`, `proto`).- Frontend CI: Node 24, pnpm 11.0.1, `pnpm lint`, `pnpm test`, `pnpm build`.diff --git a/cmd/memos/main.go b/cmd/memos/main.goindex 4e5c418e5..2b928fa66 100644--- a/cmd/memos/main.go+++ b/cmd/memos/main.go@@ -42,8 +42,9 @@ var (versionCmd = &cobra.Command{Use: "version",Short: "Print the current Memos version",- Run: func(_ *cobra.Command, _ []string) {- fmt.Println(version.GetCurrentVersion())+ Run: func(cmd *cobra.Command, _ []string) {+ // Print the root's version so every form shares one source.+ fmt.Fprintln(cmd.OutOrStdout(), cmd.Root().Version)},})@@ -226,6 +227,10 @@ func printServerInfo(profile *profile.Profile, accessMode storepb.InstanceAccess}func main() {+ if err := version.Validate(); err != nil {+ slog.Error("invalid build metadata", slog.String("error", err.Error()))+ os.Exit(1)+ }if err := rootCmd.Execute(); err != nil {slog.Error("memos failed", slog.String("error", err.Error()))os.Exit(1)diff --git a/cmd/memos/main_test.go b/cmd/memos/main_test.goindex f3d73a2b0..16e0b1c08 100644--- a/cmd/memos/main_test.go+++ b/cmd/memos/main_test.go@@ -38,8 +38,14 @@ func TestVersionFlagUsesPosixShorthand(t *testing.T) {func TestVersionFormsPrintTheSameString(t *testing.T) {require.Equal(t, version.GetCurrentVersion(), rootCmd.Version)+ // Test binaries do not carry VCS metadata. Supply a version so Cobra takes+ // its version path rather than running the server.+ originalVersion := rootCmd.Version+ rootCmd.Version = "26.09"+ t.Cleanup(func() { rootCmd.Version = originalVersion })- // Cobra handles the version flag before RunE, so this never starts a server.+ // Cobra handles the version flag before RunE, and the version subcommand has+ // its own Run, so none of these start a server.run := func(args ...string) string {var out bytes.BufferrootCmd.SetOut(&out)@@ -53,7 +59,8 @@ func TestVersionFormsPrintTheSameString(t *testing.T) {return out.String()}- expected := version.GetCurrentVersion() + "\n"+ expected := rootCmd.Version + "\n"require.Equal(t, expected, run("--version"))require.Equal(t, expected, run("-V"))+ require.Equal(t, expected, run("version"))}diff --git a/internal/README.md b/internal/README.mdindex d943a4324..14dc16728 100644--- a/internal/README.md+++ b/internal/README.md@@ -16,7 +16,7 @@ nothing from `store`, `core`, `server`, `provider`, `markdown`, `filter`, or| `random/` | UUIDs and random strings from a secure source || `ratelimit/` | sliding-window rate limiter || `testutil/` | test fixtures and the fake S3 server (the one package allowed to import `proto/gen`) |-| `version/` | build version and version comparison |+| `version/` | CalVer build metadata from release overrides or the embedded Git commit || `webhook/` | signed webhook delivery with SSRF protection |Do not add `util`, `common`, `base`, or `helpers` packages. Name a package fordiff --git a/internal/version/version.go b/internal/version/version.goindex 16e560689..27b8dcadc 100644--- a/internal/version/version.go+++ b/internal/version/version.go@@ -1,11 +1,59 @@package version-// Version is set by release builds and defaults to a development build marker.-var Version = "dev"+import (+ "regexp"+ "runtime/debug"+ "time"-// Commit is set by CI builds and defaults to an unknown revision marker.+ "github.com/pkg/errors"+)++// Version is injected by release builds or derived from the embedded commit date.+var Version string++// Commit is injected by CI or read from build metadata, falling back to unknown.var Commit = "unknown"+// calendarVersion matches YY.MM[.N][-rc.N]; scripts/release_version.sh accepts the same tags.+var calendarVersion = regexp.MustCompile(`^[1-9][0-9]\.(0[1-9]|1[0-2])(\.[1-9][0-9]*)?(-rc\.[1-9][0-9]*)?$`)++func init() {+ if info, ok := debug.ReadBuildInfo(); ok {+ Version, Commit = resolveBuild(Version, Commit, info.Settings)+ }+}++func resolveBuild(version, commit string, settings []debug.BuildSetting) (string, string) {+ for _, setting := range settings {+ switch setting.Key {+ case "vcs.time":+ if version == "" {+ if timestamp, err := time.Parse(time.RFC3339, setting.Value); err == nil {+ version = timestamp.UTC().Format("06.01")+ }+ }+ case "vcs.revision":+ if commit == "" || commit == "unknown" {+ commit = setting.Value+ }+ default:+ }+ }+ return version, commit+}++// Validate checks that the build has a calendar version before it is exposed.+func Validate() error {+ if Version == "" {+ return errors.New("missing build version: use go run -buildvcs=true ./cmd/memos or inject internal/version.Version with -ldflags")+ }+ if !calendarVersion.MatchString(Version) {+ return errors.Errorf("invalid build version %q: expected YY.MM[.N][-rc.N]", Version)+ }+ return nil+}++// GetCurrentVersion returns the application version.func GetCurrentVersion() string {return Version}diff --git a/internal/version/version_test.go b/internal/version/version_test.gonew file mode 100644index 000000000..234e1f68b--- /dev/null+++ b/internal/version/version_test.go@@ -0,0 +1,54 @@+package version++import (+ "runtime/debug"+ "testing"++ "github.com/stretchr/testify/require"+)++func TestResolveBuild(t *testing.T) {+ for _, tc := range []struct {+ name, version, commit, timestamp, wantVersion, wantCommit string+ }{+ {"development", "", "unknown", "2026-09-27T14:58:52Z", "26.09", "abc123"},+ {"UTC month boundary", "", "unknown", "2026-10-01T00:30:00+08:00", "26.09", "abc123"},+ {"UTC year boundary", "", "unknown", "2026-12-31T23:30:00-08:00", "27.01", "abc123"},+ {"release override", "26.10.1", "release-sha", "2026-09-27T14:58:52Z", "26.10.1", "release-sha"},+ {"RC override", "26.10-rc.1", "", "2026-09-27T14:58:52Z", "26.10-rc.1", "abc123"},+ {"invalid timestamp", "", "unknown", "invalid", "", "abc123"},+ } {+ t.Run(tc.name, func(t *testing.T) {+ settings := []debug.BuildSetting{+ {Key: "vcs.time", Value: tc.timestamp},+ {Key: "vcs.revision", Value: "abc123"},+ {Key: "vcs.modified", Value: "true"},+ }+ version, commit := resolveBuild(tc.version, tc.commit, settings)+ require.Equal(t, tc.wantVersion, version)+ require.Equal(t, tc.wantCommit, commit)+ })+ }++ version, commit := resolveBuild("", "unknown", nil)+ require.Empty(t, version)+ require.Equal(t, "unknown", commit)+ version, commit = resolveBuild("26.09.1", "release-sha", nil)+ require.Equal(t, "26.09.1", version)+ require.Equal(t, "release-sha", commit)+}++func TestValidate(t *testing.T) {+ original := Version+ t.Cleanup(func() { Version = original })+ for _, value := range []string{"26.09", "26.09.1", "27.01", "26.10-rc.1", "26.10.2-rc.3"} {+ Version = value+ require.NoError(t, Validate(), value)+ }+ for _, value := range []string{"dev", "manual-abc123", "v26.09", "0.31.0", "26.9", "26.00", "26.13", "26.09.0", "26.09.01", "26.09-rc.0", "26.09-dev.1", "26.09-dirty"} {+ Version = value+ require.ErrorContains(t, Validate(), "invalid build version", value)+ }+ Version = ""+ require.ErrorContains(t, Validate(), "-buildvcs=true")+}diff --git a/scripts/Dockerfile b/scripts/Dockerfileindex 0c1dbd323..c188b254e 100644--- a/scripts/Dockerfile+++ b/scripts/Dockerfile@@ -14,9 +14,10 @@ COPY . .# Please build frontend first, so that the static files are available.# Refer to `pnpm release` in package.json for the build command.-ARG TARGETOS TARGETARCH VERSION=dev COMMIT=unknown+ARG TARGETOS TARGETARCH VERSION COMMIT=unknownRUN --mount=type=cache,target=/go/pkg/mod \--mount=type=cache,target=/root/.cache/go-build \+ : "${VERSION:?Pass VERSION from scripts/release_version.sh development-version or a release tag}" && \CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \go build \-trimpath \diff --git a/scripts/release_smoke_test.sh b/scripts/release_smoke_test.shindex 48ef68ba8..264790af6 100755--- a/scripts/release_smoke_test.sh+++ b/scripts/release_smoke_test.sh@@ -146,11 +146,12 @@ detect_previous_image() {}build_local_candidate() {- local commit_sha image_tag+ local commit_sha build_version image_tagcommand -v pnpm >/dev/null 2>&1 || die "pnpm is required to build the local candidate"- commit_sha="$(git -C "$REPO_ROOT" rev-parse --short=12 HEAD)"- image_tag="memos-smoke:${commit_sha}-${run_id}"+ commit_sha="$(git -C "$REPO_ROOT" rev-parse HEAD)"+ build_version="$(bash "$SCRIPT_DIR/release_version.sh" development-version "$REPO_ROOT")"+ image_tag="memos-smoke:${commit_sha:0:12}-${run_id}"mkdir -p "$(dirname "$frontend_index")"if [[ -f "$frontend_index" ]]; then@@ -168,7 +169,7 @@ build_local_candidate() {log "Building candidate image $image_tag"docker build \--file "$REPO_ROOT/scripts/Dockerfile" \- --build-arg VERSION=smoke-local \+ --build-arg VERSION="$build_version" \--build-arg COMMIT="$commit_sha" \--tag "$image_tag" \"$REPO_ROOT"diff --git a/scripts/release_version.sh b/scripts/release_version.shindex d7bce75b0..5118fbc53 100755--- a/scripts/release_version.sh+++ b/scripts/release_version.sh@@ -54,6 +54,16 @@ previous_release_image() {if [[ "${BASH_SOURCE[0]}" == "$0" ]]; thencase "${1:-}" in+ development-version)+ # format-local honors TZ; plain format uses the commit's original zone.+ version="$(TZ=UTC git -C "${2:-.}" show -s --format=%cd --date=format-local:%y.%m HEAD)"+ if ! parse_release_tag "$version"; then+ echo "Cannot derive a calendar version from HEAD" >&2+ exit 1+ fi+ printf '%s\n' "$version"+ exit+ ;;previous-tag)previous_release_tag "${2:?repository required}"exitdiff --git a/scripts/release_version_test.go b/scripts/release_version_test.goindex cbb2a56a8..2e5344b7f 100644--- a/scripts/release_version_test.go+++ b/scripts/release_version_test.go@@ -1,13 +1,59 @@package scriptsimport (+ "os""os/exec"+ "path/filepath""strings""testing""github.com/stretchr/testify/require"++ "github.com/usememos/memos/internal/version")+func TestDevelopmentVersion(t *testing.T) {+ for _, tc := range []struct{ date, version string }{+ {"2026-09-27T14:58:52Z", "26.09"},+ {"2026-10-01T00:30:00+08:00", "26.09"},+ {"2026-12-31T23:30:00-08:00", "27.01"},+ } {+ t.Run(tc.date, func(t *testing.T) {+ repo := t.TempDir()+ runGit := func(args ...string) {+ t.Helper()+ cmd := exec.Command("git", append([]string{"-C", repo, "-c", "user.name=Version Test", "-c", "[email protected]", "-c", "commit.gpgsign=false"}, args...)...)+ cmd.Env = append(os.Environ(), "GIT_AUTHOR_DATE=2020-01-01T00:00:00Z", "GIT_COMMITTER_DATE="+tc.date)+ out, err := cmd.CombinedOutput()+ require.NoError(t, err, string(out))+ }+ check := func(repo string) {+ t.Helper()+ cmd := exec.Command("bash", "release_version.sh", "development-version", repo)+ cmd.Env = append(os.Environ(), "TZ=Pacific/Honolulu")+ out, err := cmd.CombinedOutput()+ require.NoError(t, err, string(out))+ require.Equal(t, tc.version+"\n", string(out))+ }+ runGit("init", "-b", "main")+ runGit("commit", "--allow-empty", "-m", "source")+ runGit("tag", "26.10.1")+ check(repo)+ require.NoError(t, os.WriteFile(filepath.Join(repo, "uncommitted"), []byte("local change"), 0o600))+ check(repo)++ shallow := filepath.Join(t.TempDir(), "shallow")+ out, err := exec.Command("git", "clone", "--depth=1", "file://"+repo, shallow).CombinedOutput()+ require.NoError(t, err, string(out))+ check(shallow)+ })+ }+ t.Run("no Git metadata", func(t *testing.T) {+ out, err := exec.Command("bash", "release_version.sh", "development-version", t.TempDir()).CombinedOutput()+ require.Error(t, err, string(out))+ })+}+func TestReleaseVersions(t *testing.T) {for _, tc := range []struct {tag, series, prerelease, images string@@ -36,6 +82,22 @@ func TestReleaseVersions(t *testing.T) {}}+// The script and the binary must accept the same versions, or a tag could+// build a binary that refuses to start.+func TestReleaseTagsMatchBinaryVersions(t *testing.T) {+ original := version.Version+ t.Cleanup(func() { version.Version = original })+ for _, tag := range []string{+ "26.09", "26.09.1", "26.09.10", "27.01", "26.09-rc.1", "26.09.1-rc.2",+ "v26.09", "0.31.0", "26.9", "26.00", "26.13", "2026.09", "26.09.0", "26.09.01", "26.09-rc.0", "26.09-rc.01",+ "26.09-dev.1", "26.09-dirty", "26.09beta", "26.09.1+build", "dev",+ } {+ scriptErr := exec.Command("bash", "release_version.sh", "version", tag).Run()+ version.Version = tag+ require.Equal(t, scriptErr == nil, version.Validate() == nil, tag)+ }+}+func TestPreviousReleaseImage(t *testing.T) {repo := t.TempDir()runGit := func(args ...string) {diff --git a/server/mcp/evals/README.md b/server/mcp/evals/README.mdindex d057fd239..bbc857672 100644--- a/server/mcp/evals/README.md+++ b/server/mcp/evals/README.md@@ -45,7 +45,7 @@ update both this summary and `memos_eval.xml`; nothing in CI catches drift.1. Launch a throwaway demo-mode instance (SQLite, auto-seeded) on a free port:```bash- go run ./cmd/memos --demo --driver sqlite \+ go run -buildvcs=true ./cmd/memos --demo --driver sqlite \--port 8099 --data "$(mktemp -d)" \--instance-url http://localhost:8099```