From 2504e04375a4a8f62f5dc62f95745701521c590e Mon Sep 17 00:00:00 2001 From: Alex Huszagh Date: Tue, 24 May 2022 12:02:13 -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..b9b553255 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +- #713 - convert relative target directories to absolute paths. - #709 - Update Emscripten targets to `emcc` version 3.1.10 - #707, #708 - Set `BINDGEN_EXTRA_CLANG_ARGS` environment variable to pass sysroot to `rust-bindgen` - #696 - bump freebsd to 12.3 diff --git a/src/cli.rs b/src/cli.rs index f62ee82de..df9abd2a4 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!(