Skip to content

Commit

Permalink
refactor: version module exports a single const struct (#25014)
Browse files Browse the repository at this point in the history
This commit rewrites the internal `version` module that exported
various information about the current executable. Instead of exporting
several consts, we are now exporting a single const structure that 
contains all the necessary information.

This is the first step towards cleaning up how we use this information
and should allow us to use SUI to be able to patch this information
in already produced binary making it easier to cut new releases.

---------

Co-authored-by: Divy Srivastava <[email protected]>
  • Loading branch information
bartlomieju and littledivy authored Aug 15, 2024
1 parent 5ec3c5c commit 2bb013f
Show file tree
Hide file tree
Showing 21 changed files with 214 additions and 142 deletions.
11 changes: 7 additions & 4 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ static UNSTABLE_HEADING: &str = "Unstable";
pub fn clap_root() -> Command {
let long_version = format!(
"{} ({}, {})\nv8 {}\ntypescript {}",
crate::version::deno(),
crate::version::DENO_VERSION_INFO.deno,
// TODO(bartlomieju): alter what's printed here.
// I think it's best if we print as follows:
// <version>(+<short_git_hash>) (<release_channel>, <profile>, <target>)
Expand All @@ -1346,14 +1346,17 @@ pub fn clap_root() -> Command {
// v2.1.13-lts (LTS (long term support), release, aarch64-apple-darwin)
// For canary it would be:
// v1.46.0+25bb59d (canary, release, aarch64-apple-darwin)
if matches!(crate::version::RELEASE_CHANNEL, ReleaseChannel::Canary) {
if matches!(
crate::version::DENO_VERSION_INFO.release_channel,
ReleaseChannel::Canary
) {
"canary"
} else {
env!("PROFILE")
},
env!("TARGET"),
deno_core::v8_version(),
crate::version::TYPESCRIPT
crate::version::DENO_VERSION_INFO.typescript
);

let mut cmd = run_args(Command::new("deno"), true)
Expand All @@ -1368,7 +1371,7 @@ pub fn clap_root() -> Command {
)
.color(ColorChoice::Auto)
.term_width(800)
.version(crate::version::deno())
.version(crate::version::DENO_VERSION_INFO.deno)
.long_version(long_version)
// cause --unstable flags to display at the bottom of the help text
.next_display_order(1000)
Expand Down
5 changes: 4 additions & 1 deletion cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,7 +1293,10 @@ impl CliOptions {
return Ok(None);
};

Ok(Some(InspectorServer::new(host, version::get_user_agent())?))
Ok(Some(InspectorServer::new(
host,
version::DENO_VERSION_INFO.user_agent,
)?))
}

pub fn maybe_lockfile(&self) -> Option<&Arc<CliLockfile>> {
Expand Down
12 changes: 1 addition & 11 deletions cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::path::PathBuf;

use deno_core::snapshot::*;
use deno_runtime::*;
mod shared;

mod ts {
use super::*;
Expand Down Expand Up @@ -329,18 +330,7 @@ mod ts {
fn create_cli_snapshot(snapshot_path: PathBuf) {
use deno_runtime::ops::bootstrap::SnapshotOptions;

// NOTE(bartlomieju): keep in sync with `cli/version.rs`.
// Ideally we could deduplicate that code.
fn deno_version() -> String {
if env::var("DENO_CANARY").is_ok() {
format!("{}+{}", env!("CARGO_PKG_VERSION"), &git_commit_hash()[..7])
} else {
env!("CARGO_PKG_VERSION").to_string()
}
}

let snapshot_options = SnapshotOptions {
deno_version: deno_version(),
ts_version: ts::version(),
v8_version: deno_core::v8_version(),
target: std::env::var("TARGET").unwrap(),
Expand Down
8 changes: 6 additions & 2 deletions cli/cache/caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ impl Caches {
cell
.get_or_init(|| {
if let Some(path) = path {
CacheDB::from_path(config, path, crate::version::deno())
CacheDB::from_path(
config,
path,
crate::version::DENO_VERSION_INFO.deno,
)
} else {
CacheDB::in_memory(config, crate::version::deno())
CacheDB::in_memory(config, crate::version::DENO_VERSION_INFO.deno)
}
})
.clone()
Expand Down
2 changes: 1 addition & 1 deletion cli/cache/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl FastInsecureHasher {

pub fn new_deno_versioned() -> Self {
let mut hasher = Self::new_without_deno_version();
hasher.write_str(crate::version::deno());
hasher.write_str(crate::version::DENO_VERSION_INFO.deno);
hasher
}

Expand Down
2 changes: 1 addition & 1 deletion cli/cache/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl EmitCache {
pub fn new(disk_cache: DiskCache) -> Self {
Self {
disk_cache,
cli_version: crate::version::deno(),
cli_version: crate::version::DENO_VERSION_INFO.deno,
emit_failed_flag: Default::default(),
}
}
Expand Down
16 changes: 8 additions & 8 deletions cli/http_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::auth_tokens::AuthToken;
use crate::util::progress_bar::UpdateGuard;
use crate::version::get_user_agent;
use crate::version;

use cache_control::Cachability;
use cache_control::CacheControl;
Expand Down Expand Up @@ -248,7 +248,7 @@ impl HttpClientProvider {
Entry::Occupied(entry) => Ok(HttpClient::new(entry.get().clone())),
Entry::Vacant(entry) => {
let client = create_http_client(
get_user_agent(),
version::DENO_VERSION_INFO.user_agent,
CreateHttpClientOptions {
root_cert_store: match &self.root_cert_store_provider {
Some(provider) => Some(provider.get_or_try_init()?.clone()),
Expand Down Expand Up @@ -948,7 +948,7 @@ mod test {

let client = HttpClient::new(
create_http_client(
version::get_user_agent(),
version::DENO_VERSION_INFO.user_agent,
CreateHttpClientOptions {
ca_certs: vec![std::fs::read(
test_util::testdata_path().join("tls/RootCA.pem"),
Expand Down Expand Up @@ -1000,7 +1000,7 @@ mod test {

let client = HttpClient::new(
create_http_client(
version::get_user_agent(),
version::DENO_VERSION_INFO.user_agent,
CreateHttpClientOptions::default(),
)
.unwrap(),
Expand Down Expand Up @@ -1059,7 +1059,7 @@ mod test {

let client = HttpClient::new(
create_http_client(
version::get_user_agent(),
version::DENO_VERSION_INFO.user_agent,
CreateHttpClientOptions {
root_cert_store: Some(root_cert_store),
..Default::default()
Expand Down Expand Up @@ -1108,7 +1108,7 @@ mod test {
.unwrap();
let client = HttpClient::new(
create_http_client(
version::get_user_agent(),
version::DENO_VERSION_INFO.user_agent,
CreateHttpClientOptions {
ca_certs: vec![std::fs::read(
test_util::testdata_path()
Expand Down Expand Up @@ -1149,7 +1149,7 @@ mod test {
let url = Url::parse("https://localhost:5545/etag_script.ts").unwrap();
let client = HttpClient::new(
create_http_client(
version::get_user_agent(),
version::DENO_VERSION_INFO.user_agent,
CreateHttpClientOptions {
ca_certs: vec![std::fs::read(
test_util::testdata_path()
Expand Down Expand Up @@ -1205,7 +1205,7 @@ mod test {
.unwrap();
let client = HttpClient::new(
create_http_client(
version::get_user_agent(),
version::DENO_VERSION_INFO.user_agent,
CreateHttpClientOptions {
ca_certs: vec![std::fs::read(
test_util::testdata_path()
Expand Down
2 changes: 1 addition & 1 deletion cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ impl Inner {

let version = format!(
"{} ({}, {})",
crate::version::deno(),
crate::version::DENO_VERSION_INFO.deno,
env!("PROFILE"),
env!("TARGET")
);
Expand Down
2 changes: 1 addition & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn setup_panic_hook() {
eprintln!("var set and include the backtrace in your report.");
eprintln!();
eprintln!("Platform: {} {}", env::consts::OS, env::consts::ARCH);
eprintln!("Version: {}", version::deno());
eprintln!("Version: {}", version::DENO_VERSION_INFO.deno);
eprintln!("Args: {:?}", env::args().collect::<Vec<_>>());
eprintln!();
orig_hook(panic_info);
Expand Down
1 change: 1 addition & 0 deletions cli/shared.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

/// This module is shared between build script and the binaries. Use it sparsely.
use deno_core::anyhow::bail;
use deno_core::error::AnyError;

Expand Down
29 changes: 17 additions & 12 deletions cli/standalone/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,23 @@ impl<'a> DenoCompileBinaryWriter<'a> {
let target = compile_flags.resolve_target();
let binary_name = format!("denort-{target}.zip");

let binary_path_suffix = match crate::version::RELEASE_CHANNEL {
ReleaseChannel::Canary => {
format!("canary/{}/{}", crate::version::GIT_COMMIT_HASH, binary_name)
}
ReleaseChannel::Stable => {
format!("release/v{}/{}", env!("CARGO_PKG_VERSION"), binary_name)
}
_ => bail!(
"`deno compile` current doesn't support {} release channel",
crate::version::RELEASE_CHANNEL.name()
),
};
let binary_path_suffix =
match crate::version::DENO_VERSION_INFO.release_channel {
ReleaseChannel::Canary => {
format!(
"canary/{}/{}",
crate::version::DENO_VERSION_INFO.git_hash,
binary_name
)
}
ReleaseChannel::Stable => {
format!("release/v{}/{}", env!("CARGO_PKG_VERSION"), binary_name)
}
_ => bail!(
"`deno compile` current doesn't support {} release channel",
crate::version::DENO_VERSION_INFO.release_channel.name()
),
};

let download_directory = self.deno_dir.dl_folder_path();
let binary_path = download_directory.join(&binary_path_suffix);
Expand Down
1 change: 0 additions & 1 deletion cli/tools/bench/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use crate::util::file_watcher;
use crate::util::fs::collect_specifiers;
use crate::util::path::is_script_ext;
use crate::util::path::matches_pattern_or_exact_path;
use crate::version::get_user_agent;
use crate::worker::CliMainWorkerFactory;

use deno_config::glob::WalkEntry;
Expand Down
10 changes: 8 additions & 2 deletions cli/tools/bench/reporters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

use serde::Serialize;

use crate::version;

use super::*;

pub trait BenchReporter {
Expand All @@ -25,7 +27,11 @@ struct JsonReporterOutput {
impl Default for JsonReporterOutput {
fn default() -> Self {
Self {
runtime: format!("{} {}", get_user_agent(), env!("TARGET")),
runtime: format!(
"{} {}",
version::DENO_VERSION_INFO.user_agent,
env!("TARGET")
),
cpu: mitata::cpu::name(),
benches: vec![],
}
Expand Down Expand Up @@ -150,7 +156,7 @@ impl BenchReporter for ConsoleReporter {
"{}\n",
colors::gray(format!(
"runtime: deno {} ({})",
crate::version::deno(),
crate::version::DENO_VERSION_INFO.deno,
env!("TARGET")
))
);
Expand Down
4 changes: 2 additions & 2 deletions cli/tools/jupyter/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,10 +670,10 @@ fn kernel_info() -> messaging::KernelInfoReply {
status: ReplyStatus::Ok,
protocol_version: "5.3".to_string(),
implementation: "Deno kernel".to_string(),
implementation_version: crate::version::deno().to_string(),
implementation_version: crate::version::DENO_VERSION_INFO.deno.to_string(),
language_info: messaging::LanguageInfo {
name: "typescript".to_string(),
version: crate::version::TYPESCRIPT.to_string(),
version: crate::version::DENO_VERSION_INFO.typescript.to_string(),
mimetype: "text/x.typescript".to_string(),
file_extension: ".ts".to_string(),
pygments_lexer: "typescript".to_string(),
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub async fn run(
// Doing this manually, instead of using `log::info!` because these messages
// are supposed to go to stdout, not stderr.
if !cli_options.is_quiet() {
println!("Deno {}", crate::version::deno());
println!("Deno {}", crate::version::DENO_VERSION_INFO.deno);
println!("exit using ctrl+d, ctrl+c, or close()");
if repl_flags.is_default_command {
println!(
Expand Down
Loading

0 comments on commit 2bb013f

Please sign in to comment.