Skip to content

Commit 31969b4

Browse files
authored
fix(config): don't panic when --config path can't be converted to URL (#34351)
Passing a `file://` URL (or any other path whose parent can't be turned into a file URL on the host platform) to `--config` was panicking inside `resolve_start_dir` at `url_from_directory_path(...).unwrap()`. On Windows, `--config file:///D:/deno.json` joined with the cwd produced a parent path that the URL conversion rejected, and the unwrap turned that into a Deno panic instead of a user-facing error. This swaps the three unwraps in `resolve_start_dir` for `?` so the failure is propagated through the existing `WorkspaceDiscoverError::PathToUrl` variant. Behavior on platforms where the join already produced a valid path (macOS, Linux) is unchanged. Fixes #34308
1 parent 8f1411f commit 31969b4

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

libs/config/workspace/mod.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,18 +1792,15 @@ impl WorkspaceDirectory {
17921792
// so this is ok for now
17931793
let path = &paths[0];
17941794
match sys.fs_is_dir(path) {
1795-
Ok(is_dir) => Ok(
1796-
url_from_directory_path(if is_dir {
1797-
path
1798-
} else {
1799-
path.parent().unwrap()
1800-
})
1801-
.unwrap(),
1802-
),
1795+
Ok(is_dir) => Ok(url_from_directory_path(if is_dir {
1796+
path
1797+
} else {
1798+
path.parent().unwrap()
1799+
})?),
18031800
Err(_err) => {
18041801
// assume the parent is a directory
18051802
match path.parent() {
1806-
Some(parent) => Ok(url_from_directory_path(parent).unwrap()),
1803+
Some(parent) => Ok(url_from_directory_path(parent)?),
18071804
None => Err(
18081805
WorkspaceDiscoverErrorKind::FailedResolvingStartDirectory(
18091806
FailedResolvingStartDirectoryError::CouldNotResolvePath(
@@ -1825,7 +1822,7 @@ impl WorkspaceDirectory {
18251822
),
18261823
)
18271824
})?;
1828-
Ok(url_from_directory_path(parent).unwrap())
1825+
Ok(url_from_directory_path(parent)?)
18291826
}
18301827
}
18311828
}
@@ -5579,6 +5576,26 @@ pub mod test {
55795576
);
55805577
}
55815578

5579+
#[test]
5580+
fn test_config_file_path_not_convertible_to_url() {
5581+
// Regression test for https://github.com/denoland/deno/issues/34308 -
5582+
// passing a path whose parent can't be converted to a `file://` URL
5583+
// (e.g. a `file:///D:/deno.json` argument joined with cwd on Windows)
5584+
// used to panic. It should return an error instead.
5585+
let sys = InMemorySys::default();
5586+
let path = PathBuf::from("deno.json"); // relative path; parent is ""
5587+
let err = WorkspaceDirectory::discover(
5588+
&sys,
5589+
WorkspaceDiscoverStart::ConfigFile(&path),
5590+
&WorkspaceDiscoverOptions::default(),
5591+
)
5592+
.unwrap_err();
5593+
assert!(matches!(
5594+
err.as_kind(),
5595+
WorkspaceDiscoverErrorKind::PathToUrl(_)
5596+
));
5597+
}
5598+
55825599
#[test]
55835600
fn test_config_workspace() {
55845601
let sys = InMemorySys::default();

0 commit comments

Comments
 (0)