Skip to content

Commit

Permalink
Canonicalize all paths from rebar3 build info
Browse files Browse the repository at this point in the history
Summary:
A custom install of OTP may result in paths having `/../` in them for the OTP root dir, and inside app info from the `rebar3 build_info` command.

Make sure that all paths are canonicalized, so that the assumptions within ELP are still valid.

Reviewed By: jcpetruzza

Differential Revision: D61264108

fbshipit-source-id: 24127554ca535e8528dd667a2ade8c6c68f19c72
  • Loading branch information
alanz authored and facebook-github-bot committed Aug 14, 2024
1 parent 71c445b commit 6d28deb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 4 additions & 2 deletions crates/project_model/src/otp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ impl Otp {
}
let path = String::from_utf8(output.stdout)?;
let result: Utf8PathBuf = format!("{}/lib", path).into();
Ok(result)
let result = fs::canonicalize(result)?;
Ok(Utf8PathBuf::from_path_buf(result).expect("Could not create Utf8PathBuf"))
}

pub fn discover(path: Utf8PathBuf) -> (Otp, Vec<ProjectAppData>) {
Expand All @@ -73,8 +74,9 @@ impl Otp {
.filter_map(|entry| {
let entry = entry.ok()?;
let name = entry.file_name();
let path = fs::canonicalize(entry.path()).expect("Could not canonicalize path");
let dir = AbsPathBuf::assert(
Utf8PathBuf::from_path_buf(entry.path()).expect("Could not decode UTF8"),
Utf8PathBuf::from_path_buf(path).expect("Could not convert to Utf8PathBuf"),
);
Some(ProjectAppData::otp_app_data(name.to_str()?, &dir))
})
Expand Down
17 changes: 12 additions & 5 deletions crates/project_model/src/rebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* of this source tree.
*/

use std::convert::TryInto;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
Expand Down Expand Up @@ -249,10 +248,18 @@ fn into_string(term: eetf::Term) -> Result<String> {
}

fn into_abs_path(term: eetf::Term) -> Result<AbsPathBuf> {
match PathBuf::from(into_string(term)?).try_into() {
Ok(abs_path) => Ok(abs_path),
Err(path_buf) => bail!("expected absolute path, got: {:?}", path_buf),
}
let path_buf = PathBuf::from(into_string(term)?);
let path = if fs::metadata(&path_buf).is_ok() {
match fs::canonicalize::<PathBuf>(path_buf) {
Ok(path) => path,
Err(err) => bail!("expected absolute path, got: {:?}", err),
}
} else {
path_buf
};
Ok(AbsPathBuf::assert(
Utf8PathBuf::from_path_buf(path).expect("Could not convert to Utf8PathBuf"),
))
}

fn into_vec(term: eetf::Term) -> Result<Vec<eetf::Term>> {
Expand Down

0 comments on commit 6d28deb

Please sign in to comment.