summaryrefslogtreecommitdiff
path: root/cli/standalone/virtual_fs.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2024-07-03 20:54:33 -0400
committerGitHub <noreply@github.com>2024-07-04 00:54:33 +0000
commit147411e64b22fe74cb258125acab83f9182c9f81 (patch)
treea1f63dcbf0404c20534986b10f02b649df5a3ad5 /cli/standalone/virtual_fs.rs
parentdd6d19e12051fac2ea5639f621501f4710a1b8e1 (diff)
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
Diffstat (limited to 'cli/standalone/virtual_fs.rs')
-rw-r--r--cli/standalone/virtual_fs.rs41
1 files changed, 32 insertions, 9 deletions
diff --git a/cli/standalone/virtual_fs.rs b/cli/standalone/virtual_fs.rs
index 3e6823d50..ee91b9f7f 100644
--- a/cli/standalone/virtual_fs.rs
+++ b/cli/standalone/virtual_fs.rs
@@ -12,6 +12,7 @@ use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
+use deno_core::anyhow::anyhow;
use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::parking_lot::Mutex;
@@ -55,9 +56,8 @@ impl VfsBuilder {
root_dir: VirtualDirectory {
name: root_path
.file_stem()
- .unwrap()
- .to_string_lossy()
- .into_owned(),
+ .map(|s| s.to_string_lossy().into_owned())
+ .unwrap_or("root".to_string()),
entries: Vec::new(),
},
root_path,
@@ -67,13 +67,19 @@ impl VfsBuilder {
})
}
- pub fn set_root_dir_name(&mut self, name: String) {
- self.root_dir.name = name;
+ pub fn with_root_dir<R>(
+ &mut self,
+ with_root: impl FnOnce(&mut VirtualDirectory) -> R,
+ ) -> R {
+ with_root(&mut self.root_dir)
}
pub fn add_dir_recursive(&mut self, path: &Path) -> Result<(), AnyError> {
- let path = canonicalize_path(path)?;
- self.add_dir_recursive_internal(&path)
+ let target_path = canonicalize_path(path)?;
+ if path != target_path {
+ self.add_symlink(path, &target_path)?;
+ }
+ self.add_dir_recursive_internal(&target_path)
}
fn add_dir_recursive_internal(
@@ -92,7 +98,7 @@ impl VfsBuilder {
if file_type.is_dir() {
self.add_dir_recursive_internal(&path)?;
} else if file_type.is_file() {
- self.add_file_at_path(&path)?;
+ self.add_file_at_path_not_symlink(&path)?;
} else if file_type.is_symlink() {
match util::fs::canonicalize_path(&path) {
Ok(target) => {
@@ -175,6 +181,17 @@ impl VfsBuilder {
}
pub fn add_file_at_path(&mut self, path: &Path) -> Result<(), AnyError> {
+ let target_path = canonicalize_path(path)?;
+ if target_path != path {
+ self.add_symlink(path, &target_path)?;
+ }
+ self.add_file_at_path_not_symlink(&target_path)
+ }
+
+ pub fn add_file_at_path_not_symlink(
+ &mut self,
+ path: &Path,
+ ) -> Result<(), AnyError> {
let file_bytes = std::fs::read(path)
.with_context(|| format!("Reading {}", path.display()))?;
self.add_file(path, file_bytes)
@@ -195,7 +212,9 @@ impl VfsBuilder {
let name = path.file_name().unwrap().to_string_lossy();
let data_len = data.len();
match dir.entries.binary_search_by(|e| e.name().cmp(&name)) {
- Ok(_) => unreachable!(),
+ Ok(_) => {
+ // already added, just ignore
+ }
Err(insert_index) => {
dir.entries.insert(
insert_index,
@@ -228,6 +247,10 @@ impl VfsBuilder {
target.display()
);
let dest = self.path_relative_root(target)?;
+ if dest == self.path_relative_root(path)? {
+ // it's the same, ignore
+ return Ok(());
+ }
let dir = self.add_dir(path.parent().unwrap())?;
let name = path.file_name().unwrap().to_string_lossy();
match dir.entries.binary_search_by(|e| e.name().cmp(&name)) {