Skip to content

Commit d656fbd

Browse files
authored
Parse the package manifest directly for toolchains (#1564)
This prevents the lockfile for the workspace from being generated by a newer version of cargo, which may choose more up-to-date dependencies than our msrv toolchain can compile.
1 parent 1ff83c4 commit d656fbd

File tree

5 files changed

+22
-68
lines changed

5 files changed

+22
-68
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,6 @@ jobs:
119119

120120
- name: Populate cache
121121
uses: ./.github/actions/cache
122-
123-
# Ensure that Cargo resolves the minimum possible syn version so that if we
124-
# accidentally make a change which depends upon features added in more
125-
# recent versions of syn, we'll catch it in CI.
126-
- name: Pin syn dependency
127-
run: |
128-
set -eo pipefail
129-
# Override the exising `syn` dependency with one which requires an exact
130-
# version.
131-
cargo add -p zerocopy-derive 'syn@=2.0.46'
132122

133123
- name: Configure environment variables
134124
run: |

Cargo.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ zerocopy-panic-in-const = "1.57.0"
4646
[package.metadata.ci]
4747
# The versions of the stable and nightly compiler toolchains to use in CI.
4848
pinned-stable = "1.80.0"
49-
pinned-nightly = "nightly-2024-07-25"
49+
pinned-nightly = "nightly-2024-07-28"
5050

5151
[package.metadata.docs.rs]
5252
all-features = true
@@ -78,11 +78,7 @@ zerocopy-derive = { version = "=0.8.0-alpha.16", path = "zerocopy-derive" }
7878
[dev-dependencies]
7979
assert_matches = "1.5"
8080
itertools = "0.11"
81-
# We don't use this directly, but trybuild does. On the MSRV toolchain, the
82-
# version resolver fails to select any version for once_cell unless we
83-
# depend on it directly.
84-
once_cell = "=1.9"
85-
rand = { version = "0.8.5", features = ["small_rng"] }
81+
rand = { version = "0.8.5", default-features = false, features = ["small_rng"] }
8682
rustversion = "1.0"
8783
static_assertions = "1.1"
8884
testutil = { path = "testutil" }

tools/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ publish = false
2222

2323
[workspace.dependencies]
2424
regex = "1"
25-
serde_json = "1"
25+
toml = "0.8"

tools/cargo-zerocopy/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ license.workspace = true
1515
publish.workspace = true
1616

1717
[dependencies]
18-
serde_json.workspace = true
18+
toml = { workspace = true, features = ["parse"] }

tools/cargo-zerocopy/src/main.rs

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
// Cargo.toml section.
2121

2222
use std::{
23-
env, fmt,
23+
env, fmt, fs,
2424
io::{self, Read as _},
2525
process::{self, Command, Output},
2626
};
2727

28-
use serde_json::{Map, Value};
28+
use toml::{map::Map, Value};
2929

3030
#[derive(Debug)]
3131
enum Error {
@@ -110,52 +110,20 @@ impl Versions {
110110
}
111111

112112
fn get_toolchain_versions() -> Versions {
113-
// NOTE(#547): We set `CARGO_TARGET_DIR` here because `cargo metadata`
114-
// sometimes causes the `cargo-metadata` crate to be rebuilt from source
115-
// using the default toolchain. This has the effect of clobbering any
116-
// existing build artifacts from whatever toolchain the user has specified
117-
// (e.g., `+nightly`), causing the subsequent `cargo` invocation to rebuild
118-
// unnecessarily. By specifying a separate build directory here, we ensure
119-
// that this never clobbers the build artifacts used by the later `cargo`
120-
// invocation.
121-
//
122-
// In CI, make sure to use the default stable toolchain. If we're testing on
123-
// our MSRV, then we also have our MSRV toolchain installed. As of this
124-
// writing, our MSRV is low enough that the correspoding Rust toolchain's
125-
// Cargo doesn't know about the `rust-version` field, and so if we were to
126-
// use Cargo with that toolchain, `pkg-meta` would return `null` when asked
127-
// to retrieve the `rust-version` field. This also requires `RUSTFLAGS=''`
128-
// to override any unstable `RUSTFLAGS` set by the caller.
129-
let output = rustup(
130-
["run", "stable", "cargo", "metadata", "--format-version", "1"],
131-
Some(("CARGO_TARGET_DIR", "target/cargo-zerocopy")),
132-
)
133-
.env_remove("RUSTFLAGS")
134-
.output_or_exit();
135-
136-
let json = serde_json::from_slice::<Value>(&output.stdout).unwrap();
137-
let packages = json.as_object().unwrap().get("packages").unwrap();
138-
packages
139-
.as_array()
140-
.unwrap()
141-
.iter()
142-
.filter_map(|p| {
143-
let package = p.as_object().unwrap();
144-
if package.get("name").unwrap().as_str() == Some("zerocopy") {
145-
let metadata = package.get("metadata").unwrap().as_object().unwrap();
146-
let ci = metadata.get("ci").unwrap().as_object().unwrap();
147-
Some(Versions {
148-
msrv: package.get("rust_version").unwrap().as_str().unwrap().to_string(),
149-
stable: ci.get("pinned-stable").unwrap().as_str().unwrap().to_string(),
150-
nightly: ci.get("pinned-nightly").unwrap().as_str().unwrap().to_string(),
151-
build_rs: metadata.get("build-rs").unwrap().as_object().unwrap().clone(),
152-
})
153-
} else {
154-
None
155-
}
156-
})
157-
.next()
158-
.unwrap()
113+
let manifest_text = fs::read_to_string("Cargo.toml").unwrap();
114+
let manifest = toml::from_str::<Value>(&manifest_text).unwrap();
115+
116+
let package = manifest.as_table().unwrap()["package"].as_table().unwrap();
117+
let metadata = package["metadata"].as_table().unwrap();
118+
let build_rs = metadata["build-rs"].as_table().unwrap();
119+
let ci = metadata["ci"].as_table().unwrap();
120+
121+
Versions {
122+
msrv: package["rust-version"].as_str().unwrap().to_string(),
123+
stable: ci["pinned-stable"].as_str().unwrap().to_string(),
124+
nightly: ci["pinned-nightly"].as_str().unwrap().to_string(),
125+
build_rs: build_rs.clone(),
126+
}
159127
}
160128

161129
fn is_toolchain_installed(versions: &Versions, name: &str) -> Result<bool, Error> {
@@ -190,7 +158,7 @@ fn install_toolchain_or_exit(versions: &Versions, name: &str) -> Result<(), Erro
190158
}
191159

192160
let version = versions.get(name)?;
193-
rustup(["toolchain", "install", &version, "-c", "rust-src"], None).execute();
161+
rustup(["toolchain", "install", version, "-c", "rust-src"], None).execute();
194162

195163
Ok(())
196164
}
@@ -263,7 +231,7 @@ fn delegate_cargo() -> Result<(), Error> {
263231
get_toolchain_rustflags(name),
264232
env_rustflags,
265233
);
266-
rustup(["run", &version, "cargo"], Some(("RUSTFLAGS", &rustflags)))
234+
rustup(["run", version, "cargo"], Some(("RUSTFLAGS", &rustflags)))
267235
.args(args)
268236
.execute();
269237

0 commit comments

Comments
 (0)