Skip to content

Commit

Permalink
[ci] fully qualify --package IDs in toolchain roller, cargo.sh (#1568)
Browse files Browse the repository at this point in the history
By doing so, we prevent CI from being blocked by our dev-dependencies
taking dependencies on earlier versions of zerocopy. Such a scenario
introduces multiple packages named "zerocopy" and "zerocopy-derive"
into the build, and, consequently, `-p zerocopy` and
`-p zerocopy-derive` become ambiguous in most contexts.
  • Loading branch information
jswrenn authored Aug 6, 2024
1 parent 8d459a4 commit 16621ec
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/roll-pinned-toolchain-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,22 @@ jobs:
# introduced on this new toolchain that we can fix automatically.
# This is best-effort, so we don't let failure cause the whole job
# to fail.
cargo "+$VERSION_FOR_CARGO" fix --allow-dirty --tests --package zerocopy $ZEROCOPY_FEATURES || true
cargo "+$VERSION_FOR_CARGO" fix --allow-dirty --tests --package zerocopy-derive || true
#
# We use `cargo pkgid` here to ensure that the package identifier is
# fully qualified. Otherwise, if a dev-dependency were to take a
# dependency on an earlier version of zerocopy or zerocopy-derive,
# these invocations would fail due to ambiguity.
cargo "+$VERSION_FOR_CARGO" fix --allow-dirty --tests --package "$(cargo pkgid -p zerocopy)" $ZEROCOPY_FEATURES || true
cargo "+$VERSION_FOR_CARGO" fix --allow-dirty --tests --package "$(cargo pkgid -p zerocopy-derive)" || true
# Update `.stderr` files as needed for the new version.
TRYBUILD=overwrite cargo "+$VERSION_FOR_CARGO" test --package zerocopy $ZEROCOPY_FEATURES
TRYBUILD=overwrite cargo "+$VERSION_FOR_CARGO" test --package zerocopy-derive
#
# We use `cargo pkgid` here to ensure that the package identifier is
# fully qualified. Otherwise, if a dev-dependency were to take a
# dependency on an earlier version of zerocopy or zerocopy-derive,
# these invocations would fail due to ambiguity.
TRYBUILD=overwrite cargo "+$VERSION_FOR_CARGO" test --package "$(cargo pkgid -p zerocopy)" $ZEROCOPY_FEATURES
TRYBUILD=overwrite cargo "+$VERSION_FOR_CARGO" test --package "$(cargo pkgid -p zerocopy-derive)"
}
if [ "${{ matrix.toolchain }}" == stable ]; then
Expand Down
43 changes: 40 additions & 3 deletions tools/cargo-zerocopy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,46 @@ fn delegate_cargo() -> Result<(), Error> {
get_toolchain_rustflags(name),
env_rustflags,
);
rustup(["run", version, "cargo"], Some(("RUSTFLAGS", &rustflags)))
.args(args)
.execute();

let mut cmd = rustup(["run", version, "cargo"], Some(("RUSTFLAGS", &rustflags)));

// Computes the fully-qualified package name of workspace package `p`.
let fqpn = |p| {
// Generate a lockfile, if absent.
// This is a prerequisite of running pkgid.
let _ = rustup(["run", version, "cargo", "generate-lockfile"], None)
.output_or_exit();

let output = rustup(["run", version, "cargo", "pkgid", "-p"], None)
.arg(p)
.output_or_exit();
String::from_utf8(output.stdout).unwrap()
};

// Replace `-p<package>`, `-p <package>` and `--package <package`
// with the equivalent of `-p $(cargo pkgid -p <package>)`. We do
// this because unqualified package names are sometimes ambiguous
// if a dev-dependency has taken a dependency on an earlier
// version of zerocopy or zerocopy-derive.
loop {
let Some(arg) = args.next() else {
break;
};
if arg == "-p" || arg == "--package" {
cmd.arg(arg);
let Some(arg) = args.next() else {
break;
};
cmd.arg(fqpn(arg));
} else if arg.starts_with("-p") {
cmd.arg("-p");
cmd.arg(fqpn(arg[2..].to_string()));
} else {
cmd.arg(arg);
}
}

cmd.execute();

Ok(())
} else {
Expand Down

0 comments on commit 16621ec

Please sign in to comment.