From e647e637762e4f53c237e9576cdc8f4bb0d1f836 Mon Sep 17 00:00:00 2001 From: Alex Huszagh Date: Tue, 24 May 2022 11:04:36 -0500 Subject: [PATCH] Convert --target-dir to use absolute paths. Converts relative target directories to absolute paths, to avoid creating the target directory in the sysroot. Fixes #581. --- CHANGELOG.md | 1 + src/cli.rs | 20 +++++++++++++++----- src/main.rs | 2 +- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3571cdf76..240d98538 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #650 - Improve Docker caching. - #609 - Switch to Github Actions and GHCR. - #588 - fix ci: bump openssl version in freebsd again +- #581 - convert relative target directories to absolute paths. - #552 - Added CHANGELOG.md automation - #543 - Added environment variables to control the UID and GID in the container - #534 - fix image builds with update of dependencies diff --git a/src/cli.rs b/src/cli.rs index f62ee82de..534d99ac4 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -2,6 +2,7 @@ use std::str::FromStr; use std::{env, path::PathBuf}; use crate::cargo::Subcommand; +use crate::errors::Result; use crate::rustc::TargetList; use crate::Target; @@ -15,7 +16,16 @@ pub struct Args { pub docker_in_docker: bool, } -pub fn parse(target_list: &TargetList) -> Args { +// Fix for issue 581. target_dir must be absolute. +fn absolute_path(path: PathBuf) -> Result { + Ok(if path.is_absolute() { + path + } else { + env::current_dir()?.join(path) + }) +} + +pub fn parse(target_list: &TargetList) -> Result { let mut channel = None; let mut target = None; let mut target_dir = None; @@ -44,12 +54,12 @@ pub fn parse(target_list: &TargetList) -> Args { } else if arg == "--target-dir" { all.push(arg); if let Some(td) = args.next() { - target_dir = Some(PathBuf::from(&td)); + target_dir = Some(absolute_path(PathBuf::from(&td))?); all.push("/target".to_string()); } } else if arg.starts_with("--target-dir=") { if let Some((_, td)) = arg.split_once('=') { - target_dir = Some(PathBuf::from(&td)); + target_dir = Some(absolute_path(PathBuf::from(&td))?); all.push("--target-dir=/target".into()); } } else { @@ -66,12 +76,12 @@ pub fn parse(target_list: &TargetList) -> Args { .map(|s| bool::from_str(&s).unwrap_or_default()) .unwrap_or_default(); - Args { + Ok(Args { all, subcommand: sc, channel, target, target_dir, docker_in_docker, - } + }) } diff --git a/src/main.rs b/src/main.rs index e80bbc519..d72c1c469 100644 --- a/src/main.rs +++ b/src/main.rs @@ -263,7 +263,7 @@ pub fn main() -> Result<()> { fn run() -> Result { let target_list = rustc::target_list(false)?; - let args = cli::parse(&target_list); + let args = cli::parse(&target_list)?; if args.all.iter().any(|a| a == "--version" || a == "-V") && args.subcommand.is_none() { println!(