From 21863add07ef52ee4dbfde7cc10a303a773357b8 Mon Sep 17 00:00:00 2001 From: Anthony Shew Date: Mon, 4 Nov 2024 17:06:38 -0700 Subject: [PATCH] fix: Don't require a name for the root package.json. (#9378) --- .../src/package_graph/mod.rs | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/crates/turborepo-repository/src/package_graph/mod.rs b/crates/turborepo-repository/src/package_graph/mod.rs index 439fabf97e1dd..15cd6351b02cc 100644 --- a/crates/turborepo-repository/src/package_graph/mod.rs +++ b/crates/turborepo-repository/src/package_graph/mod.rs @@ -145,9 +145,20 @@ impl PackageGraph { pub fn validate(&self) -> Result<(), Error> { for info in self.packages.values() { let name = info.package_json.name.as_deref(); - if matches!(name, None | Some("")) { - let package_json_path = self.repo_root.resolve(info.package_json_path()); - return Err(Error::PackageJsonMissingName(package_json_path)); + let package_json_path = self.repo_root.resolve(info.package_json_path()); + match name { + Some("") => { + return Err(Error::PackageJsonMissingName(package_json_path)); + } + None => { + // We don't need to require a name for the root package.json. + if package_json_path == self.repo_root.join_component("package.json") { + continue; + } + + return Err(Error::PackageJsonMissingName(package_json_path)); + } + Some(_) => continue, } } graph::validate_graph(&self.graph).map_err(Error::InvalidPackageGraph)?; @@ -902,4 +913,17 @@ mod test { )) ); } + + #[tokio::test] + async fn test_does_not_require_name_for_root_package_json() { + let root = + AbsoluteSystemPathBuf::new(if cfg!(windows) { r"C:\repo" } else { "/repo" }).unwrap(); + let pkg_graph = PackageGraph::builder(&root, PackageJson::from_value(json!({})).unwrap()) + .with_package_discovery(MockDiscovery) + .build() + .await + .unwrap(); + + assert!(pkg_graph.validate().is_ok()); + } }