Skip to content

Commit

Permalink
fix: Apply path::normalize_path to targets in toml::normalize_toml …
Browse files Browse the repository at this point in the history
…phase
  • Loading branch information
linyihai committed Nov 6, 2024
1 parent f8df39b commit a456c22
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::core::summary::MissingDependencyError;
use crate::AlreadyPrintedError;
use anyhow::{anyhow, bail, Context as _};
use cargo_platform::Platform;
use cargo_util::paths::{self, normalize_path};
use cargo_util::paths;
use cargo_util_schemas::manifest::{
self, PackageName, PathBaseName, TomlDependency, TomlDetailedDependency, TomlManifest,
};
Expand Down Expand Up @@ -2712,7 +2712,7 @@ fn prepare_toml_for_publish(
let mut package = me.package().unwrap().clone();
package.workspace = None;
if let Some(StringOrBool::String(path)) = &package.build {
let path = paths::normalize_path(Path::new(path));
let path = Path::new(path).to_path_buf();
let included = packaged_files.map(|i| i.contains(&path)).unwrap_or(true);
let build = if included {
let path = path
Expand Down Expand Up @@ -3017,7 +3017,7 @@ pub fn prepare_target_for_publish(
gctx: &GlobalContext,
) -> CargoResult<Option<manifest::TomlTarget>> {
let path = target.path.as_ref().expect("previously normalized");
let path = normalize_path(&path.0);
let path = &path.0;
if let Some(packaged_files) = packaged_files {
if !packaged_files.contains(&path) {
let name = target.name.as_ref().expect("previously normalized");
Expand All @@ -3030,7 +3030,7 @@ pub fn prepare_target_for_publish(
}

let mut target = target.clone();
let path = normalize_path_sep(path, context)?;
let path = normalize_path_sep(path.to_path_buf(), context)?;
target.path = Some(manifest::PathValue(path.into()));

Ok(Some(target))
Expand Down
40 changes: 32 additions & 8 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::fs::{self, DirEntry};
use std::path::{Path, PathBuf};

use anyhow::Context as _;
use cargo_util::paths;
use cargo_util_schemas::manifest::{
PathValue, StringOrBool, StringOrVec, TomlBenchTarget, TomlBinTarget, TomlExampleTarget,
TomlLibTarget, TomlManifest, TomlTarget, TomlTestTarget,
Expand Down Expand Up @@ -133,7 +134,7 @@ pub fn normalize_lib(
warnings: &mut Vec<String>,
) -> CargoResult<Option<TomlLibTarget>> {
if is_normalized(original_lib, autodiscover) {
let Some(lib) = original_lib.cloned() else {
let Some(mut lib) = original_lib.cloned() else {
return Ok(None);
};

Expand All @@ -143,6 +144,10 @@ pub fn normalize_lib(
validate_proc_macro(&lib, "library", edition, warnings)?;
validate_crate_types(&lib, "library", edition, warnings)?;

if let Some(PathValue(path)) = &lib.path {
lib.path = Some(PathValue(paths::normalize_path(path).into()));
}

Ok(Some(lib))
} else {
let inferred = inferred_lib(package_root);
Expand Down Expand Up @@ -184,6 +189,10 @@ pub fn normalize_lib(
}
}

if let Some(PathValue(path)) = lib.path.as_ref() {
lib.path = Some(PathValue(paths::normalize_path(&path).into()));
}

Ok(Some(lib))
}
}
Expand Down Expand Up @@ -255,11 +264,15 @@ pub fn normalize_bins(
has_lib: bool,
) -> CargoResult<Vec<TomlBinTarget>> {
if are_normalized(toml_bins, autodiscover) {
let toml_bins = toml_bins.cloned().unwrap_or_default();
for bin in &toml_bins {
let mut toml_bins = toml_bins.cloned().unwrap_or_default();
for bin in toml_bins.iter_mut() {
validate_bin_name(bin, warnings)?;
validate_bin_crate_types(bin, edition, warnings, errors)?;
validate_bin_proc_macro(bin, edition, warnings, errors)?;

if let Some(PathValue(path)) = &bin.path {
bin.path = Some(PathValue(paths::normalize_path(path).into()));
}
}
Ok(toml_bins)
} else {
Expand Down Expand Up @@ -300,7 +313,7 @@ pub fn normalize_bins(
}
});
let path = match path {
Ok(path) => path,
Ok(path) => paths::normalize_path(&path).into(),
Err(e) => anyhow::bail!("{}", e),
};
bin.path = Some(PathValue(path));
Expand Down Expand Up @@ -603,13 +616,17 @@ fn normalize_targets_with_legacy_path(
autodiscover_flag_name: &str,
) -> CargoResult<Vec<TomlTarget>> {
if are_normalized(toml_targets, autodiscover) {
let toml_targets = toml_targets.cloned().unwrap_or_default();
for target in &toml_targets {
let mut toml_targets = toml_targets.cloned().unwrap_or_default();
for target in toml_targets.iter_mut() {
// Check early to improve error messages
validate_target_name(target, target_kind_human, target_kind, warnings)?;

validate_proc_macro(target, target_kind_human, edition, warnings)?;
validate_crate_types(target, target_kind_human, edition, warnings)?;

if let Some(PathValue(path)) = &target.path {
target.path = Some(PathValue(paths::normalize_path(path).into()));
}
}
Ok(toml_targets)
} else {
Expand Down Expand Up @@ -651,7 +668,7 @@ fn normalize_targets_with_legacy_path(
continue;
}
};
target.path = Some(PathValue(path));
target.path = Some(PathValue(paths::normalize_path(&path).into()));
result.push(target);
}
Ok(result)
Expand Down Expand Up @@ -1037,7 +1054,14 @@ pub fn normalize_build(build: Option<&StringOrBool>, package_root: &Path) -> Opt
}
}
// Explicitly no build script.
Some(StringOrBool::Bool(false)) | Some(StringOrBool::String(_)) => build.cloned(),
Some(StringOrBool::Bool(false)) => build.cloned(),
Some(StringOrBool::String(build_file)) => {
let build_file = paths::normalize_path(Path::new(build_file));
let build = build_file.into_os_string().into_string().expect(
"`build_file` started as a String and `normalize_path` shouldn't have changed that",
);
Some(StringOrBool::String(build))
}
Some(StringOrBool::Bool(true)) => Some(StringOrBool::String(BUILD_RS.to_owned())),
}
}
Expand Down
24 changes: 12 additions & 12 deletions tests/testsuite/binary_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,27 +413,27 @@ fn targets_with_relative_path_in_workspace_members() {
p.cargo("check")
.with_stderr_data(str![[r#"
...
--> relative-bar/./build.rs:1:17
--> relative-bar/build.rs:1:17
...
--> relative-bar/./src/lib.rs:1:4
--> relative-bar/src/lib.rs:1:4
...
--> relative-bar/./src/main.rs:1:17
--> relative-bar/src/main.rs:1:17
...
"#]])
.run();

p.cargo("check --example example")
.with_stderr_data(str![[r#"
...
--> relative-bar/./example.rs:1:17
--> relative-bar/example.rs:1:17
...
"#]])
.run();

p.cargo("check --test test")
.with_stderr_data(str![[r#"
...
--> relative-bar/./test.rs:5:35
--> relative-bar/test.rs:5:35
...
"#]])
.run();
Expand All @@ -442,7 +442,7 @@ fn targets_with_relative_path_in_workspace_members() {
p.cargo("check --bench bench")
.with_stderr_data(str![[r#"
...
--> relative-bar/./bench.rs:7:58
--> relative-bar/bench.rs:7:58
...
"#]])
.run();
Expand Down Expand Up @@ -490,27 +490,27 @@ fn targets_with_relative_path_in_workspace_members() {
p.cargo("check")
.with_stderr_data(str![[r#"
...
--> relative-bar/./build.rs:1:17
--> relative-bar/build.rs:1:17
...
--> relative-bar/./src/lib.rs:1:4
--> relative-bar/src/lib.rs:1:4
...
--> relative-bar/./src/main.rs:1:17
--> relative-bar/src/main.rs:1:17
...
"#]])
.run();

p.cargo("check --example example")
.with_stderr_data(str![[r#"
...
--> relative-bar/./example.rs:1:17
--> relative-bar/example.rs:1:17
...
"#]])
.run();

p.cargo("check --test test")
.with_stderr_data(str![[r#"
...
--> relative-bar/./test.rs:5:35
--> relative-bar/test.rs:5:35
...
"#]])
.run();
Expand All @@ -519,7 +519,7 @@ fn targets_with_relative_path_in_workspace_members() {
p.cargo("check --bench bench")
.with_stderr_data(str![[r#"
...
--> relative-bar/./bench.rs:7:58
--> relative-bar/bench.rs:7:58
...
"#]])
.run();
Expand Down

0 comments on commit a456c22

Please sign in to comment.