diff options
author | Satya Rohith <me@satyarohith.com> | 2021-04-21 21:22:00 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-21 17:52:00 +0200 |
commit | 3b78f6c4493093701660bba496d87342ffbc08d7 (patch) | |
tree | 8c7a52188f8ead12cab1078f5e5cb4ae62387ba2 | |
parent | 320c19c7c09f18af7647f2a278dd8c3e18bffba4 (diff) |
fix: do not panic on not found cwd (#10238)
-rw-r--r-- | cli/fs_util.rs | 4 | ||||
-rw-r--r-- | cli/tests/dont_panic_not_found_cwd.ts | 3 | ||||
-rw-r--r-- | cli/tests/integration_tests.rs | 19 | ||||
-rw-r--r-- | cli/tools/installer.rs | 4 | ||||
-rw-r--r-- | runtime/fs_util.rs | 4 | ||||
-rw-r--r-- | runtime/ops/runtime.rs | 5 | ||||
-rw-r--r-- | runtime/web_worker.rs | 5 | ||||
-rw-r--r-- | runtime/worker.rs | 5 |
8 files changed, 43 insertions, 6 deletions
diff --git a/cli/fs_util.rs b/cli/fs_util.rs index 584d62598..a862e4bd3 100644 --- a/cli/fs_util.rs +++ b/cli/fs_util.rs @@ -1,6 +1,7 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use deno_core::error::AnyError; +use deno_core::error::Context; pub use deno_core::normalize_path; use deno_runtime::deno_crypto::rand; use std::env::current_dir; @@ -81,7 +82,8 @@ pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, AnyError> { let resolved_path = if path.is_absolute() { path.to_owned() } else { - let cwd = current_dir().unwrap(); + let cwd = + current_dir().context("Failed to get current working directory")?; cwd.join(path) }; diff --git a/cli/tests/dont_panic_not_found_cwd.ts b/cli/tests/dont_panic_not_found_cwd.ts new file mode 100644 index 000000000..e5ae1dfa4 --- /dev/null +++ b/cli/tests/dont_panic_not_found_cwd.ts @@ -0,0 +1,3 @@ +const dir = Deno.makeTempDirSync(); +Deno.chdir(dir); +Deno.removeSync(dir); diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index d178512c5..28d2dc7c6 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -5144,6 +5144,25 @@ console.log("finish"); assert!(stderr.contains("BadResource")); } + #[cfg(not(windows))] + #[test] + fn should_not_panic_on_not_found_cwd() { + let output = util::deno_cmd() + .current_dir(util::root_path()) + .arg("run") + .arg("--allow-write") + .arg("--allow-read") + .arg("cli/tests/dont_panic_not_found_cwd.ts") + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap() + .wait_with_output() + .unwrap(); + assert!(!output.status.success()); + let stderr = std::str::from_utf8(&output.stderr).unwrap().trim(); + assert!(stderr.contains("Failed to get current working directory")); + } + #[cfg(windows)] // Clippy suggests to remove the `NoStd` prefix from all variants. I disagree. #[allow(clippy::enum_variant_names)] diff --git a/cli/tools/installer.rs b/cli/tools/installer.rs index 747d9cbc0..48e3b8fdd 100644 --- a/cli/tools/installer.rs +++ b/cli/tools/installer.rs @@ -3,6 +3,7 @@ use crate::flags::Flags; use crate::fs_util::canonicalize_path; use deno_core::error::generic_error; use deno_core::error::AnyError; +use deno_core::error::Context; use deno_core::url::Url; use log::Level; use regex::Regex; @@ -175,7 +176,8 @@ pub fn install( let module_path = if module_path.is_absolute() { module_path } else { - let cwd = env::current_dir().unwrap(); + let cwd = env::current_dir() + .context("Failed to get current working directory")?; cwd.join(module_path) }; Url::from_file_path(module_path).expect("Path should be absolute") diff --git a/runtime/fs_util.rs b/runtime/fs_util.rs index bf0735205..93286ecf3 100644 --- a/runtime/fs_util.rs +++ b/runtime/fs_util.rs @@ -1,6 +1,7 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use deno_core::error::AnyError; +use deno_core::error::Context; pub use deno_core::normalize_path; use std::env::current_dir; use std::io::Error; @@ -24,7 +25,8 @@ pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, AnyError> { let resolved_path = if path.is_absolute() { path.to_owned() } else { - let cwd = current_dir().unwrap(); + let cwd = + current_dir().context("Failed to get current working directory")?; cwd.join(path) }; diff --git a/runtime/ops/runtime.rs b/runtime/ops/runtime.rs index d90e92593..a02bf4548 100644 --- a/runtime/ops/runtime.rs +++ b/runtime/ops/runtime.rs @@ -5,6 +5,7 @@ use crate::metrics::RuntimeMetrics; use crate::ops::UnstableChecker; use crate::permissions::Permissions; use deno_core::error::AnyError; +use deno_core::error::Context; use deno_core::serde_json::json; use deno_core::serde_json::Value; use deno_core::ModuleSpecifier; @@ -29,7 +30,9 @@ fn op_main_module( let main = state.borrow::<ModuleSpecifier>().to_string(); let main_url = deno_core::resolve_url_or_path(&main)?; if main_url.scheme() == "file" { - let main_path = std::env::current_dir().unwrap().join(main_url.to_string()); + let main_path = std::env::current_dir() + .context("Failed to get current working directory")? + .join(main_url.to_string()); state .borrow_mut::<Permissions>() .read diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 57c3909e1..cdc3d7e3d 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -8,6 +8,7 @@ use crate::ops; use crate::permissions::Permissions; use crate::tokio_util::create_basic_runtime; use deno_core::error::AnyError; +use deno_core::error::Context as ErrorContext; use deno_core::futures::channel::mpsc; use deno_core::futures::future::poll_fn; use deno_core::futures::future::FutureExt; @@ -320,7 +321,9 @@ impl WebWorker { /// Same as execute2() but the filename defaults to "$CWD/__anonymous__". pub fn execute(&mut self, js_source: &str) -> Result<(), AnyError> { - let path = env::current_dir().unwrap().join("__anonymous__"); + let path = env::current_dir() + .context("Failed to get current working directory")? + .join("__anonymous__"); let url = Url::from_file_path(path).unwrap(); self.js_runtime.execute(url.as_str(), js_source) } diff --git a/runtime/worker.rs b/runtime/worker.rs index 253725533..6de87f52e 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -8,6 +8,7 @@ use crate::metrics::RuntimeMetrics; use crate::ops; use crate::permissions::Permissions; use deno_core::error::AnyError; +use deno_core::error::Context as ErrorContext; use deno_core::futures::future::poll_fn; use deno_core::futures::future::FutureExt; use deno_core::futures::stream::StreamExt; @@ -201,7 +202,9 @@ impl MainWorker { /// Same as execute2() but the filename defaults to "$CWD/__anonymous__". pub fn execute(&mut self, js_source: &str) -> Result<(), AnyError> { - let path = env::current_dir().unwrap().join("__anonymous__"); + let path = env::current_dir() + .context("Failed to get current working directory")? + .join("__anonymous__"); let url = Url::from_file_path(path).unwrap(); self.js_runtime.execute(url.as_str(), js_source) } |