Skip to content

Commit

Permalink
prepend fs_root to environment_for_command
Browse files Browse the repository at this point in the history
Signed-off-by: mwrock <matt@mattwrock.com>
  • Loading branch information
mwrock committed Nov 9, 2018
1 parent 14abfd4 commit 5ac14df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,22 @@ where
pkg_path
}

/// Given a linux style absolute path (prepended with '/') and a fs_root,
/// this will "re-root" the path just under the fs_root. Otherwise returns
/// the given path unchanged. Non-Windows platforms will always return the
/// unchanged path.
pub fn fs_rooted_path<T>(path: &PathBuf, fs_root: Option<T>) -> PathBuf
where
T: AsRef<Path>,
{
match fs_root {
Some(ref root) if path.starts_with("/") && cfg!(windows) => {
Path::new(root.as_ref()).join(path.strip_prefix("/").unwrap())
}
_ => path.to_path_buf(),
}
}

/// Returns the absolute path for a given command, if it exists, by searching the `PATH`
/// environment variable.
///
Expand Down
27 changes: 22 additions & 5 deletions src/package/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,22 @@ impl PackageInstall {
// present for backwards compatibility with older Habitat releases.
env.remove(PATH_KEY);

let path = env::join_paths(self.runtime_paths()?)?
let mut paths = self.runtime_paths()?;

// Let's join the paths to the FS_ROOT
// In most cases, this does nothing and should only mutate
// the paths in a windows studio where FS_ROOT_PATH will
// be the studio root path (ie c:\hab\studios\...)
for path in &mut paths {
*path = fs::fs_rooted_path(&path, Some(&*self.fs_root_path));
}

let joined = env::join_paths(paths)?
.into_string()
.map_err(|s| Error::InvalidPathString(s))?;
// Only insert a PATH entry if the resulting path string is non-empty
if !path.is_empty() {
env.insert(PATH_KEY.to_string(), path);
if !joined.is_empty() {
env.insert(PATH_KEY.to_string(), joined);
}

Ok(env)
Expand Down Expand Up @@ -1420,13 +1430,20 @@ core/bar=pub:core/publish sub:core/subscribe
);

let mut expected = HashMap::new();
let fs_root_path = fs_root.into_path();
expected.insert("FOO".to_string(), "bar".to_string());
expected.insert("JAVA_HOME".to_string(), "/my/java/home".to_string());
expected.insert(
"PATH".to_string(),
env::join_paths(vec![
pkg_prefix_for(&pkg_install).join("bin"),
pkg_prefix_for(&other_pkg_install).join("sbin"),
fs::fs_rooted_path(
&pkg_prefix_for(&pkg_install).join("bin"),
Some(&*fs_root_path),
),
fs::fs_rooted_path(
&pkg_prefix_for(&other_pkg_install).join("sbin"),
Some(&*fs_root_path),
),
]).unwrap()
.to_string_lossy()
.into_owned(),
Expand Down

0 comments on commit 5ac14df

Please sign in to comment.