Skip to content

Commit ef810f6

Browse files
committed
fix: Normalize the target path
1 parent 0c8f556 commit ef810f6

File tree

5 files changed

+218
-85
lines changed

5 files changed

+218
-85
lines changed

crates/cargo-util/src/paths.rs

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ pub fn dylib_path() -> Vec<PathBuf> {
7575

7676
/// Normalize a path, removing things like `.` and `..`.
7777
///
78+
/// CAUTION: This expects the path to be absolute.
79+
///
7880
/// CAUTION: This does not resolve symlinks (unlike
7981
/// [`std::fs::canonicalize`]). This may cause incorrect or surprising
8082
/// behavior at times. This should be used carefully. Unfortunately,

src/cargo/ops/vendor.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::ops;
44
use crate::sources::path::PathSource;
55
use crate::sources::CRATES_IO_REGISTRY;
66
use crate::util::cache_lock::CacheLockMode;
7+
use crate::util::toml::AbsolutePathTomlTarget;
78
use crate::util::{try_canonicalize, CargoResult, GlobalContext};
89
use anyhow::{bail, Context as _};
910
use cargo_util::{paths, Sha256};
@@ -412,9 +413,13 @@ fn prepare_for_vendor(
412413
) -> CargoResult<Package> {
413414
let contents = me.manifest().contents();
414415
let document = me.manifest().document();
416+
let abs_targets =
417+
crate::util::toml::absolute_normalized_targets(me.manifest().normalized_toml(), me.root())?;
415418
let original_toml = prepare_toml_for_vendor(
416419
me.manifest().normalized_toml().clone(),
417420
packaged_files,
421+
&abs_targets,
422+
me.root(),
418423
gctx,
419424
)?;
420425
let normalized_toml = original_toml.clone();
@@ -428,10 +433,12 @@ fn prepare_for_vendor(
428433
document.clone(),
429434
original_toml,
430435
normalized_toml,
436+
&abs_targets,
431437
features,
432438
workspace_config,
433439
source_id,
434440
me.manifest_path(),
441+
me.root(),
435442
gctx,
436443
&mut warnings,
437444
&mut errors,
@@ -443,14 +450,19 @@ fn prepare_for_vendor(
443450
fn prepare_toml_for_vendor(
444451
mut me: cargo_util_schemas::manifest::TomlManifest,
445452
packaged_files: &[PathBuf],
453+
abs_targets: &AbsolutePathTomlTarget,
454+
package_root: &Path,
446455
gctx: &GlobalContext,
447456
) -> CargoResult<cargo_util_schemas::manifest::TomlManifest> {
448457
let package = me
449458
.package
450459
.as_mut()
451460
.expect("venedored manifests must have packages");
452-
if let Some(cargo_util_schemas::manifest::StringOrBool::String(path)) = &package.build {
453-
let path = paths::normalize_path(Path::new(path));
461+
if let Some(path) = &abs_targets.build {
462+
let path = path
463+
.strip_prefix(package_root)
464+
.expect("path shoud be under the package root")
465+
.to_path_buf();
454466
let included = packaged_files.contains(&path);
455467
let build = if included {
456468
let path = path
@@ -469,37 +481,42 @@ fn prepare_toml_for_vendor(
469481
package.build = Some(build);
470482
}
471483

472-
let lib = if let Some(target) = &me.lib {
484+
let lib = if let Some(target) = &abs_targets.lib {
473485
crate::util::toml::prepare_target_for_publish(
474486
target,
475487
Some(packaged_files),
488+
package_root,
476489
"library",
477490
gctx,
478491
)?
479492
} else {
480493
None
481494
};
482495
let bin = crate::util::toml::prepare_targets_for_publish(
483-
me.bin.as_ref(),
496+
abs_targets.bin.as_ref(),
484497
Some(packaged_files),
498+
package_root,
485499
"binary",
486500
gctx,
487501
)?;
488502
let example = crate::util::toml::prepare_targets_for_publish(
489-
me.example.as_ref(),
503+
abs_targets.example.as_ref(),
490504
Some(packaged_files),
505+
package_root,
491506
"example",
492507
gctx,
493508
)?;
494509
let test = crate::util::toml::prepare_targets_for_publish(
495-
me.test.as_ref(),
510+
abs_targets.test.as_ref(),
496511
Some(packaged_files),
512+
package_root,
497513
"test",
498514
gctx,
499515
)?;
500516
let bench = crate::util::toml::prepare_targets_for_publish(
501-
me.bench.as_ref(),
517+
abs_targets.bench.as_ref(),
502518
Some(packaged_files),
519+
package_root,
503520
"benchmark",
504521
gctx,
505522
)?;

0 commit comments

Comments
 (0)