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 /cli | |
parent | 320c19c7c09f18af7647f2a278dd8c3e18bffba4 (diff) |
fix: do not panic on not found cwd (#10238)
Diffstat (limited to 'cli')
-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 |
4 files changed, 28 insertions, 2 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") |