Skip to content

Commit 0b530c3

Browse files
committed
Add target directory parameter: address suggestions
1 parent dd0b7a2 commit 0b530c3

File tree

5 files changed

+25
-27
lines changed

5 files changed

+25
-27
lines changed

src/cargo/core/workspace.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'cfg> Workspace<'cfg> {
131131
/// root and all member packages. It will then validate the workspace
132132
/// before returning it, so `Ok` is only returned for valid workspaces.
133133
pub fn new(manifest_path: &Path, config: &'cfg Config) -> CargoResult<Workspace<'cfg>> {
134-
let target_dir = config.target_dir()?;
134+
let target_dir = config.target_dir();
135135

136136
let mut ws = Workspace {
137137
config,
@@ -191,7 +191,7 @@ impl<'cfg> Workspace<'cfg> {
191191
ws.target_dir = if let Some(dir) = target_dir {
192192
Some(dir)
193193
} else {
194-
ws.config.target_dir()?
194+
ws.config.target_dir()
195195
};
196196
ws.members.push(ws.current_manifest.clone());
197197
ws.default_members.push(ws.current_manifest.clone());

src/cargo/ops/cargo_install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ fn install_one(
213213
let mut needs_cleanup = false;
214214
let overidden_target_dir = if source_id.is_path() {
215215
None
216-
} else if let Some(dir) = config.target_dir()? {
216+
} else if let Some(dir) = config.target_dir() {
217217
Some(dir)
218218
} else if let Ok(td) = TempFileBuilder::new().prefix("cargo-install").tempdir() {
219219
let p = td.path().to_owned();

src/cargo/util/config.rs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ pub struct Config {
7070
cache_rustc_info: bool,
7171
/// Creation time of this config, used to output the total build time
7272
creation_time: Instant,
73-
/// Target Directory via resolved Cli parameter
74-
cli_target_dir: Option<Filesystem>,
73+
/// Target Directory via resolved Cli parameter
74+
target_dir: Option<Filesystem>,
7575
}
7676

7777
impl Config {
@@ -115,7 +115,7 @@ impl Config {
115115
crates_io_source_id: LazyCell::new(),
116116
cache_rustc_info,
117117
creation_time: Instant::now(),
118-
cli_target_dir: None,
118+
target_dir: None,
119119
}
120120
}
121121

@@ -242,17 +242,8 @@ impl Config {
242242
&self.cwd
243243
}
244244

245-
pub fn target_dir(&self) -> CargoResult<Option<Filesystem>> {
246-
if let Some(ref dir) = self.cli_target_dir {
247-
Ok(Some(dir.clone()))
248-
} else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
249-
Ok(Some(Filesystem::new(self.cwd.join(dir))))
250-
} else if let Some(val) = self.get_path("build.target-dir")? {
251-
let val = self.cwd.join(val.val);
252-
Ok(Some(Filesystem::new(val)))
253-
} else {
254-
Ok(None)
255-
}
245+
pub fn target_dir(&self) -> Option<Filesystem> {
246+
self.target_dir.clone()
256247
}
257248

258249
fn get(&self, key: &str) -> CargoResult<Option<ConfigValue>> {
@@ -500,17 +491,23 @@ impl Config {
500491
| (None, None, None) => Verbosity::Normal,
501492
};
502493

503-
let cli_target_dir = match target_dir.as_ref() {
504-
Some(dir) => Some(Filesystem::new(dir.clone())),
505-
None => None,
494+
let target_dir = if let Some(dir) = target_dir.as_ref() {
495+
Some(Filesystem::new(self.cwd.join(dir)))
496+
} else if let Some(dir) = env::var_os("CARGO_TARGET_DIR") {
497+
Some(Filesystem::new(self.cwd.join(dir)))
498+
} else if let Ok(Some(val)) = self.get_path("build.target-dir") {
499+
let val = self.cwd.join(val.val);
500+
Some(Filesystem::new(val))
501+
} else {
502+
None
506503
};
507504

508505
self.shell().set_verbosity(verbosity);
509506
self.shell().set_color_choice(color.map(|s| &s[..]))?;
510507
self.extra_verbose = extra_verbose;
511508
self.frozen = frozen;
512509
self.locked = locked;
513-
self.cli_target_dir = cli_target_dir;
510+
self.target_dir = target_dir;
514511
self.cli_flags.parse(unstable_flags)?;
515512

516513
Ok(())

tests/testsuite/bad_config.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,10 @@ fn invalid_global_config() {
284284
p.cargo("build").arg("-v"),
285285
execs().with_status(101).with_stderr(
286286
"\
287-
[ERROR] Couldn't load Cargo configuration
287+
error: failed to parse manifest at `[..]`
288+
289+
Caused by:
290+
Couldn't load Cargo configuration
288291
289292
Caused by:
290293
could not parse TOML configuration in `[..]`

tests/testsuite/build.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3688,7 +3688,7 @@ fn custom_target_dir_line_parameter() {
36883688
let exe_name = format!("foo{}", env::consts::EXE_SUFFIX);
36893689

36903690
assert_that(
3691-
p.cargo("build").arg("--target-dir").arg("foo/target"),
3691+
p.cargo("build --target-dir foo/target"),
36923692
execs().with_status(0),
36933693
);
36943694
assert_that(
@@ -3721,7 +3721,7 @@ fn custom_target_dir_line_parameter() {
37213721
)
37223722
.unwrap();
37233723
assert_that(
3724-
p.cargo("build").arg("--target-dir").arg("bar/target"),
3724+
p.cargo("build --target-dir bar/target"),
37253725
execs().with_status(0),
37263726
);
37273727
assert_that(
@@ -3738,9 +3738,7 @@ fn custom_target_dir_line_parameter() {
37383738
);
37393739

37403740
assert_that(
3741-
p.cargo("build")
3742-
.arg("--target-dir")
3743-
.arg("foobar/target")
3741+
p.cargo("build --target-dir foobar/target")
37443742
.env("CARGO_TARGET_DIR", "bar/target"),
37453743
execs().with_status(0),
37463744
);

0 commit comments

Comments
 (0)