summaryrefslogtreecommitdiff
path: root/cli/npm/resolvers/mod.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-02-20 19:14:06 +0100
committerGitHub <noreply@github.com>2023-02-20 19:14:06 +0100
commit4d1a14ca7fa9496f36470a7771448a9b006b0204 (patch)
treede226f6368faec98065bf14382f23541484cfe72 /cli/npm/resolvers/mod.rs
parent88f6fc6a1684326ae1f947ea8ec24ad0bff0f449 (diff)
feat: auto-discover package.json for npm dependencies (#17272)
This commits adds auto-discovery of "package.json" file when running "deno run" and "deno task" subcommands. In case of "deno run" the "package.json" is being looked up starting from the directory of the script that is being run, stopping early if "deno.json(c)" file is found (ie. FS tree won't be traversed "up" from "deno.json"). When "package.json" is discovered the "--node-modules-dir" flag is implied, leading to creation of local "node_modules/" directory - we did that, because most tools relying on "package.json" will expect "node_modules/" directory to be present (eg. Vite). Additionally "dependencies" and "devDependencies" specified in the "package.json" are downloaded on startup. This is a stepping stone to supporting bare specifier imports, but the actual integration will be done in a follow up commit. --------- Co-authored-by: David Sherret <dsherret@gmail.com>
Diffstat (limited to 'cli/npm/resolvers/mod.rs')
-rw-r--r--cli/npm/resolvers/mod.rs100
1 files changed, 27 insertions, 73 deletions
diff --git a/cli/npm/resolvers/mod.rs b/cli/npm/resolvers/mod.rs
index 9dda160b0..a2638a15b 100644
--- a/cli/npm/resolvers/mod.rs
+++ b/cli/npm/resolvers/mod.rs
@@ -17,7 +17,6 @@ use deno_runtime::deno_node::NodeResolutionMode;
use deno_runtime::deno_node::PathClean;
use deno_runtime::deno_node::RequireNpmResolver;
use global::GlobalNpmPackageResolver;
-use once_cell::sync::Lazy;
use serde::Deserialize;
use serde::Serialize;
use std::collections::HashSet;
@@ -35,37 +34,11 @@ use super::NpmPackageNodeId;
use super::NpmResolutionSnapshot;
use super::RealNpmRegistryApi;
-const RESOLUTION_STATE_ENV_VAR_NAME: &str =
- "DENO_DONT_USE_INTERNAL_NODE_COMPAT_STATE";
-
-static IS_NPM_MAIN: Lazy<bool> =
- Lazy::new(|| std::env::var(RESOLUTION_STATE_ENV_VAR_NAME).is_ok());
-
/// State provided to the process via an environment variable.
-#[derive(Debug, Serialize, Deserialize)]
-struct NpmProcessState {
- snapshot: NpmResolutionSnapshot,
- local_node_modules_path: Option<String>,
-}
-
-impl NpmProcessState {
- pub fn was_set() -> bool {
- *IS_NPM_MAIN
- }
-
- pub fn take() -> Option<NpmProcessState> {
- // initialize the lazy before we remove the env var below
- if !Self::was_set() {
- return None;
- }
-
- let state = std::env::var(RESOLUTION_STATE_ENV_VAR_NAME).ok()?;
- let state = serde_json::from_str(&state).ok()?;
- // remove the environment variable so that sub processes
- // that are spawned do not also use this.
- std::env::remove_var(RESOLUTION_STATE_ENV_VAR_NAME);
- Some(state)
- }
+#[derive(Clone, Debug, Serialize, Deserialize)]
+pub struct NpmProcessState {
+ pub snapshot: NpmResolutionSnapshot,
+ pub local_node_modules_path: Option<String>,
}
#[derive(Clone)]
@@ -89,13 +62,8 @@ impl std::fmt::Debug for NpmPackageResolver {
}
impl NpmPackageResolver {
- pub fn new(
- cache: NpmCache,
- api: RealNpmRegistryApi,
- no_npm: bool,
- local_node_modules_path: Option<PathBuf>,
- ) -> Self {
- Self::new_inner(cache, api, no_npm, local_node_modules_path, None, None)
+ pub fn new(cache: NpmCache, api: RealNpmRegistryApi) -> Self {
+ Self::new_inner(cache, api, false, None, None, None)
}
pub async fn new_with_maybe_lockfile(
@@ -103,32 +71,34 @@ impl NpmPackageResolver {
api: RealNpmRegistryApi,
no_npm: bool,
local_node_modules_path: Option<PathBuf>,
+ initial_snapshot: Option<NpmResolutionSnapshot>,
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
) -> Result<Self, AnyError> {
- let maybe_snapshot = if let Some(lockfile) = &maybe_lockfile {
- if lockfile.lock().overwrite {
- None
- } else {
- Some(
- NpmResolutionSnapshot::from_lockfile(lockfile.clone(), &api)
- .await
- .with_context(|| {
- format!(
- "failed reading lockfile '{}'",
- lockfile.lock().filename.display()
- )
- })?,
- )
+ let mut initial_snapshot = initial_snapshot;
+
+ if initial_snapshot.is_none() {
+ if let Some(lockfile) = &maybe_lockfile {
+ if !lockfile.lock().overwrite {
+ initial_snapshot = Some(
+ NpmResolutionSnapshot::from_lockfile(lockfile.clone(), &api)
+ .await
+ .with_context(|| {
+ format!(
+ "failed reading lockfile '{}'",
+ lockfile.lock().filename.display()
+ )
+ })?,
+ )
+ }
}
- } else {
- None
- };
+ }
+
Ok(Self::new_inner(
cache,
api,
no_npm,
local_node_modules_path,
- maybe_snapshot,
+ initial_snapshot,
maybe_lockfile,
))
}
@@ -138,17 +108,9 @@ impl NpmPackageResolver {
api: RealNpmRegistryApi,
no_npm: bool,
local_node_modules_path: Option<PathBuf>,
- initial_snapshot: Option<NpmResolutionSnapshot>,
+ maybe_snapshot: Option<NpmResolutionSnapshot>,
maybe_lockfile: Option<Arc<Mutex<Lockfile>>>,
) -> Self {
- let process_npm_state = NpmProcessState::take();
- let local_node_modules_path = local_node_modules_path.or_else(|| {
- process_npm_state
- .as_ref()
- .and_then(|s| s.local_node_modules_path.as_ref().map(PathBuf::from))
- });
- let maybe_snapshot =
- initial_snapshot.or_else(|| process_npm_state.map(|s| s.snapshot));
let inner: Arc<dyn InnerNpmPackageResolver> = match &local_node_modules_path
{
Some(node_modules_folder) => Arc::new(LocalNpmPackageResolver::new(
@@ -289,14 +251,6 @@ impl NpmPackageResolver {
self.inner.set_package_reqs(packages).await
}
- // If the main module should be treated as being in an npm package.
- // This is triggered via a secret environment variable which is used
- // for functionality like child_process.fork. Users should NOT depend
- // on this functionality.
- pub fn is_npm_main(&self) -> bool {
- NpmProcessState::was_set()
- }
-
/// Gets the state of npm for the process.
pub fn get_npm_process_state(&self) -> String {
serde_json::to_string(&NpmProcessState {