Skip to content

Incorrect output_root when --target parameter is a JSON file #513

@FZDSLR

Description

@FZDSLR

When building a project with cargo-c using a JSON file ( like /nix/store/58c64g7rvm8h3xpyg359xjpkfy29hpi7-rust/loongarch64-unknown-linux-gnu.json ) as the --target parameter (e.g., --target custom-target.json), cargo-c fails to properly handle the JSON target specification, resulting in an incorrectly computed output root path (output_root). When using cargo-c and a JSON target file to build librsvg on nix, it will cause errors like:

 Error: CliError { error: Some(failed to write `/nix/store/58c64g7rvm8h3xpyg359xjpkfy29hpi7-rust/loongarch64-unknown-linux-gnu.json/release/librsvg_c.pc`
 
 Caused by:
     Not a directory (os error 20)), exit_code: 101 }

because the builder doesn't have permission to write in the (wrong) path.

For reference, Cargo natively supports JSON files as --target parameters (Check https://github.com/rust-lang/cargo/blob/master/src/cargo/core/compiler/compile_kind.rs#L177-L179).

Applying the patch below to cargo-c passes the build, but I'm not familiar enough with Rust to determine if we can directly utilize existing Rust/cargo methods to solve this problem.

diff --git a/src/build.rs b/src/build.rs
index d560fd1..da7f512 100644
--- a/src/build.rs
+++ b/src/build.rs
@@ -1160,11 +1160,20 @@ pub fn cbuild(
     let mut compile_opts = compile_options(ws, config, args, profile, UserIntent::Build)?;
 
     // TODO: there must be a simpler way to get the right path.
+    let target_name = if target.ends_with(".json") {
+        Path::new(&target)
+        .file_stem()
+        .and_then(|s| s.to_str())
+        .unwrap_or(&target)
+    } else {
+        &target
+    };
+
     let root_output = ws
         .target_dir()
         .as_path_unlocked()
         .to_path_buf()
-        .join(PathBuf::from(target))
+        .join(PathBuf::from(target_name))
         .join(profiles.get_dir_name());
 
     let mut members = Vec::new();

Metadata

Metadata

Assignees

No one assigned

    Labels

    F-CargoParityTasks related to make the behavior as close as cargo as possible

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions