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

Config in Cargo.toml: invalid type: map, expected a string for key target.x86_64-unknown-freebsd.image #1182

Closed
4 of 11 tasks
n8henrie opened this issue Jan 5, 2023 · 5 comments · Fixed by #1183
Closed
4 of 11 tasks
Assignees
Labels

Comments

@n8henrie
Copy link

n8henrie commented Jan 5, 2023

Checklist

Describe your issue

Related: #657

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

x86_64-unknown-freebsd

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

  • macOS
  • Windows
  • Linux / BSD
  • other OS (specify in description)

What architecture is the host?

  • x86_64 / AMD64
  • arm32
  • arm64 (including Mac M1)

What container engine is cross using?

  • docker
  • podman
  • other container engine (specify in description)

cross version

cross 0.2.4

Example

$ cargo new foo && cd $_
$ cat <<'EOF' >> Cargo.toml
[package.metadata.cross.target.x86_64-unknown-freebsd]
image.name = "ghcr.io/cross-rs/x86_64-unknown-freebsd:local"
image.toolchain = ["linux/arm64/v8=aarch64-unknown-linux-gnu"]
EOF
$ cross build --target x86_64-unknown-freebsd --release

Using cross 0.2.4 from nixpkgs or crates.io, I get:

Error:
   0: invalid type: map, expected a string for key `target.x86_64-unknown-freebsd.image`

Using cross from main, I get a different error:

$ cargo install cross --git https://github.com/cross-rs/cross
$ cross --version
cross 0.2.4 (1d9d310 2023-01-05)
$ cross build --target x86_64-unknown-freebsd --release
Error:
   0: invalid type: string "linux/arm64/v8=aarch64-unknown-linux-gnu", expected a borrowed string for key `target.x86_64-unknown-freebsd.image.toolchain`

Location:
   src/cross_toml.rs:181

If I instead put (what I believe to be) the analogous table into Cross.toml, I get the same error with nixpkgs and crates.io but it works fine from main:

$ cat <<'EOF' > Cross.toml
[target.x86_64-unknown-freebsd]
image.name = "ghcr.io/cross-rs/x86_64-unknown-freebsd:local"
image.toolchain = ["linux/arm64/v8=aarch64-unknown-linux-gnu"]
EOF
$ cross --version
cross 0.2.4 (1d9d310 2023-01-05)
[cross] warning: `cross` does not provide a Docker image for target aarch64-apple-darwin, specify a custom image in `Cross.toml`.
[cross] note: Falling back to `cargo` on the host.
cargo 1.66.0 (d65d197ad 2022-11-15)
$ cross build --target x86_64-unknown-freebsd --release
   Compiling foo v0.1.0 (/private/var/folders/kb/tw_lp_xd2_bbv0hqk4m0bvt80000gn/T/tmp.ililnMNd9D/foo)
    Finished release [optimized] target(s) in 8.52s

Using the current main, as recommended in the README, is it expected that this table works from Cross.toml but not from Cargo.toml?

Thanks for your work on a cool project!

Additional information / notes

No response

@Emilgardis Emilgardis added the bug label Jan 6, 2023
@Emilgardis
Copy link
Member

Emilgardis commented Jan 6, 2023

This should work in Cargo.toml, the issue is how we're doing the deserialization

D: Deserializer<'de>,

and

#[serde(try_from = "&str")]

@Emilgardis
Copy link
Member

Emilgardis commented Jan 6, 2023

Alright, that was wrong, it's a bit more "wierd".

When merging, we create a CrossToml from the metadata here:

serde_json::from_value(value)

this works fine in most cases, however, when the serialization does not match the deserialization, this fails when they do not match (i.e it can be either a toml string or array).

Solution is to serialize

cross/src/docker/image.rs

Lines 104 to 117 in 1d9d310

/// The architecture/platform to use in the image
///
/// https://github.com/containerd/containerd/blob/release/1.6/platforms/platforms.go#L63
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(try_from = "&str")]
pub struct ImagePlatform {
/// CPU architecture, x86_64, aarch64 etc
pub architecture: Architecture,
/// The OS, i.e linux, windows, darwin
pub os: Os,
/// The platform variant, i.e v8, v7, v6 etc
pub variant: Option<String>,
pub target: TargetTriple,
}
as a string

@n8henrie
Copy link
Author

n8henrie commented Jan 6, 2023

Wow, super fast fix. Thank you!

After putting the config back in Cargo.toml it builds

$ rm -f Cross.toml
$ cargo install cross --git https://github.com/Emilgardis/cross --branch fix-1182
$ cross --version
cross 0.2.4 (6a57d01 2023-01-06)
[cross] warning: `cross` does not provide a Docker image for target aarch64-apple-darwin, specify a custom image in `Cross.toml`.
[cross] note: Falling back to `cargo` on the host.
cargo 1.66.0 (d65d197ad 2022-11-15)
$ cross build --target x86_64-unknown-freebsd --release
    Finished release [optimized] target(s) in 1.41s
$ file target/x86_64-unknown-freebsd/release/hello-world-pfsense
target/x86_64-unknown-freebsd/release/hello-world-pfsense: ELF 64-bit LSB pie executable, x86-64, version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 12.4, FreeBSD-style, stripped
$ scp target/x86_64-unknown-freebsd/release/hello-world-pfsense router:/tmp
hello-world-pfsense                                                                                                                                                                                         100%  275KB   1.4MB/s   00:00
$ ssh router /tmp/hello-world-pfsense
Hello, world!

I'm not sure about the warning (EDIT: I guess it's just a warning, probably not be a good idea to have it try to parse the config in order to silence the warning in a version check), but it seems to work!

bors bot added a commit that referenced this issue Jan 6, 2023
1183: Fix error when using certain config values in Cargo.toml r=Emilgardis a=Emilgardis

`pre-build = "file"` and `image.toolchain` had a bad serialization, this fixes that

Closes #1182


Co-authored-by: Emil Gardström <emil.gardstrom@gmail.com>
@bors bors bot closed this as completed in 8c86a98 Jan 6, 2023
@n8henrie
Copy link
Author

n8henrie commented Mar 31, 2024

This error is back.

$ tail -n 3 Cargo.toml 
[package.metadata.cross.target.x86_64-unknown-freebsd]
image.name = "ghcr.io/cross-rs/x86_64-unknown-freebsd:local"
image.toolchain = ["linux/arm64/v8=aarch64-unknown-linux-gnu"]
$ cross --version
cross 0.2.5
Error: 
   0: invalid type: map, expected a string for key `target.x86_64-unknown-freebsd.image`

Based on the docs it looks like image.name and image.toolchain are still supported configuration values.

EDIT: Only getting this from crates.io, installing from git seems to work.

@guapodero
Copy link

This error is back.

$ tail -n 3 Cargo.toml 
[package.metadata.cross.target.x86_64-unknown-freebsd]
image.name = "ghcr.io/cross-rs/x86_64-unknown-freebsd:local"
image.toolchain = ["linux/arm64/v8=aarch64-unknown-linux-gnu"]
$ cross --version
cross 0.2.5
Error: 
   0: invalid type: map, expected a string for key `target.x86_64-unknown-freebsd.image`

Based on the docs it looks like image.name and image.toolchain are still supported configuration values.

EDIT: Only getting this from crates.io, installing from git seems to work.

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants