Skip to content

Commit

Permalink
Check for Depot version
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Jun 27, 2024
1 parent b80ade6 commit 4c6903b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 27 deletions.
12 changes: 10 additions & 2 deletions crates/depot/src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
utils,
workspace::{
package::{PackageDepotConfig, PackageName, Platform, Target},
Workspace,
Workspace, WorkspaceDepotConfig, DEPOT_VERSION,
},
CommonArgs,
};
Expand Down Expand Up @@ -172,6 +172,9 @@ impl NewCommand {

let manifest = json!({
"private": true,
"depot": {
"depot-version": DEPOT_VERSION
},
// STUPID HACK: see note on same code in new_package
"pnpm": {
"overrides": {
Expand Down Expand Up @@ -640,7 +643,12 @@ export default defineConfig(({{ mode }}) => ({{
target: Some(*target),
no_server: None,
};
other.insert("depot".into(), serde_json::to_value(pkg_config)?);
let ws_config = WorkspaceDepotConfig {
depot_version: DEPOT_VERSION.to_string(),
};
let mut config = serde_json::to_value(pkg_config)?;
json_merge(&mut config, serde_json::to_value(ws_config)?);
other.insert("depot".into(), config);

// STUPID HACK:
// - This npm bug (and I guess pnpm bug) causes platform-specific rollup packages to not be installed:
Expand Down
27 changes: 27 additions & 0 deletions crates/depot/src/workspace/manifest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use anyhow::{Context, Result};
use std::{fs, path::Path};

use package_json_schema::PackageJson;
use serde::de::DeserializeOwned;

pub struct DepotManifest<Config> {
pub manifest: PackageJson,
pub config: Config,
}

impl<Config: DeserializeOwned> DepotManifest<Config> {
pub fn load(path: &Path) -> Result<Self> {
let contents = fs::read_to_string(path)
.with_context(|| format!("Missing manifest at: `{}`", path.display()))?;
let manifest = PackageJson::try_from(contents)?;
Self::from_json(manifest, path)
}

pub fn from_json(mut manifest: PackageJson, path: &Path) -> Result<Self> {
let error_msg = || format!("Missing \"depot\" key from manifest: `{}`", path.display());
let other = manifest.other.as_mut().with_context(error_msg)?;
let config_value = other.remove("depot").with_context(error_msg)?;
let config: Config = serde_json::from_value(config_value)?;
Ok(DepotManifest { manifest, config })
}
}
24 changes: 23 additions & 1 deletion crates/depot/src/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ use futures::{
stream::{self, TryStreamExt},
StreamExt,
};
use log::debug;
use log::{debug, warn};
use manifest::DepotManifest;
use package::Package;
use std::{
cmp::Ordering,
Expand All @@ -25,10 +26,19 @@ use std::{

mod dep_graph;
mod fingerprint;
mod manifest;
pub mod package;
pub mod process;
mod runner;

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct WorkspaceDepotConfig {
pub depot_version: String,
}

pub type WorkspaceManifest = DepotManifest<WorkspaceDepotConfig>;

/// Represents an entire Depot workspace.
///
/// This is a central data structure that is held by many parts of the application,
Expand Down Expand Up @@ -189,6 +199,8 @@ pub trait WorkspaceCommand: CoreCommand + Debug + Send + Sync + 'static {
}
}

pub const DEPOT_VERSION: &str = env!("CARGO_PKG_VERSION");

impl Workspace {
pub async fn load(
global_config: GlobalConfig,
Expand Down Expand Up @@ -219,6 +231,16 @@ impl Workspace {
let monorepo = pkg_dir.exists();
debug!("Workspace is monorepo: {monorepo}");

let manifest = WorkspaceManifest::load(&root.join("package.json"))?;
let created_version = &manifest.config.depot_version;
if DEPOT_VERSION != created_version {
warn!(
r#"Depot binary is v{DEPOT_VERSION} but workspace was created with v{created_version}.
Double-check that this workspace is compatible and update depot.depot_version in package.json."#
);
}

let pkg_roots = if monorepo {
pkg_dir
.read_dir()?
Expand Down
26 changes: 2 additions & 24 deletions crates/depot/src/workspace/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use anyhow::{bail, ensure, Context, Error, Result};

use ignore::Walk;
use maplit::hashset;
use package_json_schema::PackageJson;
use std::{
fmt::{self, Debug},
fs,
hash::Hash,
path::{Path, PathBuf},
str::FromStr,
Expand All @@ -14,7 +12,7 @@ use std::{

use crate::{shareable, workspace::process::Process};

use super::{dep_graph::DepGraph, Workspace};
use super::{dep_graph::DepGraph, manifest::DepotManifest, Workspace};

#[derive(Copy, Clone, clap::ValueEnum, serde::Serialize, serde::Deserialize)]
pub enum Platform {
Expand Down Expand Up @@ -120,27 +118,7 @@ pub struct PackageDepotConfig {
pub no_server: Option<bool>,
}

pub struct PackageManifest {
pub manifest: PackageJson,
pub config: PackageDepotConfig,
}

impl PackageManifest {
pub fn load(path: &Path) -> Result<Self> {
let contents = fs::read_to_string(path)
.with_context(|| format!("Package does not have manifest at: `{}`", path.display()))?;
let manifest = PackageJson::try_from(contents)?;
Self::from_json(manifest, path)
}

pub fn from_json(mut manifest: PackageJson, path: &Path) -> Result<Self> {
let error_msg = || format!("Missing \"depot\" key from manifest: `{}`", path.display());
let other = manifest.other.as_mut().with_context(error_msg)?;
let config_value = other.remove("depot").with_context(error_msg)?;
let config: PackageDepotConfig = serde_json::from_value(config_value)?;
Ok(PackageManifest { manifest, config })
}
}
pub type PackageManifest = DepotManifest<PackageDepotConfig>;

pub type PackageIndex = usize;

Expand Down

0 comments on commit 4c6903b

Please sign in to comment.