Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lockfile): track JSR and npm dependencies in config file #22004

Merged
merged 19 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ libz-sys = { version = "1.1", default-features = false }
log = "=0.4.20"
lsp-types = "=0.94.1" # used by tower-lsp and "proposed" feature is unstable in patch releases
memmem = "0.1.1"
monch = "=0.4.3"
monch = "=0.5.0"
notify = "=5.0.0"
num-bigint = { version = "0.4", features = ["rand"] }
once_cell = "1.17.1"
Expand Down Expand Up @@ -352,3 +352,10 @@ opt-level = 3
opt-level = 3
[profile.release.package.base64-simd]
opt-level = 3

[patch.crates-io]
deno_lockfile = { path = "../deno_lockfile" }
deno_npm = { path = "../deno_npm" }
deno_graph = { path = "../deno_graph" }
deno_semver = { path = "../deno_semver" }
import_map = { path = "../import_map/rs-lib" }
4 changes: 1 addition & 3 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ deno_lint = { version = "=0.53.0", features = ["docs"] }
deno_lockfile.workspace = true
deno_npm = "=0.15.3"
deno_runtime = { workspace = true, features = ["include_js_files_for_snapshotting"] }
# todo(dsherret): investigate https://github.com/denoland/deno_semver/commit/98f9174baef199809295077b3b68c9fa58defb9b causing
# lsp_completions_auto_import_and_quick_fix_with_import_map to fail when bumping this version
deno_semver = "=0.5.1"
deno_semver = "=0.5.3"
deno_task_shell = "=0.14.0"
eszip = "=0.57.0"
napi_sym.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,10 @@ impl CliOptions {
self.flags.no_npm
}

pub fn no_config(&self) -> bool {
self.flags.config_flag == deno_config::ConfigFlag::Disabled
}

pub fn permissions_options(&self) -> PermissionsOptions {
PermissionsOptions {
allow_env: self.flags.allow_env.clone(),
Expand Down
34 changes: 4 additions & 30 deletions cli/args/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::path::PathBuf;
use deno_core::anyhow::bail;
use deno_core::error::AnyError;
use deno_npm::registry::parse_dep_entry_name_and_raw_version;
use deno_npm::registry::PackageDepNpmSchemeValueParseError;
use deno_runtime::deno_node::PackageJson;
use deno_semver::package::PackageReq;
use deno_semver::VersionReq;
Expand All @@ -16,8 +15,6 @@ use thiserror::Error;

#[derive(Debug, Error, Clone)]
pub enum PackageJsonDepValueParseError {
#[error(transparent)]
SchemeValue(#[from] PackageDepNpmSchemeValueParseError),
#[error(transparent)]
Specifier(#[from] VersionReqSpecifierParseError),
#[error("Not implemented scheme '{scheme}'")]
Expand All @@ -39,17 +36,17 @@ impl PackageJsonDepsProvider {
self.0.as_ref()
}

pub fn reqs(&self) -> Vec<&PackageReq> {
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
package_reqs
Some(package_reqs)
}
None => Vec::new(),
None => None,
}
}
}
Expand Down Expand Up @@ -77,9 +74,7 @@ pub fn get_local_package_json_version_reqs(
scheme: value.split(':').next().unwrap().to_string(),
});
}
let (name, version_req) = parse_dep_entry_name_and_raw_version(key, value)
.map_err(PackageJsonDepValueParseError::SchemeValue)?;

let (name, version_req) = parse_dep_entry_name_and_raw_version(key, value);
let result = VersionReq::parse_from_specifier(version_req);
match result {
Ok(version_req) => Ok(PackageReq {
Expand Down Expand Up @@ -159,27 +154,6 @@ mod test {

use super::*;

#[test]
fn test_parse_dep_entry_name_and_raw_version() {
let cases = [
("test", "^1.2", Ok(("test", "^1.2"))),
("test", "1.x - 2.6", Ok(("test", "1.x - 2.6"))),
("test", "npm:package@^1.2", Ok(("package", "^1.2"))),
(
"test",
"npm:package",
Err("Could not find @ symbol in npm url 'npm:package'"),
),
];
for (key, value, expected_result) in cases {
let result = parse_dep_entry_name_and_raw_version(key, value);
match result {
Ok(result) => assert_eq!(result, expected_result.unwrap()),
Err(err) => assert_eq!(err.to_string(), expected_result.err().unwrap()),
}
}
}
bartlomieju marked this conversation as resolved.
Show resolved Hide resolved

fn get_local_package_json_version_reqs_for_tests(
package_json: &PackageJson,
) -> IndexMap<String, Result<PackageReq, String>> {
Expand Down
50 changes: 48 additions & 2 deletions cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ use crate::standalone::DenoCompileBinaryWriter;
use crate::tools::check::TypeChecker;
use crate::util::file_watcher::WatcherCommunicator;
use crate::util::fs::canonicalize_path_maybe_not_exists;
use crate::util::import_map::deno_json_deps;
use crate::util::import_map::import_map_deps;
use crate::util::progress_bar::ProgressBar;
use crate::util::progress_bar::ProgressBarStyle;
use crate::worker::CliMainWorkerFactory;
Expand All @@ -54,12 +56,14 @@ use deno_core::parking_lot::Mutex;
use deno_core::FeatureChecker;

use deno_graph::GraphKind;
use deno_lockfile::WorkspaceMemberConfig;
use deno_runtime::deno_fs;
use deno_runtime::deno_node::analyze::NodeCodeTranslator;
use deno_runtime::deno_node::NodeResolver;
use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_runtime::deno_web::BlobStore;
use deno_runtime::inspector_server::InspectorServer;
use deno_semver::package::PackageNv;
use import_map::ImportMap;
use log::warn;
use std::future::Future;
Expand Down Expand Up @@ -303,6 +307,48 @@ impl CliFactory {
.npm_resolver
.get_or_try_init_async(async {
let fs = self.fs();

let maybe_lockfile = self.maybe_lockfile();
if let Some(lockfile) = maybe_lockfile.as_ref() {
// todo(THIS PR): makes no sense for this to be here
let mut lockfile = lockfile.lock();
let package_json_deps = self.package_json_deps_provider().reqs().map(|reqs| reqs.into_iter().map(|s| format!("npm:{}", s)).collect());
let config = match self.options.maybe_workspace_config() {
Some(workspace_config) => {
deno_lockfile::WorkspaceConfig {
root: WorkspaceMemberConfig {
package_json_deps,
dependencies: Some(import_map_deps(&workspace_config.base_import_map_value).into_iter().map(|req| req.to_string()).collect()),
},
members: workspace_config.members.iter().map(|member| {
(member.package_name.clone(), WorkspaceMemberConfig {
package_json_deps: None,
dependencies: Some(deno_json_deps(&member.config_file).into_iter().map(|req| req.to_string()).collect()),
})
}).collect(),
}
}
None => {
deno_lockfile::WorkspaceConfig {
root: WorkspaceMemberConfig {
package_json_deps,
dependencies: self.options.maybe_config_file().as_ref().map(|config| deno_json_deps(config).into_iter().map(|req| req.to_string()).collect()),
},
members: Default::default(),
}
}
};
lockfile.set_workspace_config(deno_lockfile::SetWorkspaceConfigOptions {
no_npm: self.options.no_npm(),
no_config: self.options.no_config(),
config,
nv_to_jsr_url: |nv| {
let nv = PackageNv::from_str(nv).ok()?;
Some(deno_graph::source::recommended_registry_package_url(crate::args::deno_registry_url(), &nv).to_string())
}
});
}

create_cli_npm_resolver(if self.options.unstable_byonm() {
CliNpmResolverCreateOptions::Byonm(CliNpmResolverByonmCreateOptions {
fs: fs.clone(),
Expand All @@ -320,7 +366,7 @@ impl CliFactory {
Some(snapshot) => {
CliNpmResolverManagedSnapshotOption::Specified(Some(snapshot))
}
None => match self.maybe_lockfile() {
None => match maybe_lockfile.as_ref() {
Some(lockfile) => {
CliNpmResolverManagedSnapshotOption::ResolveFromLockfile(
lockfile.clone(),
Expand All @@ -329,7 +375,7 @@ impl CliFactory {
None => CliNpmResolverManagedSnapshotOption::Specified(None),
},
},
maybe_lockfile: self.maybe_lockfile().as_ref().cloned(),
maybe_lockfile: maybe_lockfile.as_ref().cloned(),
fs: fs.clone(),
http_client: self.http_client().clone(),
npm_global_cache_dir: self.deno_dir()?.npm_folder_path(),
Expand Down
4 changes: 4 additions & 0 deletions cli/graph_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,10 @@ impl ModuleGraphBuilder {
format!("jsr:{}", to),
);
}
for (name, deps) in graph.packages.package_deps() {
lockfile
.insert_package_deps(name.to_string(), deps.map(|s| s.to_string()));
}
}
}

Expand Down
Loading