Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 7 additions & 6 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ repository = "https://github.com/denoland/deno"

[workspace.dependencies]
deno_ast = { version = "=0.50.3", features = ["transpiling"] }
deno_core = { version = "0.365.0" }
deno_core = { version = "0.366.0" }

deno_cache_dir = "=0.25.0"
deno_doc = "=0.186.0"
Expand Down
16 changes: 14 additions & 2 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ pub struct InternalFlags {

#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Flags {
pub initial_cwd: Option<PathBuf>,
/// Vector of CLI arguments - these are user script arguments, all Deno
/// specific flags are removed.
pub argv: Vec<String>,
Expand Down Expand Up @@ -1392,8 +1393,15 @@ static DENO_HELP: &str = cstr!(
<y>Discord:</> https://discord.gg/deno
");

/// Main entry point for parsing deno's command line flags.
pub fn flags_from_vec(args: Vec<OsString>) -> clap::error::Result<Flags> {
flags_from_vec_with_initial_cwd(args, None)
}

/// Main entry point for parsing deno's command line flags.
pub fn flags_from_vec_with_initial_cwd(
args: Vec<OsString>,
initial_cwd: Option<PathBuf>,
) -> clap::error::Result<Flags> {
let mut app = clap_root();
let mut matches =
app
Expand All @@ -1420,7 +1428,10 @@ pub fn flags_from_vec(args: Vec<OsString>) -> clap::error::Result<Flags> {
_ => e,
})?;

let mut flags = Flags::default();
let mut flags = Flags {
initial_cwd,
..Default::default()
};

// to pass all flags, even help and
if matches.subcommand_matches("deploy").is_some() {
Expand Down Expand Up @@ -1484,6 +1495,7 @@ pub fn flags_from_vec(args: Vec<OsString>) -> clap::error::Result<Flags> {
}

help_parse(&mut flags, subcommand);

return Ok(flags);
}

Expand Down
21 changes: 17 additions & 4 deletions cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,10 +651,16 @@ impl CliFactory {
self.services.workspace_factory.get_or_try_init(|| {
let initial_cwd = match self.overrides.initial_cwd.clone() {
Some(v) => v,
None => self
.sys()
.env_current_dir()
.with_context(|| "Failed getting cwd.")?,
None => {
if let Some(initial_cwd) = self.flags.initial_cwd.clone() {
initial_cwd
} else {
self
.sys()
.env_current_dir()
.with_context(|| "Failed getting cwd.")?
}
}
};
let options = new_workspace_factory_options(&initial_cwd, &self.flags);
let mut factory =
Expand Down Expand Up @@ -1135,6 +1141,9 @@ impl CliFactory {
no_legacy_abort: cli_options.no_legacy_abort(),
startup_snapshot: deno_snapshots::CLI_SNAPSHOT,
enable_raw_imports: cli_options.unstable_raw_imports(),
maybe_initial_cwd: Some(deno_path_util::url_from_directory_path(
cli_options.initial_cwd(),
)?),
})
}

Expand All @@ -1154,11 +1163,15 @@ impl CliFactory {
};
let maybe_coverage_dir = cli_options.coverage_dir();

let initial_cwd =
deno_path_util::url_from_directory_path(cli_options.initial_cwd())?;

Ok(CliMainWorkerOptions {
needs_test_modules: cli_options.sub_command().needs_test(),
create_hmr_runner,
maybe_coverage_dir,
default_npm_caching_strategy: cli_options.default_npm_caching_strategy(),
maybe_initial_cwd: Some(Arc::new(initial_cwd)),
})
}

Expand Down
12 changes: 10 additions & 2 deletions cli/lib/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ pub struct LibMainWorkerOptions {
pub startup_snapshot: Option<&'static [u8]>,
pub serve_port: Option<u16>,
pub serve_host: Option<String>,
pub maybe_initial_cwd: Option<Url>,
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -462,6 +463,7 @@ impl<TSys: DenoLibSys> LibWorkerFactorySharedState<TSys> {
),
permissions: args.permissions,
};
let maybe_initial_cwd = shared.options.maybe_initial_cwd.clone();
let options = WebWorkerOptions {
name: args.name,
main_module: args.main_module.clone(),
Expand Down Expand Up @@ -503,7 +505,9 @@ impl<TSys: DenoLibSys> LibWorkerFactorySharedState<TSys> {
.clone(),
seed: shared.options.seed,
create_web_worker_cb,
format_js_error_fn: Some(Arc::new(format_js_error)),
format_js_error_fn: Some(Arc::new(move |a| {
format_js_error(a, maybe_initial_cwd.as_ref())
})),
worker_type: args.worker_type,
stdio: stdio.clone(),
cache_storage_dir,
Expand Down Expand Up @@ -657,6 +661,8 @@ impl<TSys: DenoLibSys> LibMainWorkerFactory<TSys> {
bundle_provider: shared.bundle_provider.clone(),
};

let maybe_initial_cwd = shared.options.maybe_initial_cwd.clone();

let options = WorkerOptions {
bootstrap: BootstrapOptions {
deno_version: crate::version::DENO_VERSION_INFO.deno.to_string(),
Expand Down Expand Up @@ -694,7 +700,9 @@ impl<TSys: DenoLibSys> LibMainWorkerFactory<TSys> {
.unsafely_ignore_certificate_errors
.clone(),
seed: shared.options.seed,
format_js_error_fn: Some(Arc::new(format_js_error)),
format_js_error_fn: Some(Arc::new(move |e| {
format_js_error(e, maybe_initial_cwd.as_ref())
})),
create_web_worker_cb: shared.create_web_worker_callback(stdio.clone()),
maybe_inspector_server: shared.maybe_inspector_server.clone(),
should_break_on_first_statement: shared.options.inspect_brk,
Expand Down
52 changes: 35 additions & 17 deletions cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use self::util::draw_thread::DrawThread;
use crate::args::CompletionsFlags;
use crate::args::DenoSubcommand;
use crate::args::Flags;
use crate::args::flags_from_vec;
use crate::args::flags_from_vec_with_initial_cwd;
use crate::args::get_default_v8_flags;
use crate::util::display;
use crate::util::v8::get_v8_flags_from_env;
Expand Down Expand Up @@ -558,9 +558,13 @@ fn exit_with_message(message: &str, code: i32) -> ! {
deno_runtime::exit(code);
}

fn exit_for_error(error: AnyError) -> ! {
fn exit_for_error(error: AnyError, initial_cwd: Option<&std::path::Path>) -> ! {
let error_string = match js_error_downcast_ref(&error) {
Some(e) => format_js_error(e),
Some(e) => {
let initial_cwd = initial_cwd
.and_then(|cwd| deno_path_util::url_from_directory_path(cwd).ok());
format_js_error(e, initial_cwd.as_ref())
}
None => format!("{error:?}"),
};

Expand Down Expand Up @@ -615,6 +619,15 @@ pub fn main() {
.unwrap();

let args: Vec<_> = env::args_os().collect();
let initial_cwd =
match std::env::current_dir().with_context(|| "Failed getting cwd.") {
Ok(cwd) => Some(cwd),
Err(err) => {
log::error!("Failed getting cwd: {err}");
None
}
};
let initial_cwd_clone = initial_cwd.clone();
let future = async move {
let roots = LibWorkerFactoryRoots::default();

Expand All @@ -641,7 +654,7 @@ pub fn main() {
// NOTE(lucacasonato): due to new PKU feature introduced in V8 11.6 we need to
// initialize the V8 platform on a parent thread of all threads that will spawn
// V8 isolates.
let flags = resolve_flags_and_init(args).await?;
let flags = resolve_flags_and_init(args, initial_cwd_clone).await?;

if waited_unconfigured_runtime.is_none() {
init_v8(&flags);
Expand All @@ -657,12 +670,13 @@ pub fn main() {

match result {
Ok(exit_code) => deno_runtime::exit(exit_code),
Err(err) => exit_for_error(err),
Err(err) => exit_for_error(err, initial_cwd.as_deref()),
}
}

async fn resolve_flags_and_init(
args: Vec<std::ffi::OsString>,
initial_cwd: Option<std::path::PathBuf>,
) -> Result<Flags, AnyError> {
// this env var is used by clap to enable dynamic completions, it's set by the shell when
// executing deno to get dynamic completions.
Expand All @@ -671,17 +685,18 @@ async fn resolve_flags_and_init(
deno_runtime::exit(0);
}

let mut flags = match flags_from_vec(args) {
Ok(flags) => flags,
Err(err @ clap::Error { .. })
if err.kind() == clap::error::ErrorKind::DisplayVersion =>
{
// Ignore results to avoid BrokenPipe errors.
let _ = err.print();
deno_runtime::exit(0);
}
Err(err) => exit_for_error(AnyError::from(err)),
};
let mut flags =
match flags_from_vec_with_initial_cwd(args, initial_cwd.clone()) {
Ok(flags) => flags,
Err(err @ clap::Error { .. })
if err.kind() == clap::error::ErrorKind::DisplayVersion =>
{
// Ignore results to avoid BrokenPipe errors.
let _ = err.print();
deno_runtime::exit(0);
}
Err(err) => exit_for_error(AnyError::from(err), initial_cwd.as_deref()),
};
// preserve already loaded env variables
if flags.subcommand.watch_flags().is_some() {
WatchEnvTracker::snapshot();
Expand All @@ -706,7 +721,10 @@ async fn resolve_flags_and_init(
// Tunnel sets up env vars and OTEL, so connect before everything else.
if flags.tunnel {
if let Err(err) = initialize_tunnel(&flags).await {
exit_for_error(err.context("Failed to start with tunnel"));
exit_for_error(
err.context("Failed to start with tunnel"),
initial_cwd.as_deref(),
);
}
// SAFETY: We're doing this before any threads are created.
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion cli/rt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn unwrap_or_exit<T>(result: Result<T, AnyError>) -> T {
Ok(value) => value,
Err(error) => {
let error_string = match js_error_downcast_ref(&error) {
Some(js_error) => format_js_error(js_error),
Some(js_error) => format_js_error(js_error, None),
None => format!("{:?}", error),
};

Expand Down
1 change: 1 addition & 0 deletions cli/rt/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,7 @@ pub async fn run(
no_legacy_abort: false,
startup_snapshot: deno_snapshots::CLI_SNAPSHOT,
enable_raw_imports: metadata.unstable_config.raw_imports,
maybe_initial_cwd: None,
};
let worker_factory = LibMainWorkerFactory::new(
Arc::new(BlobStore::default()),
Expand Down
20 changes: 18 additions & 2 deletions cli/tools/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,27 @@ pub async fn compile(
graph
};

let initial_cwd =
deno_path_util::url_from_directory_path(cli_options.initial_cwd())?;

log::info!(
"{} {} to {}",
colors::green("Compile"),
entrypoint,
output_path.display(),
crate::util::path::relative_specifier_path_for_display(
&initial_cwd,
entrypoint
),
{
if let Ok(output_path) = deno_path_util::url_from_file_path(&output_path)
{
crate::util::path::relative_specifier_path_for_display(
&initial_cwd,
&output_path,
)
} else {
output_path.display().to_string()
}
}
);
validate_output_path(&output_path)?;

Expand Down
2 changes: 1 addition & 1 deletion cli/tools/lint/reporters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl LintReporter for PrettyLintReporter {
fn visit_error(&mut self, file_path: &str, err: &AnyError) {
log::error!("Error linting: {file_path}");
let text = match js_error_downcast_ref(err) {
Some(js_error) => format_js_error(js_error),
Some(js_error) => format_js_error(js_error, None),
None => format!("{err:#}"),
};
for line in text.split('\n') {
Expand Down
2 changes: 1 addition & 1 deletion cli/tools/test/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn format_test_error(
if options.hide_stacktraces {
return js_error.exception_message;
}
format_js_error(&js_error)
format_js_error(&js_error, options.initial_cwd.as_ref())
}

pub fn format_sanitizer_diff(
Expand Down
3 changes: 3 additions & 0 deletions cli/tools/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ impl From<&TestDescription> for TestFailureDescription {
#[derive(Debug, Default, Clone, PartialEq)]
pub struct TestFailureFormatOptions {
pub hide_stacktraces: bool,
pub initial_cwd: Option<Url>,
}

#[allow(clippy::derive_partial_eq_without_eq)]
Expand Down Expand Up @@ -605,6 +606,7 @@ fn get_test_reporter(options: &TestSpecifiersOptions) -> Box<dyn TestReporter> {
let parallel = options.concurrent_jobs.get() > 1;
let failure_format_options = TestFailureFormatOptions {
hide_stacktraces: options.hide_stacktraces,
initial_cwd: Some(options.cwd.clone()),
};
let reporter: Box<dyn TestReporter> = match &options.reporter {
TestReporterConfig::Dot => Box::new(DotTestReporter::new(
Expand Down Expand Up @@ -637,6 +639,7 @@ fn get_test_reporter(options: &TestSpecifiersOptions) -> Box<dyn TestReporter> {
junit_path.to_string(),
TestFailureFormatOptions {
hide_stacktraces: options.hide_stacktraces,
initial_cwd: Some(options.cwd.clone()),
},
));
return Box::new(CompoundTestReporter::new(vec![reporter, junit]));
Expand Down
Loading
Loading