Skip to content

Commit 73628eb

Browse files
authored
fix(byonm): Deno.readFile* fails to recognize paths correctly when called from a node package (#601)
1 parent 8590136 commit 73628eb

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

crates/base/src/runtime/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,10 @@ where
658658
base_url,
659659
} = rt_provider;
660660

661+
let node_modules = metadata
662+
.node_modules()
663+
.ok()
664+
.flatten();
661665
let entrypoint = metadata.entrypoint.clone();
662666
let main_module_url = match entrypoint.as_ref() {
663667
Some(Entrypoint::Key(key)) => base_url.join(key)?,
@@ -717,6 +721,7 @@ where
717721

718722
let (fs, s3_fs) = build_file_system_fn(if is_user_worker {
719723
Arc::new(StaticFs::new(
724+
node_modules,
720725
static_files,
721726
if matches!(entrypoint, Some(Entrypoint::ModuleCode(_)) | None)
722727
&& is_some_entry_point

crates/fs/impl/static_fs.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::PathBuf;
66
use std::rc::Rc;
77
use std::sync::Arc;
88

9+
use deno::standalone::binary::NodeModules;
910
use deno_core::normalize_path;
1011
use deno_fs::AccessCheckCb;
1112
use deno_fs::FsDirEntry;
@@ -26,22 +27,34 @@ pub struct StaticFs {
2627
static_files: EszipStaticFiles,
2728
base_dir_path: PathBuf,
2829
vfs_path: PathBuf,
30+
byonm_node_modules_path: Option<PathBuf>,
2931
snapshot: Option<ValidSerializedNpmResolutionSnapshot>,
3032
vfs: Arc<FileBackedVfs>,
3133
}
3234

3335
impl StaticFs {
3436
pub fn new(
37+
node_modules: Option<NodeModules>,
3538
static_files: EszipStaticFiles,
3639
base_dir_path: PathBuf,
3740
vfs_path: PathBuf,
3841
vfs: Arc<FileBackedVfs>,
3942
snapshot: Option<ValidSerializedNpmResolutionSnapshot>,
4043
) -> Self {
44+
let byonm_node_modules_path = if let Some(NodeModules::Byonm {
45+
root_node_modules_dir: Some(path),
46+
}) = node_modules
47+
{
48+
Some(vfs_path.join(path))
49+
} else {
50+
None
51+
};
52+
4153
Self {
4254
vfs,
4355
static_files,
4456
base_dir_path,
57+
byonm_node_modules_path,
4558
vfs_path,
4659
snapshot,
4760
}
@@ -375,7 +388,13 @@ impl deno_fs::FileSystem for StaticFs {
375388
_access_check: Option<AccessCheckCb>,
376389
) -> FsResult<Cow<'static, [u8]>> {
377390
let is_npm = self.is_valid_npm_package(path);
378-
if is_npm {
391+
let is_byonm_path = self
392+
.byonm_node_modules_path
393+
.as_ref()
394+
.map(|it| path.starts_with(it))
395+
.unwrap_or_default();
396+
397+
if is_npm || is_byonm_path {
379398
let options = OpenOptions::read();
380399
let file = self.open_sync(path, options, None)?;
381400
let buf = file.read_all_sync()?;

deno/standalone/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use indexmap::IndexMap;
99
use serde::Deserialize;
1010
use serde::Serialize;
1111

12-
#[derive(Deserialize, Serialize)]
12+
#[derive(Debug, Clone, Deserialize, Serialize)]
1313
pub enum NodeModules {
1414
Managed {
1515
/// Relative path for the node_modules directory in the vfs.

0 commit comments

Comments
 (0)