cross-rs / cross

“Zero setup” cross compilation and “cross testing” of Rust crates
Apache License 2.0
6.59k stars 365 forks source link

git2::Repository::open error: repository path is not owned by current user #1497

Closed pravic closed 4 months ago

pravic commented 4 months ago

Checklist

Describe your issue

Problem

If https://lib.rs/git2 inside build.rs tries to open the project repository in order to describe it, the build.rs fails with

repository path is not owned by current user

Apparently, the target is build in Docker using the root user but the project directory itself is owned by the host user - which results the mentioned error when git2 tries to open the repository.

build.rs

fn main() -> Result<(), Box<dyn Error>> {
    let repo = match git2::Repository::open(".") {
        Ok(r) => r,
        Err(err) => {
            println!("open repo err: {} ", err);
            return Err(Box::new(err));
        }
    };
    // ...
}

Workaround

On the other hand, calling git describe from the build script works fine:

fn main() -> Result<(), Box<dyn Error>> {
    if std::env::var("CROSS_SYSROOT").is_ok() {
        let app_git_tag = std::process::Command::new("git").arg("describe").output().expect("`git describe` failed");
        let app_git_tag = std::str::from_utf8(&app_git_tag.stdout).expect("`git describe` not utf8");
        println!("cargo:rustc-env=APP_GIT_TAG={}", app_git_tag);
        return Ok(());
    }
    let repo = match git2::Repository::open(".") {
        Ok(r) => r,
        Err(err) => {
            println!("open repo err: {} ", err);
            return Err(Box::new(err));
        }
    };
    // ...
}

What target(s) are you cross-compiling for?

x86_64-unknown-linux-gnu

Which operating system is the host (e.g computer cross is on) running?

What architecture is the host?

What container engine is cross using?

cross version

cross 0.2.5 (19be834 2024-05-17)

Example

// build.rs
fn main() -> Result<(), Box<dyn Error>> {
    let repo = match git2::Repository::open(".") {
        Ok(r) => r,
        Err(err) => {
            println!("open repo err: {} ", err);
            return Err(Box::new(err));
        }
    };
    // ...
}
cross build --release --target x86_64-unknown-linux-gnu 

cargo stderr:

error: failed to run custom build command for `logs`

Caused by:
  process didn't exit successfully: `/target/release/build/logs-2e37abb95c8ee7c3/build-script-build` (exit status: 1)
  --- stdout
  open repo err: repository path '/Users/hello/logs/' is not owned by current user; class=Config (7); code=Owner (-36) 

  --- stderr
  Error: Error { code: -36, klass: 7, message: "repository path '/Users/hello/logs/' is not owned by current user" }

Additional information / notes

Cross is installed from the latest master via cargo install cross --git.

pravic commented 4 months ago

Judging by the mentioned workaround, looks like it's a problem more of https://github.com/rust-lang/git2-rs rather than from cross?

Emilgardis commented 4 months ago

The error could be correct, just that git2 crate for some reason hits the path that git doesn't like.

Try https://github.com/cross-rs/cross/discussions/1473#discussioncomment-9128550

[build]
pre-build = ["git config --system --add safe.directory '*'"]
pravic commented 4 months ago

Yep, it works. Thanks!