Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/bundler/ParseTask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,7 @@ pub mod parse_worker {
false,
contents_file.unwrap_valid(),
read_arena,
bun_resolver::cache::NonRegularFile::Read,
) {
Ok(e) => {
// `bun_resolver::cache::Entry` ↔ `crate::cache::Entry`
Expand Down
1 change: 0 additions & 1 deletion src/bundler/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ bun_dispatch::link_interface! {
fn loaders() -> *const bun_collections::StringArrayHashMap<bun_ast::Loader>;
fn eval_source() -> Option<*const bun_ast::Source>;
fn main() -> &'static [u8];
fn read_dir_info_package_json(dir: &[u8]) -> Option<*const bun_resolver::PackageJSON>;
fn is_blob_url(specifier: &[u8]) -> bool;
fn resolve_blob(specifier: &[u8]) -> Option<options::OpaqueBlob>;
fn blob_loader(blob: options::OpaqueBlob) -> Option<bun_ast::Loader>;
Expand Down
20 changes: 1 addition & 19 deletions src/bundler/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bun_js_parser::parser::Runtime;
use bun_options_types::schema::api;
use bun_resolver::fs as Fs;
use bun_resolver::fs::PathResolverExt as _;
use bun_resolver::package_json::{MacroMap as MacroRemap, PackageJSON};
use bun_resolver::package_json::MacroMap as MacroRemap;
use enum_map::EnumMap;
use std::borrow::Cow;

Expand Down Expand Up @@ -468,9 +468,6 @@ pub struct LoaderResult<'a> {
pub path: Fs::Path<'a>,
pub is_main: bool,
pub specifier: &'a [u8],
/// NOTE: This is always `null` for non-js-like loaders since it's not
/// needed for them.
pub package_json: Option<&'a PackageJSON>,
}

pub fn get_loader_and_virtual_source<'a>(
Expand Down Expand Up @@ -557,27 +554,12 @@ pub fn get_loader_and_virtual_source<'a>(

let is_main = strings::eql_long(specifier, jsc_vm.main(), true);

let dir = path.name().dir;
// NOTE: we cannot trust `path.isFile()` since it's not always correct
// NOTE: assume we may need a package.json when no loader is specified
let is_js_like = loader.map(|l| l.is_js_like()).unwrap_or(true);
let package_json: Option<&PackageJSON> = if is_js_like && bun_paths::is_absolute(dir) {
jsc_vm
.read_dir_info_package_json(dir)
// SAFETY: the vtable returns a pointer into the resolver's DirInfo
// cache owned by `jsc_vm.owner`, which outlives `'a`.
.map(|p| unsafe { &*p })
} else {
None
};

Ok(LoaderResult {
loader,
virtual_source,
path,
is_main,
specifier,
package_json,
})
}

Expand Down
4 changes: 4 additions & 0 deletions src/bundler/transpiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,7 @@ pub struct ParseOptions<'a, 'b> {
pub arena: &'a Arena,
pub dirname_fd: FD,
pub file_descriptor: Option<FD>,
pub non_regular_file: resolver::cache::NonRegularFile,

/// On exception, we might still want to watch the file.
pub file_fd_ptr: Option<&'b mut FD>,
Expand Down Expand Up @@ -1481,6 +1482,7 @@ impl<'a> Transpiler<'a> {
USE_SHARED_BUFFER,
file_descriptor,
if USE_SHARED_BUFFER { None } else { Some(arena) },
this_parse.non_regular_file,
) {
Ok(e) => e,
Err(err) => {
Expand Down Expand Up @@ -2966,6 +2968,7 @@ impl<'a> Transpiler<'a> {
loader,
dirname_fd,
file_descriptor: None,
non_regular_file: resolver::cache::NonRegularFile::Read,
file_fd_ptr: None,
macro_remappings,
macro_js_ctx: default_macro_js_value(),
Expand Down Expand Up @@ -3139,6 +3142,7 @@ impl<'a> Transpiler<'a> {
false,
None,
None,
resolver::cache::NonRegularFile::Read,
) {
Ok(e) => e,
Err(err) => {
Expand Down
1 change: 1 addition & 0 deletions src/jsc/RuntimeTranspilerStore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ impl TranspilerJob {
loader,
dirname_fd: Fd::INVALID,
file_descriptor: None,
non_regular_file: bun_resolver::cache::NonRegularFile::Read,
// SAFETY: `input_file_fd` is a stack local declared above and
// outlives `parse_options`; `addr_of_mut!` avoids forming an
// intermediate `&mut` so the close-guard's later borrow stays sound.
Expand Down
10 changes: 4 additions & 6 deletions src/jsc/VirtualMachine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4425,11 +4425,6 @@ impl VirtualMachine {
Ok(lr) => lr,
Err(_) => return Err(crate::CrateError::ModuleNotFound),
};
let module_type = lr
.package_json
.map(|pkg| pkg.module_type)
.unwrap_or(bun_bundler::options::ModuleType::Unknown);

// A drop-guard so both the normal and error paths reset the arena on
// the right edge.
struct ArenaReset<'a>(&'a mut VirtualMachine, bool);
Expand Down Expand Up @@ -4463,7 +4458,10 @@ impl VirtualMachine {
} else {
bun_ast::Loader::File
}),
module_type,
// The one caller fetches with `PrintSource`, which returns the
// file as it is. A `package.json` lookup for the module type would
// scan a directory the error printer has no other reason to touch.
Comment thread
robobun marked this conversation as resolved.
Resolved
module_type: bun_bundler::options::ModuleType::Unknown,
source_code_printer: printer.as_ptr(),
// `fetchWithoutOnLoadPlugins` forbids the async path.
promise_ptr: core::ptr::null_mut(),
Expand Down
45 changes: 34 additions & 11 deletions src/resolver/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,19 @@ pub mod cache {
}
}

/// What [`Fs::read_file_with_allocator`] does with a path that is not a
/// regular file.
Comment thread
robobun marked this conversation as resolved.
Resolved
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum NonRegularFile {
/// Open and read it like a file. A FIFO blocks until it has a writer.
Read,
/// Fail (`EISDIR` for a directory, `ENODEV` otherwise) without
/// blocking on it or reading a byte from it. For a read that happens
/// after the fact, such as the error printer's re-read of a module
/// that has already run.
Comment thread
robobun marked this conversation as resolved.
Resolved
Reject,
}

/// File-read cache: shared read buffers plus flags controlling buffer
/// reuse and streaming reads.
pub struct Fs {
Expand Down Expand Up @@ -2329,27 +2342,31 @@ pub mod cache {
use_shared_buffer: bool,
_file_handle: Option<Fd>,
arena: Option<&bun_alloc::Arena>,
non_regular_file: NonRegularFile,
) -> crate::CrateResult<Entry> {
let rfs = &_fs.fs;

let will_close = rfs.need_to_close_files() && _file_handle.is_none();

let open_at = |dir: Fd, path: &[u8], flags: i32| match non_regular_file {
NonRegularFile::Read => bun_sys::File::openat(dir, path, flags, 0),
NonRegularFile::Reject => {
bun_sys::File::open_regular_at(dir, path).map(|(file, _size)| file)
}
};
let open_path =
|path: &[u8]| open_at(Fd::cwd(), path, bun_sys::O::RDONLY | bun_sys::O::CLOEXEC);

// A single let-expression avoids `mem::zeroed()` on a
// type that may have niche (NonZero) fields.
let file_handle: bun_sys::File = if let Some(f) = _file_handle {
bun_sys::lseek(f, 0, libc::SEEK_SET).map_err(crate::Error::from)?;
bun_sys::File::from_fd(f)
} else if feature_flags::STORE_FILE_DESCRIPTORS && dirname_fd.is_valid() {
match bun_sys::openat_a(
dirname_fd,
bun_paths::basename(path),
bun_sys::O::RDONLY,
0,
) {
Ok(fd) => bun_sys::File::from_fd(fd),
match open_at(dirname_fd, bun_paths::basename(path), bun_sys::O::RDONLY) {
Ok(file) => file,
Err(err) if err.get_errno() == bun_sys::E::ENOENT => {
let handle = bun_sys::open_file(path, bun_sys::OpenFlags::READ_ONLY)
.map_err(crate::Error::from)?;
let handle = open_path(path).map_err(crate::Error::from)?;
bun_core::pretty_errorln!(
"<r><d>Internal error: directory mismatch for directory \"{}\", fd {}<r>. You don't need to do anything, but this indicates a bug.",
bstr::BStr::new(path),
Expand All @@ -2360,8 +2377,7 @@ pub mod cache {
Err(err) => return Err(err.into()),
}
} else {
bun_sys::open_file(path, bun_sys::OpenFlags::READ_ONLY)
.map_err(crate::Error::from)?
open_path(path).map_err(crate::Error::from)?
};

let mut owned: Option<bun_sys::File> = None;
Expand All @@ -2374,6 +2390,13 @@ pub mod cache {
};
let file_handle = bun_sys::File::borrow(&fd);

// A caller's handle did not go through `open_regular_at`.
if _file_handle.is_some() && non_regular_file == NonRegularFile::Reject {
file_handle
.ensure_regular(path)
.map_err(crate::Error::from)?;
}

#[cfg(not(windows))] // skip on Windows because NTCreateFile will do it.
bun_core::scoped_log!(
CacheFs,
Expand Down
1 change: 1 addition & 0 deletions src/resolver/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ impl PackageJSON {
false,
None,
None,
crate::cache::NonRegularFile::Read,
) {
Ok(e) => e,
Err(err) => {
Expand Down
1 change: 1 addition & 0 deletions src/resolver/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3988,6 +3988,7 @@ impl<'a> Resolver<'a> {
false,
None,
None,
crate::cache::NonRegularFile::Read,
)?;
// NOTE: reshaped for borrowck — `mem::take` the contents (leaving
// `Contents::Empty` behind) so `entry` stays whole for the close-guard.
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/api/JSTranspiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,7 @@ impl TransformTask {
macro_remappings: clone_macro_map(&self.macro_map),
dirname_fd: bun_sys::Fd::INVALID,
file_descriptor: None,
non_regular_file: bun_resolver::cache::NonRegularFile::Read,
loader: self.loader,
jsx,
path: source.path,
Expand Down Expand Up @@ -1226,6 +1227,7 @@ impl JSTranspiler {
macro_remappings: clone_macro_map(&config.macro_map),
dirname_fd: bun_sys::Fd::INVALID,
file_descriptor: None,
non_regular_file: bun_resolver::cache::NonRegularFile::Read,
loader: loader.unwrap_or(config.default_loader),
jsx,
path: source.path,
Expand Down
34 changes: 12 additions & 22 deletions src/runtime/jsc_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,6 @@ mod vm_loader_ctx {
use super::*;
use crate::webcore::Blob;
use bun_bundler::options::OpaqueBlob;
use bun_resolver::package_json::PackageJSON;

/// Recover an [`OpaqueBlob`] as a shared `&Blob` (live until `blob_deinit`).
///
Expand All @@ -1447,12 +1446,9 @@ mod vm_loader_ctx {

// `this: *mut VirtualMachine`. Bodies use raw place projections —
// `(*this).field` — so no `&VirtualMachine` retag is materialized for the
// simple field reads. This matters because `read_dir_info_package_json`
// holds a live `&mut transpiler.resolver` across a re-entrant `read_dir_info`
// that can call back into these hooks; a `&VirtualMachine` formed here would
// alias that `&mut` (SB/TB UB). The two accessors that call `&self` methods
// simple field reads. The two accessors that call `&self` methods
// (`main`, `blob_loader`) form a transient `&VirtualMachine` scoped to the
// single call, which never spans the re-entrant path.
// single call.
bun_bundler::link_impl_VmLoaderCtx! {
Runtime for extern VirtualMachine => |this| {
origin_host() => (*this).origin.host,
Expand All @@ -1464,19 +1460,6 @@ mod vm_loader_ctx {
.as_deref()
.map(core::ptr::from_ref::<bun_ast::Source>),
main() => &*core::ptr::from_ref::<[u8]>((*this).main()),
read_dir_info_package_json(dir) => {
// Short-lived `&mut Resolver` (not `&mut VirtualMachine`) for
// the call — narrows the borrow re-entrant JS could alias.
match (*this).transpiler.resolver.read_dir_info(dir) {
Ok(Some(dir_info)) => {
dir_info
.package_json()
.or(dir_info.enclosing_package_json)
.map(core::ptr::from_ref::<PackageJSON>)
}
_ => None,
}
},
is_blob_url(spec) => crate::webcore::object_url_registry::is_blob_url(spec),
resolve_blob(spec) => {
crate::webcore::object_url_registry::ObjectURLRegistry::singleton()
Expand Down Expand Up @@ -2621,6 +2604,13 @@ fn transpile_source_code_inner(
loader,
dirname_fd: bun_sys::Fd::INVALID,
file_descriptor: None,
// The error printer must not block on, or read from, what is
// no longer the regular file the loader read.
Comment thread
robobun marked this conversation as resolved.
Resolved
non_regular_file: if args.flags == FetchFlags::PrintSource {
bun_resolver::cache::NonRegularFile::Reject
} else {
bun_resolver::cache::NonRegularFile::Read
},
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
// SAFETY: `input_file_fd_ptr` points at this frame's
// `input_file_fd`; reborrow through the raw pointer so the
// `_fd_guard` scopeguard's tag is not invalidated by a
Expand Down Expand Up @@ -2702,11 +2692,11 @@ fn transpile_source_code_inner(
hash,
package_json,
);
// Node compile cache: record the failed module so exit-time
// persist logs the "was not initialized" skip (Node parity).
Comment thread
robobun marked this conversation as resolved.
Resolved
note_compile_cache_parse_failure(path, loader, module_type);
}
arena_guard.2 = false; // give_back_arena = false
// Node compile cache: record the failed module so exit-time
// persist logs the "was not initialized" skip (Node parity).
note_compile_cache_parse_failure(path, loader, module_type);
return Err(crate::Error::ParseError);
};

Expand Down
2 changes: 1 addition & 1 deletion src/sourcemap/Mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ impl Lookup {
let normalized = bun_paths::resolve_path::join_abs_string_buf_z::<
bun_paths::platform::Loose,
>(dir, &mut buf, &[name]);
match bun_sys::File::read_from(bun_sys::Fd::cwd(), normalized) {
match bun_sys::File::read_regular_from(bun_sys::Fd::cwd(), normalized) {
Ok(r) => break 'bytes r,
Err(_) => return None,
}
Expand Down
6 changes: 3 additions & 3 deletions src/sourcemap/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,9 @@ pub(crate) fn get_source_map_impl<P: SourceProvider + ?Sized>(
let load_path =
bun_core::ZStr::from_buf(&load_path_buf[..], source_filename.len() + 4);

// `bun_sys::File::read_from` returns an owned `Vec<u8>`,
// freed on scope exit by `Vec`'s Drop.
let data = match bun_sys::File::read_from(bun_core::Fd::cwd(), load_path) {
// Reached from `error.stack` and the error printer: a FIFO
// here must not block them.
Comment thread
robobun marked this conversation as resolved.
Resolved
let data = match bun_sys::File::read_regular_from(bun_core::Fd::cwd(), load_path) {
Ok(data) => data,
Err(_) => break 'try_external,
};
Expand Down
27 changes: 27 additions & 0 deletions src/sys/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,33 @@ impl File {
}

// ── one-shot path helpers (open + io + close) ───────────────────────
/// Open `path` for reading and return it with its size; `EISDIR` for a directory, `ENODEV` for any other non-regular file.
pub fn open_regular_at(dir: impl AsFd, path: &[u8]) -> Maybe<(Self, u64)> {
Comment thread
robobun marked this conversation as resolved.
Resolved
let dir = dir.as_fd();
// On Windows `O_NONBLOCK` would make the handle overlapped; the fstat still rejects there.
#[cfg(unix)]
let flags = O::RDONLY | O::CLOEXEC | O::NONBLOCK;
#[cfg(not(unix))]
let flags = O::RDONLY | O::CLOEXEC;
let file = Self::openat(dir, path, flags, 0)?;
let size = file.ensure_regular(path)?;
Ok((file, size))
Comment thread
robobun marked this conversation as resolved.
Resolved
}
/// The check of [`File::open_regular_at`] for a file opened with other flags: the size of a regular file, else its error.
pub fn ensure_regular(&self, path: &[u8]) -> Maybe<u64> {
let st = self.stat().map_err(|e| e.with_path(path))?;
let mode = st.st_mode as Mode;
if !S::ISREG(mode) {
let errno = if S::ISDIR(mode) { E::EISDIR } else { E::ENODEV };
return Err(Error::new(errno, Tag::open).with_path(path));
}
Ok(st.st_size.max(0) as u64)
}
/// [`File::read_from`] of a regular file only ([`File::open_regular_at`]).
pub fn read_regular_from(dir: impl AsFd, path: &[u8]) -> Maybe<Vec<u8>> {
let (file, _size) = Self::open_regular_at(dir, path)?;
file.read_to_end()
}
/// Open + read + close. Accepts `&[u8]`; `&ZStr` callers deref-coerce.
pub fn read_from(dir: impl AsFd, path: &[u8]) -> Maybe<Vec<u8>> {
let dir = dir.as_fd();
Expand Down
Loading
Loading