feat: npm workspace and better Deno workspace support (#24334)

Adds much better support for the unstable Deno workspaces as well as
support for npm workspaces. npm workspaces is still lacking in that we
only install packages into the root node_modules folder. We'll make it
smarter over time in order for it to figure out when to add node_modules
folders within packages.

This includes a breaking change in config file resolution where we stop
searching for config files on the first found package.json unless it's
in a workspace. For the previous behaviour, the root deno.json needs to
be updated to be a workspace by adding `"workspace":
["./path-to-pkg-json-folder-goes-here"]`. See details in
https://github.com/denoland/deno_config/pull/66

Closes #24340
Closes #24159
Closes #24161
Closes #22020
Closes #18546
Closes #16106
Closes #24160
This commit is contained in:
David Sherret 2024-07-03 20:54:33 -04:00 committed by GitHub
parent dd6d19e120
commit 147411e64b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
235 changed files with 4446 additions and 2451 deletions

View file

@ -1,77 +1,87 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;
use deno_config::package_json::PackageJsonDeps;
use deno_core::anyhow::bail;
use deno_core::error::AnyError;
use deno_runtime::deno_fs::RealFs;
use deno_runtime::deno_node::load_pkg_json;
use deno_runtime::deno_node::PackageJson;
use deno_config::package_json::PackageJsonDepValue;
use deno_config::workspace::Workspace;
use deno_semver::package::PackageReq;
#[derive(Debug, Default)]
pub struct PackageJsonDepsProvider(Option<PackageJsonDeps>);
impl PackageJsonDepsProvider {
pub fn new(deps: Option<PackageJsonDeps>) -> Self {
Self(deps)
}
pub fn deps(&self) -> Option<&PackageJsonDeps> {
self.0.as_ref()
}
pub fn reqs(&self) -> Option<Vec<&PackageReq>> {
match &self.0 {
Some(deps) => {
let mut package_reqs = deps
.values()
.filter_map(|r| r.as_ref().ok())
.collect::<Vec<_>>();
package_reqs.sort(); // deterministic resolution
Some(package_reqs)
}
None => None,
}
}
#[derive(Debug)]
pub struct InstallNpmWorkspacePkg {
pub alias: String,
pub pkg_dir: PathBuf,
}
/// Attempts to discover the package.json file, maybe stopping when it
/// reaches the specified `maybe_stop_at` directory.
pub fn discover_from(
start: &Path,
maybe_stop_at: Option<PathBuf>,
) -> Result<Option<Arc<PackageJson>>, AnyError> {
const PACKAGE_JSON_NAME: &str = "package.json";
// todo(#24419): this is not correct, but it's good enough for now.
// We need deno_npm to be able to understand workspace packages and
// then have a way to properly lay them out on the file system
#[derive(Debug, Default)]
pub struct PackageJsonInstallDepsProvider {
remote_pkg_reqs: Vec<PackageReq>,
workspace_pkgs: Vec<InstallNpmWorkspacePkg>,
}
// note: ancestors() includes the `start` path
for ancestor in start.ancestors() {
let path = ancestor.join(PACKAGE_JSON_NAME);
impl PackageJsonInstallDepsProvider {
pub fn empty() -> Self {
Self::default()
}
let package_json = match load_pkg_json(&RealFs, &path) {
Ok(Some(package_json)) => package_json,
Ok(None) => {
if let Some(stop_at) = maybe_stop_at.as_ref() {
if ancestor == stop_at {
break;
pub fn from_workspace(workspace: &Arc<Workspace>) -> Self {
let mut workspace_pkgs = Vec::new();
let mut remote_pkg_reqs = Vec::new();
let workspace_npm_pkgs = workspace.npm_packages();
for pkg_json in workspace.package_jsons() {
let deps = pkg_json.resolve_local_package_json_deps();
let mut pkg_reqs = Vec::with_capacity(deps.len());
for (alias, dep) in deps {
let Ok(dep) = dep else {
continue;
};
match dep {
PackageJsonDepValue::Req(pkg_req) => {
if let Some(pkg) = workspace_npm_pkgs
.iter()
.find(|pkg| pkg.matches_req(&pkg_req))
{
workspace_pkgs.push(InstallNpmWorkspacePkg {
alias,
pkg_dir: pkg.pkg_json.dir_path().to_path_buf(),
});
} else {
pkg_reqs.push(pkg_req)
}
}
PackageJsonDepValue::Workspace(version_req) => {
if let Some(pkg) = workspace_npm_pkgs.iter().find(|pkg| {
pkg.matches_name_and_version_req(&alias, &version_req)
}) {
workspace_pkgs.push(InstallNpmWorkspacePkg {
alias,
pkg_dir: pkg.pkg_json.dir_path().to_path_buf(),
});
}
}
}
continue;
}
Err(err) => bail!(
"Error loading package.json at {}. {:#}",
path.display(),
err
),
};
// sort within each package
pkg_reqs.sort();
log::debug!("package.json file found at '{}'", path.display());
return Ok(Some(package_json));
remote_pkg_reqs.extend(pkg_reqs);
}
remote_pkg_reqs.shrink_to_fit();
workspace_pkgs.shrink_to_fit();
Self {
remote_pkg_reqs,
workspace_pkgs,
}
}
log::debug!("No package.json file found");
Ok(None)
pub fn remote_pkg_reqs(&self) -> &Vec<PackageReq> {
&self.remote_pkg_reqs
}
pub fn workspace_pkgs(&self) -> &Vec<InstallNpmWorkspacePkg> {
&self.workspace_pkgs
}
}