From 40ae81dd3c43a596375d5bfdcc00053e786328cc Mon Sep 17 00:00:00 2001
From: Jake Howard <git@theorangeone.net>
Date: Wed, 5 Jan 2022 21:18:24 +0000
Subject: [PATCH] Move $BWRS_VERSION fallback into build.rs

---
 build.rs    | 39 ++++++++++++++++++---------------------
 src/main.rs | 10 +---------
 2 files changed, 19 insertions(+), 30 deletions(-)

diff --git a/build.rs b/build.rs
index f638aeeb..7c0b08c8 100644
--- a/build.rs
+++ b/build.rs
@@ -15,11 +15,14 @@ fn main() {
         "You need to enable one DB backend. To build with previous defaults do: cargo build --features sqlite"
     );
 
-    if let Ok(version) = env::var("VW_VERSION") {
+    // Support $BWRS_VERSION for legacy compatibility, but default to $VW_VERSION.
+    // If neither exist, read from git.
+    let maybe_vaultwarden_version =
+        env::var("VW_VERSION").or(env::var("BWRS_VERSION")).or_else(|_| version_from_git_info());
+
+    if let Ok(version) = maybe_vaultwarden_version {
         println!("cargo:rustc-env=VW_VERSION={}", version);
         println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version);
-    } else {
-        read_git_info().ok();
     }
 }
 
@@ -33,7 +36,13 @@ fn run(args: &[&str]) -> Result<String, std::io::Error> {
 }
 
 /// This method reads info from Git, namely tags, branch, and revision
-fn read_git_info() -> Result<(), std::io::Error> {
+/// To access these values, use:
+///    - env!("GIT_EXACT_TAG")
+///    - env!("GIT_LAST_TAG")
+///    - env!("GIT_BRANCH")
+///    - env!("GIT_REV")
+///    - env!("VW_VERSION")
+fn version_from_git_info() -> Result<String, std::io::Error> {
     // The exact tag for the current commit, can be empty when
     // the current commit doesn't have an associated tag
     let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
@@ -56,23 +65,11 @@ fn read_git_info() -> Result<(), std::io::Error> {
     println!("cargo:rustc-env=GIT_REV={}", rev_short);
 
     // Combined version
-    let version = if let Some(exact) = exact_tag {
-        exact
+    if let Some(exact) = exact_tag {
+        Ok(exact)
     } else if &branch != "main" && &branch != "master" {
-        format!("{}-{} ({})", last_tag, rev_short, branch)
+        Ok(format!("{}-{} ({})", last_tag, rev_short, branch))
     } else {
-        format!("{}-{}", last_tag, rev_short)
-    };
-
-    println!("cargo:rustc-env=VW_VERSION={}", version);
-    println!("cargo:rustc-env=CARGO_PKG_VERSION={}", version);
-
-    // To access these values, use:
-    //    env!("GIT_EXACT_TAG")
-    //    env!("GIT_LAST_TAG")
-    //    env!("GIT_BRANCH")
-    //    env!("GIT_REV")
-    //    env!("VW_VERSION")
-
-    Ok(())
+        Ok(format!("{}-{}", last_tag, rev_short))
+    }
 }
diff --git a/src/main.rs b/src/main.rs
index 67b21322..d7bef292 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -76,15 +76,7 @@ const HELP: &str = "\
             -v, --version    Prints the app version
 ";
 
-// HACK: Option::or cannot be used in a constant context
-const fn get_version() -> Option<&'static str> {
-    let bwrs_version = option_env!("BWRS_VERSION");
-    match bwrs_version {
-        Some(_) => bwrs_version,
-        None => option_env!("VW_VERSION"),
-    }
-}
-pub const VERSION: Option<&str> = get_version();
+pub const VERSION: Option<&str> = option_env!("VW_VERSION");
 
 fn parse_args() {
     let mut pargs = pico_args::Arguments::from_env();