From 3691343a48263412d81280aefec900ce65ac9954 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:21:16 -0700 Subject: [PATCH 01/10] chore(quest): claim quest/m1/ffi-export.md Quest-Claim: 954eba1d8b1596e6 From b32d80e4dec68e5abd0b44576e0585c5f02c3c86 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:43:52 -0700 Subject: [PATCH 02/10] feat(ffi): export moq-tokio, hang, and moq-mux, and carry config patches Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- driver/src/cargo.rs | 18 +- ffi/README.md | 13 +- ffi/justfile | 55 ++++-- ffi/moq/ffi.toml | 389 ++++++++++++++++++++++++++++++++++++++++- ffi/moq/moq.c | 35 +++- ffi/src/cli.rs | 175 ++++++++++++++++-- ffi/src/glue.rs | 9 + ffi/tests/fixtures.rs | 84 +++++++++ quest/m1/ffi-export.md | 21 +-- quest/m4/fork.md | 7 + 10 files changed, 748 insertions(+), 58 deletions(-) diff --git a/driver/src/cargo.rs b/driver/src/cargo.rs index 348961405..79697a1e3 100644 --- a/driver/src/cargo.rs +++ b/driver/src/cargo.rs @@ -123,7 +123,7 @@ impl Check<'_> { } cmd }; - run_cargo(cargo("clean").arg("--quiet"))?; + run_cargo(cargo("clean").arg("--quiet").args(resolution(self.args)))?; // Its own, since runs may share a target directory. let ran = target_dir.join(format!("{}-{}.ran", self.tool, std::process::id())); match std::fs::remove_file(&ran) { @@ -180,6 +180,22 @@ fn run_cargo(cmd: &mut Command) -> Result<(), String> { } } +/// The cargo args that pick how the workspace resolves, which `cargo clean` resolves it +/// by too: `--config`, and the lockfile and network flags. +fn resolution(args: &[String]) -> Vec<&str> { + let mut picked = Vec::new(); + let mut args = args.iter().map(String::as_str); + while let Some(arg) = args.next() { + match arg { + "--locked" | "--frozen" | "--offline" => picked.push(arg), + "--config" => picked.extend([Some(arg), args.next()].into_iter().flatten()), + _ if arg.starts_with("--config=") => picked.push(arg), + _ => {} + } + } + picked +} + /// The directory of the workspace `manifest`, or the current directory, belongs to. fn workspace_root(manifest: Option<&Path>) -> Result { let mut cmd = cargo(); diff --git a/ffi/README.md b/ffi/README.md index f53fc72bd..88c5dd3bc 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -18,7 +18,10 @@ all and an export names another's types across crates. `--out` writes a crate of libraries, its runtime beside it, and the header in `include/.h`; `cargo build` on that crate makes a static and a shared library. That crate enables each export's features as the check did, including `--features /`; such a feature on a -dependency that is no export is an error. +dependency that is no export is an error. It resolves them as the check did, wherever it +is built: its manifest carries the workspace's `[patch]` and `[replace]`, and the `[patch]` +of cargo's config where the check ran, from its files and `--config`, which outranks the +workspace's. Cargo's config `paths`, which no manifest can carry, is an error. ```toml # ffi.toml, beside the package's Cargo.toml unless --manifest-path says otherwise. @@ -128,7 +131,7 @@ rest. It ignores the config's `exclude`. ```bash just ffi test # export the fixtures, build them, and drive api from C -just ffi moq # export moq-net and its exports at the pinned revision, build them, and run moq/moq.c +just ffi moq # export moq's crates at the pinned revision, build them, and run moq/moq.c just ffi census # census each into target/census nix develop .#bench --command just ffi bench # frame writes, through the export and moq's rs/moq-c ``` @@ -138,5 +141,7 @@ export into the same library. `tests/api.c` runs the same script as the fixture's `examples/transcript.rs`, and the two transcripts must match line for line; then it breaks each runtime check, which must abort with its message. The `reject` fixture's expected errors, each with its notes, are `fixtures/reject/expected.txt`. -moq-net's `moq/moq.c` publishes and subscribes to a track in one process, polling each -read, and scopes an origin to moq-pattern's patterns. +moq's crates export through `target/moq-ffi`, an empty library on each that `just ffi moq` +writes in place of moq-ffi's remainder. `moq/moq.c` publishes and subscribes to a moq-net +track in one process, polling each read, scopes an origin to moq-pattern's patterns, and +reads an OpusHead into hang's catalog through moq-mux. diff --git a/ffi/justfile b/ffi/justfile index a55080d93..34157080b 100644 --- a/ffi/justfile +++ b/ffi/justfile @@ -34,15 +34,8 @@ run *args: cargo run --locked --quiet -- {{ args }} # Census each package moq/ffi.toml exports into target/census/.md, from the moq revision `just ts moq` pins or a checkout. -census checkout="": - #!/usr/bin/env bash - set -euo pipefail - src="{{ checkout }}" - if [ -z "$src" ]; then - (cd ../ts && just moq-pin) - src=../ts/target/moq - fi - cargo run --locked --quiet -- --config moq/ffi.toml --manifest-path "$src/rs/moq-net/Cargo.toml" --census target/census +census checkout="": (standin checkout) + cargo run --locked --quiet -- --config moq/ffi.toml --manifest-path target/moq-ffi/Cargo.toml --census target/census # Time frame writes through the export and through moq's hand-written rs/moq-c, in the bench shell: nix develop .#bench. bench checkout="": (moq checkout) @@ -62,8 +55,21 @@ bench checkout="": (moq checkout) target/bench-export done -# Export moq-net's C ABI, build it, and run moq/moq.c against it, from the moq revision `just ts moq` pins or a checkout. -moq checkout="": +# Export moq's crates' C ABI, build it, and run moq/moq.c against it, from the moq revision `just ts moq` pins or a checkout. +moq checkout="": (standin checkout) + #!/usr/bin/env bash + set -euo pipefail + out=target/export/moq + cargo run --locked --quiet -- --config moq/ffi.toml --manifest-path target/moq-ffi/Cargo.toml --out "$out" + # The lockfile the check resolved with pins what the export depends on. + cp target/moq-ffi/Cargo.lock "$out/Cargo.lock" + cargo build --quiet --release --manifest-path "$out/Cargo.toml" + cc -std=c11 -pedantic -Wall -Wextra -Werror -I "$out/include" moq/moq.c "$out/target/release/libmoq.a" -lpthread -ldl -lm -o target/moq-test + target/moq-test + +# Write target/moq-ffi, an empty library on every crate moq/ffi.toml exports, from the moq revision `just ts moq` pins or a checkout: the check builds with all of them, as moq-ffi's remainder will. +[private] +standin checkout="": #!/usr/bin/env bash set -euo pipefail src="{{ checkout }}" @@ -71,10 +77,23 @@ moq checkout="": (cd ../ts && just moq-pin) src=../ts/target/moq fi - out=target/export/moq - cargo run --locked --quiet -- --config moq/ffi.toml --manifest-path "$src/rs/moq-net/Cargo.toml" --out "$out" - # moq's lockfile pins what the export depends on, as it pins moq-net's own build. - cp "$src/Cargo.lock" "$out/Cargo.lock" - cargo build --quiet --release --manifest-path "$out/Cargo.toml" - cc -std=c11 -pedantic -Wall -Wextra -Werror -I "$out/include" moq/moq.c "$out/target/release/libmoq.a" -lpthread -ldl -lm -o target/moq-test - target/moq-test + src=$(realpath "$src") + dir=target/moq-ffi + # moq-ffi's dependencies among the exports, moq-tokio with its default features as + # moq-ffi has them, and moq-pattern, which moq-ffi reaches through moq-net. + exports=(hang moq-clock moq-mux moq-net moq-pattern moq-tokio) + mkdir -p "$dir/src" + { + printf '# Generated by just ffi standin, from %s.\n' "$src" + printf '[package]\nname = "moq-ffi"\nversion = "0.0.0"\nedition = "2024"\npublish = false\n\n[dependencies]\n' + for name in "${exports[@]}"; do + printf '%s = { path = "%s/rs/%s" }\n' "$name" "$src" "$name" + done + printf '\n[workspace]\n' + } > "$dir/Cargo.toml" + # rustc loads a dependency only once the crate names it. + for name in "${exports[@]}"; do + printf 'use %s as _;\n' "${name//-/_}" + done > "$dir/src/lib.rs" + # moq's lockfile pins what moq's own builds use. + cp "$src/Cargo.lock" "$dir/Cargo.lock" diff --git a/ffi/moq/ffi.toml b/ffi/moq/ffi.toml index d68100c67..a95f847db 100644 --- a/ffi/moq/ffi.toml +++ b/ffi/moq/ffi.toml @@ -1,5 +1,9 @@ -package = "moq-net" +package = "moq-ffi" prefix = "moq" + +# moq-net, and each package below, exported into one library through the stand-in for +# moq-ffi's remainder that `just ffi standin` writes, which builds with them all. +[crates.moq-net] # What the export refuses at the pinned revision, as `just ffi census` lists it: the API # a hand-written wrapper still has to cover. exclude = [ @@ -24,7 +28,6 @@ exclude = [ "Server::accept", "Server::accept_request", "Server::with_publisher", - "Session", "Timed", "Timestamp::as_micros", "Timestamp::as_millis", @@ -34,15 +37,11 @@ exclude = [ "Versions::iter", "announce::Announce", "announce::Consumer::poll_synced", - "auth::Issued", - "auth::Request", - "auth::Requests", "bytes", "cache::Pool::poll", "frame::Buffer", "frame::Producer::group", - "goaway::Consumer", - "goaway::Producer", + "goaway::Consumer::poll", "group::Consumer::poll_read_frames", "group::Consumer::read_frames", "group::Producer::write_frames", @@ -101,7 +100,7 @@ exclude = [ "varint::encode_quic", ] -[rename] +[crates.moq-net.rename] # The rule makes both this type and `Session::stats` `moq_session_stats`. "session::Stats" = "session_stats_snapshot" # The rule makes both this type and `Path::relative` `moq_path_relative`. @@ -133,3 +132,377 @@ exclude = [ "wall" = "now_wall" # The rule makes both this function and the type `Interval` `moq_interval`. "interval" = "interval_every" + +[crates.moq-tokio] +exclude = [ + "Addrs", + # Not refused: each names both a field and a method, whose getters `rename` cannot + # tell apart, so both are left out. + "Backoff::initial", + "Backoff::max", + "Backoff::timeout", + "Client::connect", + "Client::with_publisher", + "Listener::accept_health", + "Listener::local_addr", + "Listener::tcp_local_addr", + "Listener::websocket_local_addr", + "Log::level", + "Log::new", + "RedactedUrl", + "Server::accept_health", + "Server::local_addr", + "Server::websocket_local_addr", + "accept::Failure::ALL", + "accept::Failure::classify", + "accept::Health::failed", + "accept::Health::new", + "bind", + "cli::Answer::exit", + "cli::Deprecated::changed", + "cli::Deprecated::flag", + "cli::Deprecated::toml", + "cli::FileSource", + "cli::Merge", + "cli::UnknownEnv", + "cli::answer", + "cli::check_env", + "cli::usage_rs", + "connect::Addr", + "connect::Config::bind", + "connect::Config::tokens", + "connect::Config::url", + "connect::Config::version", + "connect::EmptyToken", + "connect::Resolved::bind", + "connect::Token", + "connect::Tokens::set", + "connection::Monitor::poll_presence", + "crypto::install_default", + "crypto::provider", + "failover::Attempt", + "listen::Config::preferred_v4", + "listen::Config::preferred_v6", + "listen::Config::version", + "listen::Reset::secret", + "notify", + "panic::isolate", + "panic::message", + "quic::Config::keylog", + "quic::Config::qlog", + "quic::Error", + "quic::Limits", + "quic::LimitsError", + "quic::Resolved::keylog", + "quic::Resolved::qlog", + "quic::driver::AckFrequencyConfig", + "quic::driver::ApplicationClose", + "quic::driver::AsyncUdpSocket", + "quic::driver::Chunk", + "quic::driver::ClientConfig", + "quic::driver::ClosedStream", + "quic::driver::ConfigError", + "quic::driver::ConnectError", + "quic::driver::Connecting", + "quic::driver::Connection::accept_bi", + "quic::driver::Connection::accept_uni", + "quic::driver::Connection::authenticated", + "quic::driver::Connection::close", + "quic::driver::Connection::close_reason", + "quic::driver::Connection::closed", + "quic::driver::Connection::congestion_state", + "quic::driver::Connection::handshake_data", + "quic::driver::Connection::open_bi", + "quic::driver::Connection::open_uni", + "quic::driver::Connection::peer_identity", + "quic::driver::Connection::read_datagram", + "quic::driver::Connection::remote_address", + "quic::driver::Connection::send_datagram", + "quic::driver::Connection::set_limits", + "quic::driver::Connection::stats", + "quic::driver::ConnectionClose", + "quic::driver::ConnectionError", + "quic::driver::ConnectionId", + "quic::driver::ConnectionIdGenerator", + "quic::driver::ConnectionStats", + "quic::driver::Dir", + "quic::driver::EcnCodepoint", + "quic::driver::Endpoint", + "quic::driver::EndpointConfig", + "quic::driver::FrameStats", + "quic::driver::FrameType", + "quic::driver::IdleTimeout", + "quic::driver::Incoming", + "quic::driver::InvalidCid", + "quic::driver::Lease", + "quic::driver::MtuDiscoveryConfig", + "quic::driver::PathStats", + "quic::driver::ReadError", + "quic::driver::ReadExactError", + "quic::driver::ReadToEndError", + "quic::driver::RecvStream", + "quic::driver::ResetError", + "quic::driver::SendDatagramError", + "quic::driver::SendStream", + "quic::driver::ServerConfig", + "quic::driver::StoppedError", + "quic::driver::StreamId", + "quic::driver::Transmit", + "quic::driver::TransportConfig", + "quic::driver::TransportErrorCode", + "quic::driver::UdpSender", + "quic::driver::UdpStats", + "quic::driver::ValidationTokenConfig", + "quic::driver::VarInt", + "quic::driver::VarIntBoundsExceeded", + "quic::driver::WriteError", + "quic::driver::Written", + "quic::driver::crypto", + "rustls", + "secret::Secret::as_bytes", + "server::Config::websocket", + "server::Link::local", + "server::Link::remote", + "server::Request::local_addr", + "server::Request::remote_addr", + "server::Request::url", + "server::Request::with_publisher", + "server::Request::within", + "tcp::Config::bind", + "tcp::Error", + "tcp::Listener", + "tls::Certificates::fingerprints", + "tls::Connect::build", + "tls::Connect::cert", + "tls::Connect::fingerprint", + "tls::Connect::key", + "tls::Connect::root", + "tls::Identity::generate", + "tls::KeyLog", + "tls::Listen::cert", + "tls::Listen::generate", + "tls::Listen::key", + "tls::Listen::load_roots", + "tls::Listen::root", + "tls::Listen::server_config", + "tls::PeerIdentity::chain", + "tls::PeerIdentity::from_chain", + "tls::Serve::config", + "tls::Serve::resolver", + "tls::parse_fingerprint", + "transport::Error", + "transport::RecvStream", + "transport::SendStream", + "transport::Session", + "unix::Allow::gid", + "unix::Allow::pid", + "unix::Allow::uid", + "unix::Config::bind", + "unix::Listener::accept", + "unix::Listener::path", + "unix::Listener::with_protocols", + "watch::Files", + "websocket::Error", + "websocket::Listener", + "websocket::Options", + "websocket::accept", + "worker::Group::finished", + "worker::Group::local_addr", + "worker::Group::members", + "worker::Member", + "worker::Spawner", + "worker::Workers::local_addr", +] + +[crates.moq-tokio.rename] +# The rule names these and moq-net's `Client`, `Server`, and `Error` alike. +"Client" = "tokio_client" +"Server" = "tokio_server" +"Error" = "tokio_error" +# The rule makes both this enum and `ConnectError` `moq_connect_error`. +"connect::Error" = "connect_target_error" +# The rule makes both this type and `Connection::monitor` `moq_connection_monitor`. +"connection::Monitor" = "monitor" + +[crates.hang] +exclude = [ + "Catalog", + "catalog::Archive::store", + "catalog::Archive::timelines", + "catalog::Audio::enabled", + "catalog::Audio::ranked", + "catalog::Audio::renditions", + "catalog::Binary::tracks", + "catalog::BinaryConfig::extra", + "catalog::Consumer", + "catalog::H265", + "catalog::Json::tracks", + "catalog::JsonConfig::extra", + "catalog::Reference", + "catalog::Text::renditions", + "catalog::UnknownContainer", + "catalog::UnknownProjection", + "catalog::Video::enabled", + "catalog::Video::ranked", + "catalog::Video::renditions", + "container::Frame::encode", + "echo::Snapshot", + "health::Connection::evidence", + "health::Evidence", + "health::Health", + "health::Limits", + "health::PublisherThresholds::skipped", + "health::Rendition::evidence", + "health::Report", + "health::Sample", + "health::TransportThresholds::headroom", + "health::TransportThresholds::loss", + "health::TransportThresholds::rtt", + "health::ViewerThresholds::errors", + "health::ViewerThresholds::late", + "health::ViewerThresholds::stalled", + "health::publisher", + "health::viewer", + "stats::Snapshot", + "timeline::Record", + "timeline::RecordExt", +] + +[crates.hang.rename] +# The rule names this and moq-net's `Error` alike. +"Error" = "hang_error" +# The rule makes both these enums and the `kind` methods of `AudioCodec` and `VideoCodec` +# `moq_catalog__codec_kind`. +"catalog::AudioCodecKind" = "audio_codec_kind" +"catalog::VideoCodecKind" = "video_codec_kind" + +[crates.moq-mux] +exclude = [ + "Linger", + "Source::bind", + "Source::catalog", + "Source::follow", + "Source::resolve", + "Source::resolve_reference", + "Source::stats", + "Source::subscribe_track", + "binary::Consumer::latest", + "binary::Consumer::next", + "binary::Consumer::poll_latest", + "binary::Consumer::poll_next", + "binary::Snapshot", + "binary::Stream", + "catalog::Config", + "catalog::Consumer", + "catalog::Entry", + "catalog::Guard", + "catalog::IntoRendition", + "catalog::Producer", + "catalog::RenditionConfig", + "catalog::Reserved", + "catalog::Select", + "catalog::Stream", + "catalog::Timebase", + "catalog::hang::CatalogExt", + "catalog::hang::Consumer", + "catalog::hang::Extra::get", + "catalog::hang::Extra::iter", + "catalog::hang::Extra::remove", + "catalog::hang::Extra::set", + "catalog::msf::Consumer", + "codec::aac::Config::parse", + "codec::aac::Import", + "codec::annexb::NalIterator", + "codec::annexb::build_prefix", + "codec::annexb::find_start_code", + "codec::annexb::from_length_prefixed", + "codec::annexb::length_prefixed_to_annexb", + "codec::av1::Error", + "codec::av1::Import", + "codec::av1::Split::decode", + "codec::av1::Split::flush", + "codec::flac::Config", + "codec::flac::Import", + "codec::h264::Avcc::pps", + "codec::h264::Avcc::sps", + "codec::h264::Export", + "codec::h264::Import", + "codec::h264::Split::decode", + "codec::h264::Split::flush", + "codec::h265::Export", + "codec::h265::Hvcc::pps", + "codec::h265::Hvcc::sps", + "codec::h265::Hvcc::vps", + "codec::h265::Import", + "codec::h265::Split::decode", + "codec::h265::Split::flush", + "codec::mp3::Import", + "codec::opus::Config::parse", + "codec::opus::Import", + "codec::opus::Mapping::new", + "codec::opus::mapping::Config", + "codec::vp8::Import", + "codec::vp9::Import", + "container::Consumer", + "container::Container", + "container::GroupConsumer", + "container::InvalidEnd", + "container::MissingKeyframe", + "container::Producer", + "container::flv::Export", + "container::flv::Import", + "container::fmp4::Export", + "container::fmp4::Fragmenter::push", + "container::fmp4::Fragmenter::push_group", + "container::fmp4::Import", + "container::fmp4::Muxer::fragment", + "container::fmp4::Muxer::read", + "container::fmp4::Wire::new", + "container::fmp4::Wire::trak", + "container::fmp4::mp4_atom", + "container::mkv::Error", + "container::mkv::Export", + "container::mkv::Import", + "container::ts::Catalog", + "container::ts::Export", + "container::ts::Follower", + "container::ts::Import", + "container::ts::Mpegts::program_descriptors", + "container::ts::Mpegts::si", + "container::ts::Mpegts::tracks", + "container::ts::MultipleProgramsError", + "container::ts::Passthrough", + "container::ts::Programs", + "container::ts::Track::descriptors", + "container::ts::stats::Export::streams", + "container::ts::stats::Snapshot::streams", + "follow::Event", + "follow::Instances", + "import::Container", + "import::Track", + "import::TrackStream", + "json::Consumer", + "json::Snapshot", + "json::Stream", + "select::Broadcast::retain", + "timeline::Consumer", + "timeline::Entry", + "timeline::Event", + "timeline::Producer::push", +] + +[crates.moq-mux.rename] +# The rule names these and moq-net's `Error` and hang's `container::Frame` alike. +"Error" = "mux_error" +"container::Frame" = "mux_container_frame" +# The rule makes both this constant and `CatalogFormat`'s `Default` `moq_catalog_catalog_format_default`. +"catalog::CatalogFormat::DEFAULT" = "catalog_catalog_format_fallback" +# The rule makes both this constant and the method `Clock::timescale` `moq_clock_timescale`. +"Clock::TIMESCALE" = "clock_fixed_timescale" +# The rule makes each of these functions and its module's `Config` `moq_codec__config`. +"codec::aac::config" = "codec_aac_audio_config" +"codec::mp3::config" = "codec_mp3_audio_config" +"codec::opus::config" = "codec_opus_audio_config" +# The rule makes these methods and the variants' tags `moq_container_fmp4_chunk_`. +"container::fmp4::Chunk::init" = "container_fmp4_chunk_into_init" +"container::fmp4::Chunk::fragment" = "container_fmp4_chunk_into_fragment" diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 31d267d94..6aae8630a 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -1,6 +1,7 @@ -/* moq-net's exported C API against the real library: a broadcast's track, published - * and subscribed to in one process, with every read polled as a future, and an origin. - * moq-pattern and moq-clock export into the same library, and moq-net names their types. +/* moq's exported C API against the real library: a broadcast's track, published and + * subscribed to in one process, with every read polled as a future, and an origin. + * moq-pattern, moq-clock, moq-tokio, hang, and moq-mux export into the same library, and + * each names the others' types. * * It needs no runtime: the waker counts wakes, and each poll that must wait is polled * again after the write that wakes it. */ @@ -183,6 +184,34 @@ int main(void) { moq_origin_driver_free(driver); moq_origin_producer_free(origin); + /* moq-mux reads an OpusHead into hang's catalog config: 44.1 kHz in, stereo, which + * the catalog lists at the decoder's 48 kHz, keeping the head as its description. */ + static const uint8_t head[] = {'O', 'p', 'u', 's', 'H', 'e', 'a', 'd', 1, 2, 0x38, 0x01, 0x44, 0xac, 0, 0, 0, 0, 0}; + moq_catalog_audio_config *audio = NULL; + moq_mux_error *mux_err = NULL; + CHECK(moq_codec_opus_audio_config((moq_buf){.ptr = head, .len = sizeof head}, &audio, &mux_err)); + CHECK(moq_catalog_audio_config_sample_rate(audio) == 48000); + CHECK(moq_catalog_audio_config_channel_count(audio) == 2); + moq_catalog_audio_codec codec = moq_catalog_audio_config_codec(audio); + CHECK(codec.tag == MOQ_CATALOG_AUDIO_CODEC_OPUS); + moq_catalog_audio_codec_free(&codec); + moq_buf description = moq_catalog_audio_config_description(audio); + CHECK(description.len == sizeof head && memcmp(description.ptr, head, sizeof head) == 0); + moq_buf_free(description); + moq_catalog_audio_config_free(audio); + /* A truncated head is moq-mux's error. */ + CHECK(!moq_codec_opus_audio_config((moq_buf){.ptr = head, .len = 8}, &audio, &mux_err)); + CHECK(mux_err != NULL); + moq_buf message = moq_mux_error_to_string(mux_err); + CHECK(message.len > 0); + moq_buf_free(message); + moq_mux_error_free(mux_err); + + /* moq-tokio's terminal connect errors, which moq-net's `Error` shares no names with. */ + moq_connect_error unauthorized = MOQ_CONNECT_ERROR_UNAUTHORIZED; + CHECK(moq_connect_error_is_auth(&unauthorized)); + CHECK(is(moq_connect_error_to_string(&unauthorized), "unauthorized")); + if (failures == 0) { printf("ok\n"); } diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index d5b53a1d0..05d74fe4d 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -159,19 +159,144 @@ fn crates(manifest: &Path, config: &Config, cargo: &[String]) -> Result<(String, } lines.push_str(&package.line()); } - Ok((lines, patches(&metadata.workspace_root)?)) + let dir = manifest.parent().expect("a manifest path has a parent"); + Ok((lines, patches(&metadata.workspace_root, dir, &picks)?)) } -/// The workspace's `[patch]` and `[replace]` tables, with each path absolute, so the -/// glue's own workspace resolves the exports as the check does. -fn patches(root: &Path) -> Result { +/// The workspace's `[patch]` and `[replace]` tables, and the `[patch]` of cargo's config +/// where the check runs, in `dir`, which outranks the workspace's a dependency at a time: +/// each path absolute, so the glue's own workspace, built from anywhere, resolves the +/// exports as the check does. +fn patches(root: &Path, dir: &Path, picks: &Picks<'_>) -> Result { let manifest = root.join("Cargo.toml"); - let text = std::fs::read_to_string(&manifest).map_err(|err| format!("reading {}: {err}", manifest.display()))?; - let table: toml::Table = text - .parse() - .map_err(|err| format!("parsing {}: {err}", manifest.display()))?; - toml::to_string(&overrides(&table, root)) - .map_err(|err| format!("writing the patches of {}: {err}", manifest.display())) + let mut patches = overrides(&read_toml(&manifest)?, root); + let config = config(dir, picks)?; + if let Some(paths) = config.get("paths") { + return Err(format!( + "cargo's config overrides {paths} by `paths`, which no manifest can, so the export's library would build them unpatched" + )); + } + for (source, deps) in config + .get("patch") + .and_then(toml::Value::as_table) + .into_iter() + .flatten() + { + let patch = patches.entry("patch").or_insert_with(|| toml::Table::new().into()); + let into = (patch.as_table_mut()).map(|patch| patch.entry(source).or_insert_with(|| toml::Table::new().into())); + let (Some(toml::Value::Table(into)), Some(deps)) = (into, deps.as_table()) else { + return Err(format!("`patch.{source}` is not a table")); + }; + into.extend(deps.clone()); + } + toml::to_string(&patches).map_err(|err| format!("writing the patches of {}: {err}", manifest.display())) +} + +fn read_toml(path: &Path) -> Result { + let text = std::fs::read_to_string(path).map_err(|err| format!("reading {}: {err}", path.display()))?; + text.parse().map_err(|err| format!("parsing {}: {err}", path.display())) +} + +/// The `[patch]` and `paths` of cargo's config as cargo reads it in `dir`, each patch's +/// path absolute: `$CARGO_HOME`'s file, then each directory's from the root down to +/// `dir`, then each `--config` value, each outranking the last. +fn config(dir: &Path, picks: &Picks<'_>) -> Result { + // Cargo walks the directory it runs in, symlinks resolved. + let dir = std::fs::canonicalize(dir).map_err(|err| format!("resolving {}: {err}", dir.display()))?; + let home = match std::env::var_os("CARGO_HOME") { + Some(home) => Some(dir.join(home)), + None => std::env::home_dir().map(|home| home.join(".cargo")), + }; + let mut dirs: Vec = dir.ancestors().map(|d| d.join(".cargo")).collect(); + dirs.reverse(); + if let Some(home) = home.filter(|home| !dirs.contains(home)) { + dirs.insert(0, home); + } + let mut config = toml::Table::new(); + for dir in dirs { + // The name without `.toml` wins where both are. + let file = ["config", "config.toml"] + .into_iter() + .map(|name| dir.join(name)) + .find(|f| f.is_file()); + if let Some(file) = file { + merge(&mut config, load(&file)?); + } + } + for value in picks.configs() { + // A value naming a file is that file, and otherwise a TOML key and value, whose + // paths are from where cargo runs. + let file = dir.join(value); + let table = match !value.is_empty() && file.exists() { + true => load(&file)?, + false => { + let table = value + .parse() + .map_err(|err| format!("parsing --config {value}: {err}"))?; + absolute(table, &dir) + } + }; + merge(&mut config, table); + } + config.retain(|key, _| key == "patch" || key == "paths"); + Ok(config) +} + +/// A cargo config file's values with its `include`s' merged in beneath them, in order, +/// each patch's path absolute: from the directory holding the file's directory. +fn load(file: &Path) -> Result { + let mut table = read_toml(file)?; + let dir = file.parent().expect("a file has a parent"); + let includes = match table.remove("include") { + None => Vec::new(), + Some(toml::Value::String(path)) => vec![toml::Value::String(path)], + Some(toml::Value::Array(includes)) => includes, + Some(_) => return Err(format!("{}: `include` is not a path or a list", file.display())), + }; + let mut config = toml::Table::new(); + for include in includes { + let (path, optional) = match &include { + toml::Value::String(path) => (Some(path.as_str()), false), + toml::Value::Table(table) => ( + table.get("path").and_then(toml::Value::as_str), + table.get("optional").and_then(toml::Value::as_bool).unwrap_or(false), + ), + _ => (None, false), + }; + let Some(path) = path else { + return Err(format!("{}: an `include` names no path: {include}", file.display())); + }; + let path = dir.join(path); + if optional && !path.exists() { + continue; + } + merge(&mut config, load(&path)?); + } + merge(&mut config, absolute(table, dir.parent().unwrap_or(dir))); + Ok(config) +} + +/// A config's values with each patch's path made absolute from `root`. Cargo reads no +/// `[replace]` from its config. +fn absolute(mut table: toml::Table, root: &Path) -> toml::Table { + if let Some(patch) = overrides(&table, root).remove("patch") { + table.insert("patch".to_string(), patch); + } + table +} + +/// Merge `high` into `low` as cargo merges its config: tables key by key, arrays joined +/// with `high`'s last, and any other value `high`'s. +fn merge(low: &mut toml::Table, high: toml::Table) { + for (key, high) in high { + match (low.get_mut(&key), high) { + (Some(toml::Value::Table(low)), toml::Value::Table(high)) => merge(low, high), + (Some(toml::Value::Array(low)), toml::Value::Array(high)) => low.extend(high), + (_, high) => { + low.insert(key, high); + } + } + } } /// A manifest's `[patch.]` and `[replace]` dependencies, each path made absolute @@ -261,6 +386,19 @@ impl<'a> Picks<'a> { Ok(picks) } + /// Each `--config` value, in order. + fn configs(&self) -> Vec<&'a str> { + let mut values = Vec::new(); + let mut flags = self.flags.iter().copied(); + while let Some(flag) = flags.next() { + match flag { + "--config" => values.extend(flags.next()), + _ => values.extend(flag.strip_prefix("--config=")), + } + } + values + } + /// The same picks, as cargo args. fn args(&self) -> Vec { let mut args: Vec = self.flags.iter().map(|f| f.to_string()).collect(); @@ -377,6 +515,22 @@ mod tests { assert_eq!(overrides(&manifest, Path::new("/ws")), expected); } + #[test] + fn merges_config_as_cargo() { + let mut low: toml::Table = "[patch.crates-io]\na = { path = \"low\", features = [\"x\"] }\nb = \"1\"\n" + .parse() + .unwrap(); + let high: toml::Table = "[patch.crates-io]\na = { path = \"high\", features = [\"y\"] }\nc = \"2\"\n" + .parse() + .unwrap(); + merge(&mut low, high); + let expected: toml::Table = + "[patch.crates-io]\na = { path = \"high\", features = [\"x\", \"y\"] }\nb = \"1\"\nc = \"2\"\n" + .parse() + .unwrap(); + assert_eq!(low, expected); + } + #[test] fn reads_what_picks_a_resolution() { for spelling in [ @@ -404,6 +558,7 @@ mod tests { let cargo: Vec = cargo.iter().map(|s| s.to_string()).collect(); let flags = ["--all-features", "--locked", "--config", "a=1", "--config=b=2"]; assert_eq!(Picks::parse(&cargo).unwrap().args(), flags); + assert_eq!(Picks::parse(&cargo).unwrap().configs(), ["a=1", "b=2"]); for target in [ &["--target", "wasm32-unknown-unknown"][..], &["--target=wasm32-unknown-unknown"], diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 38538152d..16645a77a 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -102,6 +102,7 @@ impl Crate<'_> { let mut generator = Gen { abi: self.abi, packages: self.packages(), + dependencies: self.exports.iter().map(|export| dependency(&export.root())).collect(), out: String::new(), }; generator.lib(); @@ -116,6 +117,8 @@ struct Gen<'a> { abi: &'a Abi<'a>, /// The exported packages, for the docs. packages: String, + /// The name the crate depends on each export by. + dependencies: Vec, out: String, } @@ -305,6 +308,12 @@ impl Gen<'_> { self.line("#![allow(dead_code, reason = \"the runtime serves every API\")]\n"); self.line("mod rt;\n"); self.line("use std::ffi::c_void;\n"); + // An export may export nothing and only enable features in the others, as a + // package standing in for a binding crate does, which cargo would call unused. + for dependency in self.dependencies.clone() { + self.line(format!("use {dependency} as _;")); + } + self.line(""); let p = self.abi.prefix.clone(); self.line(format!( diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 064e8c365..a1d2fe44b 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -523,3 +523,87 @@ fn patched() { .arg("--target-dir") .arg(package.join("glue"))); } + +/// Patches in cargo's config, which cargo reads only where the check runs, reach the glue +/// as the check resolved them: an ancestor's file through its `include`, with paths from +/// the directory holding its `.cargo`, outranking the manifest's patch of the same +/// dependency, and a `--config` value, with paths from the package's directory. +#[test] +fn config_patched() { + let root = scratch().join("config-patched"); + let write = |path: &str, text: &str| { + let path = root.join(path); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(path, text).unwrap(); + }; + let dep = |dir: &str, name: &str, item: &str| { + write( + &format!("{dir}/Cargo.toml"), + &format!("[package]\nname = \"{name}\"\nversion = \"1.0.0\"\nedition = \"2024\"\n"), + ); + write(&format!("{dir}/src/lib.rs"), item); + }; + dep( + "pkg/dep", + "rust2-ffi-config-dep", + "pub struct Size {\n\tpub width: u32,\n}\n", + ); + // What the manifest patches in, which lacks the field the package reads. + dep( + "pkg/stale", + "rust2-ffi-config-dep", + "pub struct Size {\n\tpub height: u32,\n}\n", + ); + dep( + "pkg/cli", + "rust2-ffi-config-cli", + "pub struct Margin {\n\tpub width: u32,\n}\n", + ); + write(".cargo/config.toml", "include = [\"patch.toml\"]\n"); + write( + ".cargo/patch.toml", + "[patch.crates-io]\nrust2-ffi-config-dep = { path = \"pkg/dep\" }\n", + ); + write( + "pkg/Cargo.toml", + "[package]\nname = \"config-patched\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ + [dependencies]\nrust2-ffi-config-cli = \"1\"\nrust2-ffi-config-dep = \"1\"\n\n\ + [patch.crates-io]\nrust2-ffi-config-dep = { path = \"stale\" }\n\n\ + [workspace]\nexclude = [\"cli\", \"dep\", \"stale\"]\n", + ); + write( + "pkg/src/lib.rs", + "pub fn width(size: rust2_ffi_config_dep::Size, cli: rust2_ffi_config_cli::Margin) -> u32 {\n\ + \tsize.width + cli.width\n}\n", + ); + write( + "pkg/ffi.toml", + "package = \"config-patched\"\n\n[crates.rust2-ffi-config-cli]\n\n[crates.rust2-ffi-config-dep]\n", + ); + let out = root.join("out"); + let output = Command::new(env!("CARGO_BIN_EXE_rust2-ffi")) + .arg("--config") + .arg(root.join("pkg/ffi.toml")) + .arg("--out") + .arg(&out) + .args([ + "--", + "--offline", + "--config", + "patch.crates-io.rust2-ffi-config-cli.path = \"cli\"", + ]) + .env("CARGO_TARGET_DIR", root.join("build")) + .output() + .unwrap(); + assert!( + output.status.success(), + "the export failed:\n{}", + String::from_utf8_lossy(&output.stderr) + ); + // Built from here, where none of that config is read. + run(cargo() + .args(["build", "--quiet", "--offline", "--manifest-path"]) + .arg(out.join("Cargo.toml")) + .arg("--target-dir") + .arg(root.join("glue"))); +} diff --git a/quest/m1/ffi-export.md b/quest/m1/ffi-export.md index 68ea66c2e..a397baaf9 100644 --- a/quest/m1/ffi-export.md +++ b/quest/m1/ffi-export.md @@ -19,20 +19,13 @@ census refuses and what moq waits on: makes `Path` owned. Once it lands, bump the moq pin: the census then exports `Path::new`, `strip_prefix`, and `Relative::new`, and drops `AsPath` and the `borrow` methods from [the exclude list](/ffi/moq/ffi.toml), refusing only `Path::from_static`'s `'static` borrow. -- **Several crates**: moq-tokio's sessions and the media crates (hang, moq-mux) hold - moq-ffi's remaining API. Export them as moq-pattern and moq-clock already export beside - moq-net, each under `[crates.]` in [the config](/ffi/moq/ffi.toml). The checked - package must build with every export, so this needs moq-ffi's remainder, or a crate - standing in for it, as that package. Decided with the maintainer: - - ✅ Each crate is its own export with its own census, and an export names another's - types across crates, not names at a re-export's path. - - ✅ One rustc session collects every export from metadata: one IR, one glue crate, one - library. - - ✅ One prefix per library, with names from each crate's root and clashes checked across - exports. - - ✅ One header, `include/.h`. - - ✅ One `ffi.toml`, with a `[crates.]` table per other export, and - `--census ` writing `.md` for each. +- **Several crates**: moq-net, moq-pattern, moq-clock, moq-tokio, hang, and moq-mux export + into one library through an empty package standing in for moq-ffi's remainder, which + `just ffi standin` writes; moq-ffi takes its place once it shrinks to that remainder. + Left: + - moq-clock's C names (`moq_now`, `moq_origin`) wait for its merge into kio. + - A field and a method of one name, such as moq-tokio's `Backoff::initial`, claim one C + name that `rename` cannot split, so [the config](/ffi/moq/ffi.toml) excludes both. - **Driving**: sessions and origins run a `Driver` polled with a time and a `kio::Waiter`. Decide between exporting that poll, a generated thread that drives it, or moq-tokio's runtime helpers. diff --git a/quest/m4/fork.md b/quest/m4/fork.md index 0f7004f60..1104246d4 100644 --- a/quest/m4/fork.md +++ b/quest/m4/fork.md @@ -34,6 +34,13 @@ Each names what the fork changes, the measured gain on moq, and its source. semantics need their own before this counts as sound. Each skip saves at most about 56% of the edit: the dependents' share of its critical path, minus the final link ([relink](/quest/m3/relink.md)). +- **Cargo's resolved config, with each value's origin.** Expose the merged + `[patch]` cargo resolves with, or have `cargo metadata` report the patches it + applied. Stable cargo has no way to read either (`cargo config get` is unstable, + [rust-lang/cargo#9301](https://github.com/rust-lang/cargo/issues/9301)), so + rust2-ffi reimplements config discovery, `include`, merging, and relative paths + to carry config patches into its generated crate, and drifts when cargo's rules + change. Gain: no build speed; it deletes about 100 lines of `ffi/src/cli.rs`. ### Not reasons From b43dcb4b6293ec9d96e871470d0aa97fc3238ce2 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:06:52 -0700 Subject: [PATCH 03/10] fix(ffi): expand inline config includes and merge patches by source Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ffi/src/cli.rs | 75 ++++++++++++++++++++++++++++++------------- ffi/tests/fixtures.rs | 16 ++++----- 2 files changed, 60 insertions(+), 31 deletions(-) diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index 05d74fe4d..fa3907391 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -176,22 +176,47 @@ fn patches(root: &Path, dir: &Path, picks: &Picks<'_>) -> Result "cargo's config overrides {paths} by `paths`, which no manifest can, so the export's library would build them unpatched" )); } - for (source, deps) in config + let patch = patches.entry("patch").or_insert_with(|| toml::Table::new().into()); + let Some(patch) = patch.as_table_mut() else { + return Err(format!("{}: `patch` is not a table", manifest.display())); + }; + for (key, deps) in config .get("patch") .and_then(toml::Value::as_table) .into_iter() .flatten() { - let patch = patches.entry("patch").or_insert_with(|| toml::Table::new().into()); - let into = (patch.as_table_mut()).map(|patch| patch.entry(source).or_insert_with(|| toml::Table::new().into())); - let (Some(toml::Value::Table(into)), Some(deps)) = (into, deps.as_table()) else { - return Err(format!("`patch.{source}` is not a table")); + // Into the manifest's table of the same source, however it spells it. + let source = source(key, &config); + let into = (patch.keys()) + .find(|k| self::source(k, &config) == source) + .cloned() + .unwrap_or_else(|| key.clone()); + let into = patch.entry(into).or_insert_with(|| toml::Table::new().into()); + let (Some(into), Some(deps)) = (into.as_table_mut(), deps.as_table()) else { + return Err(format!("`patch.{key}` is not a table")); }; into.extend(deps.clone()); } + if patch.is_empty() { + patches.remove("patch"); + } toml::to_string(&patches).map_err(|err| format!("writing the patches of {}: {err}", manifest.display())) } +/// The source a `[patch.]` names, as cargo resolves it: crates.io's index or a +/// configured registry's by name, and otherwise the URL itself. +fn source(key: &str, config: &toml::Table) -> String { + if key == "crates-io" { + return "https://github.com/rust-lang/crates.io-index".to_string(); + } + let index = (config.get("registries")) + .and_then(|registries| registries.get(key)) + .and_then(|registry| registry.get("index")) + .and_then(toml::Value::as_str); + index.unwrap_or(key).to_string() +} + fn read_toml(path: &Path) -> Result { let text = std::fs::read_to_string(path).map_err(|err| format!("reading {}: {err}", path.display()))?; text.parse().map_err(|err| format!("parsing {}: {err}", path.display())) @@ -230,28 +255,37 @@ fn config(dir: &Path, picks: &Picks<'_>) -> Result { let table = match !value.is_empty() && file.exists() { true => load(&file)?, false => { - let table = value - .parse() - .map_err(|err| format!("parsing --config {value}: {err}"))?; - absolute(table, &dir) + let origin = format!("--config {value}"); + let table = value.parse().map_err(|err| format!("parsing {origin}: {err}"))?; + expand(table, &dir, &dir, &origin)? } }; merge(&mut config, table); } - config.retain(|key, _| key == "patch" || key == "paths"); + config.retain(|key, _| matches!(key, "patch" | "paths" | "registries")); Ok(config) } -/// A cargo config file's values with its `include`s' merged in beneath them, in order, -/// each patch's path absolute: from the directory holding the file's directory. +/// A cargo config file's values, as [`expand`] has them: its `include`s from its own +/// directory, and its patches' paths from the directory holding that. fn load(file: &Path) -> Result { - let mut table = read_toml(file)?; let dir = file.parent().expect("a file has a parent"); + expand( + read_toml(file)?, + dir, + dir.parent().unwrap_or(dir), + &file.display().to_string(), + ) +} + +/// A config's values with its `include`s merged in beneath them, in order, each path in +/// `dir`, and each patch's path made absolute from `root`. `origin` names it in errors. +fn expand(mut table: toml::Table, dir: &Path, root: &Path, origin: &str) -> Result { let includes = match table.remove("include") { None => Vec::new(), Some(toml::Value::String(path)) => vec![toml::Value::String(path)], Some(toml::Value::Array(includes)) => includes, - Some(_) => return Err(format!("{}: `include` is not a path or a list", file.display())), + Some(_) => return Err(format!("{origin}: `include` is not a path or a list")), }; let mut config = toml::Table::new(); for include in includes { @@ -264,7 +298,7 @@ fn load(file: &Path) -> Result { _ => (None, false), }; let Some(path) = path else { - return Err(format!("{}: an `include` names no path: {include}", file.display())); + return Err(format!("{origin}: an `include` names no path: {include}")); }; let path = dir.join(path); if optional && !path.exists() { @@ -272,17 +306,12 @@ fn load(file: &Path) -> Result { } merge(&mut config, load(&path)?); } - merge(&mut config, absolute(table, dir.parent().unwrap_or(dir))); - Ok(config) -} - -/// A config's values with each patch's path made absolute from `root`. Cargo reads no -/// `[replace]` from its config. -fn absolute(mut table: toml::Table, root: &Path) -> toml::Table { + // Cargo reads no `[replace]` from its config. if let Some(patch) = overrides(&table, root).remove("patch") { table.insert("patch".to_string(), patch); } - table + merge(&mut config, table); + Ok(config) } /// Merge `high` into `low` as cargo merges its config: tables key by key, arrays joined diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index a1d2fe44b..436b67fa7 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -527,7 +527,8 @@ fn patched() { /// Patches in cargo's config, which cargo reads only where the check runs, reach the glue /// as the check resolved them: an ancestor's file through its `include`, with paths from /// the directory holding its `.cargo`, outranking the manifest's patch of the same -/// dependency, and a `--config` value, with paths from the package's directory. +/// dependency under another spelling of its source, and a `--config` value's `include`, +/// from the package's directory. #[test] fn config_patched() { let root = scratch().join("config-patched"); @@ -564,11 +565,15 @@ fn config_patched() { ".cargo/patch.toml", "[patch.crates-io]\nrust2-ffi-config-dep = { path = \"pkg/dep\" }\n", ); + write( + "pkg/.cargo/cli.toml", + "[patch.crates-io]\nrust2-ffi-config-cli = { path = \"cli\" }\n", + ); write( "pkg/Cargo.toml", "[package]\nname = \"config-patched\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ [dependencies]\nrust2-ffi-config-cli = \"1\"\nrust2-ffi-config-dep = \"1\"\n\n\ - [patch.crates-io]\nrust2-ffi-config-dep = { path = \"stale\" }\n\n\ + [patch.'https://github.com/rust-lang/crates.io-index']\nrust2-ffi-config-dep = { path = \"stale\" }\n\n\ [workspace]\nexclude = [\"cli\", \"dep\", \"stale\"]\n", ); write( @@ -586,12 +591,7 @@ fn config_patched() { .arg(root.join("pkg/ffi.toml")) .arg("--out") .arg(&out) - .args([ - "--", - "--offline", - "--config", - "patch.crates-io.rust2-ffi-config-cli.path = \"cli\"", - ]) + .args(["--", "--offline", "--config", "include = [\".cargo/cli.toml\"]"]) .env("CARGO_TARGET_DIR", root.join("build")) .output() .unwrap(); From fa2fa80d9b037475992c35494235210b995e3460 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:24:16 -0700 Subject: [PATCH 04/10] fix(ffi): key carried patches by source URL, as cargo resolves them Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ffi/src/cli.rs | 92 ++++++++++++++++++++++++++++++++----------- ffi/tests/fixtures.rs | 8 ++-- 2 files changed, 74 insertions(+), 26 deletions(-) diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index fa3907391..c9d1b3637 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -176,34 +176,51 @@ fn patches(root: &Path, dir: &Path, picks: &Picks<'_>) -> Result "cargo's config overrides {paths} by `paths`, which no manifest can, so the export's library would build them unpatched" )); } - let patch = patches.entry("patch").or_insert_with(|| toml::Table::new().into()); - let Some(patch) = patch.as_table_mut() else { - return Err(format!("{}: `patch` is not a table", manifest.display())); + let table = |table: Option| match table { + Some(toml::Value::Table(table)) => table, + _ => toml::Table::new(), }; - for (key, deps) in config - .get("patch") - .and_then(toml::Value::as_table) - .into_iter() - .flatten() - { - // Into the manifest's table of the same source, however it spells it. - let source = source(key, &config); - let into = (patch.keys()) - .find(|k| self::source(k, &config) == source) - .cloned() - .unwrap_or_else(|| key.clone()); - let into = patch.entry(into).or_insert_with(|| toml::Table::new().into()); - let (Some(into), Some(deps)) = (into.as_table_mut(), deps.as_table()) else { - return Err(format!("`patch.{key}` is not a table")); - }; - into.extend(deps.clone()); - } - if patch.is_empty() { - patches.remove("patch"); + let manifest_patch = table(patches.remove("patch")); + let patch = combine(&manifest_patch, &table(config.get("patch").cloned()), &config)?; + if !patch.is_empty() { + patches.insert("patch".to_string(), patch.into()); } toml::to_string(&patches).map_err(|err| format!("writing the patches of {}: {err}", manifest.display())) } +/// The `[patch]` cargo resolves from a manifest's and its config's, keyed by each +/// source's URL, which needs no config to read: the config's dependency of a name +/// outranks the manifest's of that name in the same source. +fn combine(manifest: &toml::Table, patch: &toml::Table, config: &toml::Table) -> Result { + let mut combined = by_source(patch, config)?; + for (url, deps) in by_source(manifest, config)? { + match combined.get_mut(&url) { + Some(toml::Value::Table(into)) => { + for (name, dep) in deps.as_table().into_iter().flatten() { + into.entry(name).or_insert_with(|| dep.clone()); + } + } + _ => { + combined.insert(url, deps); + } + } + } + Ok(combined) +} + +/// A `[patch]` keyed by each source's URL, where a later key of the same source replaces +/// an earlier one's table whole, as cargo does. +fn by_source(patch: &toml::Table, config: &toml::Table) -> Result { + let mut by_source = toml::Table::new(); + for (key, deps) in patch { + if !deps.is_table() { + return Err(format!("`patch.{key}` is not a table")); + } + by_source.insert(source(key, config), deps.clone()); + } + Ok(by_source) +} + /// The source a `[patch.]` names, as cargo resolves it: crates.io's index or a /// configured registry's by name, and otherwise the URL itself. fn source(key: &str, config: &toml::Table) -> String { @@ -544,6 +561,35 @@ mod tests { assert_eq!(overrides(&manifest, Path::new("/ws")), expected); } + #[test] + fn combines_patches_by_source() { + let index = "https://github.com/rust-lang/crates.io-index"; + let config: toml::Table = format!( + "[registries.mirror]\nindex = \"{index}\"\n\n\ + [registries.other]\nindex = \"https://example.com/index\"\n\n\ + [patch.crates-io]\nlost = {{ path = \"/lost\" }}\n\n\ + [patch.mirror]\na = {{ path = \"/config\" }}\n\n\ + [patch.other]\nc = {{ path = \"/other\" }}\n" + ) + .parse() + .unwrap(); + let manifest: toml::Table = + format!("[patch.'{index}']\na = {{ path = \"/manifest\" }}\nb = {{ path = \"/manifest\" }}\n") + .parse() + .unwrap(); + let patch = config["patch"].as_table().unwrap(); + // A config's later table of a source, by key, replaces an earlier one whole, a config's + // dependency outranks the manifest's of its name, and only URLs name sources. + let expected: toml::Table = format!( + "[patch.'{index}']\na = {{ path = \"/config\" }}\nb = {{ path = \"/manifest\" }}\n\n\ + [patch.'https://example.com/index']\nc = {{ path = \"/other\" }}\n" + ) + .parse() + .unwrap(); + let combined = combine(manifest["patch"].as_table().unwrap(), patch, &config).unwrap(); + assert_eq!(combined, expected["patch"].as_table().unwrap().clone()); + } + #[test] fn merges_config_as_cargo() { let mut low: toml::Table = "[patch.crates-io]\na = { path = \"low\", features = [\"x\"] }\nb = \"1\"\n" diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 436b67fa7..07cb09dcf 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -528,7 +528,8 @@ fn patched() { /// as the check resolved them: an ancestor's file through its `include`, with paths from /// the directory holding its `.cargo`, outranking the manifest's patch of the same /// dependency under another spelling of its source, and a `--config` value's `include`, -/// from the package's directory. +/// from the package's directory, which names that source by a registry only the config +/// defines. #[test] fn config_patched() { let root = scratch().join("config-patched"); @@ -563,11 +564,12 @@ fn config_patched() { write(".cargo/config.toml", "include = [\"patch.toml\"]\n"); write( ".cargo/patch.toml", - "[patch.crates-io]\nrust2-ffi-config-dep = { path = \"pkg/dep\" }\n", + "[patch.index]\nrust2-ffi-config-dep = { path = \"pkg/dep\" }\n", ); write( "pkg/.cargo/cli.toml", - "[patch.crates-io]\nrust2-ffi-config-cli = { path = \"cli\" }\n", + "[registries.index]\nindex = \"https://github.com/rust-lang/crates.io-index\"\n\n\ + [patch.index]\nrust2-ffi-config-cli = { path = \"cli\" }\n", ); write( "pkg/Cargo.toml", From 0efebfbb613ef6559dca9eafb673e6cb1a3a2258 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:35:55 -0700 Subject: [PATCH 05/10] refactor(ffi): read carried patches off cargo's resolution Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ffi/README.md | 6 +- ffi/src/cli.rs | 363 +++++++++++++----------------------------- ffi/src/config.rs | 4 +- ffi/src/glue.rs | 4 +- ffi/tests/fixtures.rs | 15 +- quest/m4/fork.md | 7 - 6 files changed, 124 insertions(+), 275 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 88c5dd3bc..ec484995b 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -19,9 +19,9 @@ libraries, its runtime beside it, and the header in `include/.h`; `cargo on that crate makes a static and a shared library. That crate enables each export's features as the check did, including `--features /`; such a feature on a dependency that is no export is an error. It resolves them as the check did, wherever it -is built: its manifest carries the workspace's `[patch]` and `[replace]`, and the `[patch]` -of cargo's config where the check ran, from its files and `--config`, which outranks the -workspace's. Cargo's config `paths`, which no manifest can carry, is an error. +is built: its manifest carries the workspace's `[replace]`, and each patch cargo applied +to what the library builds with, read off cargo's resolution, wherever it was declared +(the manifest, cargo's config, `--config`, or the environment), keyed by its source's URL. ```toml # ffi.toml, beside the package's Cargo.toml unless --manifest-path says otherwise. diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index c9d1b3637..a6551477f 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -58,7 +58,7 @@ fn try_run(args: &[String]) -> Result { } /// Each export's package, as [`config::env::CRATES`] lists them: the configured package -/// first. Then its workspace's patches, as [`config::env::PATCH`] has them. +/// first. Then the patches it resolved with, as [`config::env::PATCH`] has them. fn crates(manifest: &Path, config: &Config, cargo: &[String]) -> Result<(String, String), String> { let picks = Picks::parse(cargo)?; // On the host, so the resolved dependencies are the ones the check passes rustc. @@ -159,219 +159,130 @@ fn crates(manifest: &Path, config: &Config, cargo: &[String]) -> Result<(String, } lines.push_str(&package.line()); } - let dir = manifest.parent().expect("a manifest path has a parent"); - Ok((lines, patches(&metadata.workspace_root, dir, &picks)?)) + Ok((lines, overrides(&metadata, top)?)) } -/// The workspace's `[patch]` and `[replace]` tables, and the `[patch]` of cargo's config -/// where the check runs, in `dir`, which outranks the workspace's a dependency at a time: -/// each path absolute, so the glue's own workspace, built from anywhere, resolves the -/// exports as the check does. -fn patches(root: &Path, dir: &Path, picks: &Picks<'_>) -> Result { - let manifest = root.join("Cargo.toml"); - let mut patches = overrides(&read_toml(&manifest)?, root); - let config = config(dir, picks)?; - if let Some(paths) = config.get("paths") { - return Err(format!( - "cargo's config overrides {paths} by `paths`, which no manifest can, so the export's library would build them unpatched" - )); +/// The `[patch]` and `[replace]` the glue's workspace resolves the exports by as the +/// check did, wherever it builds, as TOML with each path absolute: the workspace's +/// `[replace]`, and each patch cargo applied to what `top` builds with. +fn overrides(metadata: &Metadata, top: &Package) -> Result { + let manifest = metadata.workspace_root.join("Cargo.toml"); + let text = std::fs::read_to_string(&manifest).map_err(|err| format!("reading {}: {err}", manifest.display()))?; + let table: toml::Table = text + .parse() + .map_err(|err| format!("parsing {}: {err}", manifest.display()))?; + let mut overrides = toml::Table::new(); + if let Some(replace) = replace(&table, &metadata.workspace_root) { + overrides.insert("replace".to_string(), replace.into()); } - let table = |table: Option| match table { - Some(toml::Value::Table(table)) => table, - _ => toml::Table::new(), - }; - let manifest_patch = table(patches.remove("patch")); - let patch = combine(&manifest_patch, &table(config.get("patch").cloned()), &config)?; + let patch = patch(metadata, top)?; if !patch.is_empty() { - patches.insert("patch".to_string(), patch.into()); + overrides.insert("patch".to_string(), patch.into()); } - toml::to_string(&patches).map_err(|err| format!("writing the patches of {}: {err}", manifest.display())) + toml::to_string(&overrides).map_err(|err| format!("writing the patches of {}: {err}", manifest.display())) } -/// The `[patch]` cargo resolves from a manifest's and its config's, keyed by each -/// source's URL, which needs no config to read: the config's dependency of a name -/// outranks the manifest's of that name in the same source. -fn combine(manifest: &toml::Table, patch: &toml::Table, config: &toml::Table) -> Result { - let mut combined = by_source(patch, config)?; - for (url, deps) in by_source(manifest, config)? { - match combined.get_mut(&url) { - Some(toml::Value::Table(into)) => { - for (name, dep) in deps.as_table().into_iter().flatten() { - into.entry(name).or_insert_with(|| dep.clone()); - } +/// Each patch cargo applied in resolving what `top` builds with, wherever it was +/// declared: the manifest, cargo's config, `--config`, or the environment. A dependency +/// resolved to a package of another source than the one it names was patched; it is +/// keyed by the URL of the source it names, which reads the same without that config. +fn patch(metadata: &Metadata, top: &Package) -> Result { + let packages: BTreeMap<&str, &Package> = metadata.packages.iter().map(|p| (p.id.as_str(), p)).collect(); + let nodes: BTreeMap<&str, &Node> = metadata.resolve.nodes.iter().map(|n| (n.id.as_str(), n)).collect(); + let mut patch = toml::Table::new(); + let mut seen = BTreeSet::new(); + let mut queue = vec![top.id.as_str()]; + while let Some(id) = queue.pop() { + let (true, Some(package), Some(node)) = (seen.insert(id), packages.get(id), nodes.get(id)) else { + continue; + }; + // What the library builds, not what tests do. + let built = |kind: &Option| kind.as_deref() != Some("dev"); + for dep in node.deps.iter().filter(|d| d.dep_kinds.iter().any(|k| built(&k.kind))) { + queue.push(&dep.pkg); + let Some(resolved) = packages.get(dep.pkg.as_str()) else { + continue; + }; + let named = (package.dependencies.iter()).find(|d| { + built(&d.kind) + && d.name == resolved.name + && d.rename.as_deref().unwrap_or(&d.name).replace('-', "_") == dep.name + }); + // A path dependency names no source to patch. + let Some(named) = named.and_then(|d| d.source.as_deref()) else { + continue; + }; + // A git package's source adds the commit it resolved to. + let source = resolved.source.as_deref().map(|s| s.split('#').next().unwrap_or(s)); + if source == Some(named) { + continue; } - _ => { - combined.insert(url, deps); + let replacement = match resolved.source.as_deref() { + None => { + let dir = resolved.manifest_path.parent().expect("a manifest path has a parent"); + toml::toml! { path = (dir.display().to_string()) } + } + Some(git) if git.starts_with("git+") => { + let (url, rev) = git["git+".len()..].split_once('#').unwrap_or((git, "")); + let url = url.split('?').next().unwrap_or(url); + toml::toml! { git = url rev = rev } + } + Some(other) => { + return Err(format!( + "{} {} is patched in from {other}, which the export's library cannot patch in", + resolved.name, resolved.version + )); + } + }; + let key = patch_key(named)?; + let into = patch.entry(key).or_insert_with(|| toml::Table::new().into()); + let into = into.as_table_mut().expect("a patch's source is a table"); + match into.get(&resolved.name) { + Some(toml::Value::Table(existing)) if *existing == replacement => {} + // Two versions of one package patched in from one source. + Some(_) => { + let mut renamed = replacement; + renamed.insert("package".to_string(), resolved.name.clone().into()); + into.insert(format!("{}-{}", resolved.name, resolved.version), renamed.into()); + } + None => { + into.insert(resolved.name.clone(), replacement.into()); + } } } } - Ok(combined) -} - -/// A `[patch]` keyed by each source's URL, where a later key of the same source replaces -/// an earlier one's table whole, as cargo does. -fn by_source(patch: &toml::Table, config: &toml::Table) -> Result { - let mut by_source = toml::Table::new(); - for (key, deps) in patch { - if !deps.is_table() { - return Err(format!("`patch.{key}` is not a table")); - } - by_source.insert(source(key, config), deps.clone()); - } - Ok(by_source) + Ok(patch) } -/// The source a `[patch.]` names, as cargo resolves it: crates.io's index or a -/// configured registry's by name, and otherwise the URL itself. -fn source(key: &str, config: &toml::Table) -> String { - if key == "crates-io" { - return "https://github.com/rust-lang/crates.io-index".to_string(); +/// The `[patch]` key of a source as `cargo metadata` spells it. +fn patch_key(source: &str) -> Result { + if source == "registry+https://github.com/rust-lang/crates.io-index" || source == "sparse+https://index.crates.io/" + { + return Ok("crates-io".to_string()); } - let index = (config.get("registries")) - .and_then(|registries| registries.get(key)) - .and_then(|registry| registry.get("index")) - .and_then(toml::Value::as_str); - index.unwrap_or(key).to_string() -} - -fn read_toml(path: &Path) -> Result { - let text = std::fs::read_to_string(path).map_err(|err| format!("reading {}: {err}", path.display()))?; - text.parse().map_err(|err| format!("parsing {}: {err}", path.display())) -} - -/// The `[patch]` and `paths` of cargo's config as cargo reads it in `dir`, each patch's -/// path absolute: `$CARGO_HOME`'s file, then each directory's from the root down to -/// `dir`, then each `--config` value, each outranking the last. -fn config(dir: &Path, picks: &Picks<'_>) -> Result { - // Cargo walks the directory it runs in, symlinks resolved. - let dir = std::fs::canonicalize(dir).map_err(|err| format!("resolving {}: {err}", dir.display()))?; - let home = match std::env::var_os("CARGO_HOME") { - Some(home) => Some(dir.join(home)), - None => std::env::home_dir().map(|home| home.join(".cargo")), - }; - let mut dirs: Vec = dir.ancestors().map(|d| d.join(".cargo")).collect(); - dirs.reverse(); - if let Some(home) = home.filter(|home| !dirs.contains(home)) { - dirs.insert(0, home); + if let Some(url) = source.strip_prefix("registry+") { + return Ok(url.to_string()); } - let mut config = toml::Table::new(); - for dir in dirs { - // The name without `.toml` wins where both are. - let file = ["config", "config.toml"] - .into_iter() - .map(|name| dir.join(name)) - .find(|f| f.is_file()); - if let Some(file) = file { - merge(&mut config, load(&file)?); - } + if source.starts_with("sparse+") { + return Ok(source.to_string()); } - for value in picks.configs() { - // A value naming a file is that file, and otherwise a TOML key and value, whose - // paths are from where cargo runs. - let file = dir.join(value); - let table = match !value.is_empty() && file.exists() { - true => load(&file)?, - false => { - let origin = format!("--config {value}"); - let table = value.parse().map_err(|err| format!("parsing {origin}: {err}"))?; - expand(table, &dir, &dir, &origin)? - } - }; - merge(&mut config, table); + if let Some(url) = source.strip_prefix("git+") { + return Ok(url.split(['?', '#']).next().unwrap_or(url).to_string()); } - config.retain(|key, _| matches!(key, "patch" | "paths" | "registries")); - Ok(config) -} - -/// A cargo config file's values, as [`expand`] has them: its `include`s from its own -/// directory, and its patches' paths from the directory holding that. -fn load(file: &Path) -> Result { - let dir = file.parent().expect("a file has a parent"); - expand( - read_toml(file)?, - dir, - dir.parent().unwrap_or(dir), - &file.display().to_string(), - ) + Err(format!("a patch of {source}, which the export's library cannot patch")) } -/// A config's values with its `include`s merged in beneath them, in order, each path in -/// `dir`, and each patch's path made absolute from `root`. `origin` names it in errors. -fn expand(mut table: toml::Table, dir: &Path, root: &Path, origin: &str) -> Result { - let includes = match table.remove("include") { - None => Vec::new(), - Some(toml::Value::String(path)) => vec![toml::Value::String(path)], - Some(toml::Value::Array(includes)) => includes, - Some(_) => return Err(format!("{origin}: `include` is not a path or a list")), - }; - let mut config = toml::Table::new(); - for include in includes { - let (path, optional) = match &include { - toml::Value::String(path) => (Some(path.as_str()), false), - toml::Value::Table(table) => ( - table.get("path").and_then(toml::Value::as_str), - table.get("optional").and_then(toml::Value::as_bool).unwrap_or(false), - ), - _ => (None, false), - }; - let Some(path) = path else { - return Err(format!("{origin}: an `include` names no path: {include}")); - }; - let path = dir.join(path); - if optional && !path.exists() { - continue; - } - merge(&mut config, load(&path)?); - } - // Cargo reads no `[replace]` from its config. - if let Some(patch) = overrides(&table, root).remove("patch") { - table.insert("patch".to_string(), patch); - } - merge(&mut config, table); - Ok(config) -} - -/// Merge `high` into `low` as cargo merges its config: tables key by key, arrays joined -/// with `high`'s last, and any other value `high`'s. -fn merge(low: &mut toml::Table, high: toml::Table) { - for (key, high) in high { - match (low.get_mut(&key), high) { - (Some(toml::Value::Table(low)), toml::Value::Table(high)) => merge(low, high), - (Some(toml::Value::Array(low)), toml::Value::Array(high)) => low.extend(high), - (_, high) => { - low.insert(key, high); - } - } - } -} - -/// A manifest's `[patch.]` and `[replace]` dependencies, each path made absolute -/// from `root`, the manifest's directory. -fn overrides(manifest: &toml::Table, root: &Path) -> toml::Table { - let mut overrides = manifest.clone(); - overrides.retain(|key, _| key == "patch" || key == "replace"); - let mut deps: Vec<&mut toml::Value> = Vec::new(); - for (key, value) in overrides.iter_mut() { - let Some(table) = value.as_table_mut() else { - continue; - }; - match key.as_str() { - // A table of dependencies for each source. - "patch" => deps.extend( - (table.iter_mut()) - .filter_map(|(_, source)| source.as_table_mut()) - .flat_map(|source| source.iter_mut().map(|(_, dep)| dep)), - ), - _ => deps.extend(table.iter_mut().map(|(_, dep)| dep)), - } - } +/// A manifest's `[replace]` dependencies, each path made absolute from `root`, the +/// manifest's directory. +fn replace(manifest: &toml::Table, root: &Path) -> Option { + let mut replace = manifest.get("replace")?.as_table()?.clone(); // A dependency's own table: a bare string is a version requirement. - for dep in deps { + for (_, dep) in replace.iter_mut() { if let Some(toml::Value::String(path)) = dep.get_mut("path") { *path = root.join(path.as_str()).display().to_string(); } } - overrides + Some(replace) } /// What a cargo subcommand prints for the package, run as the check resolves: from the @@ -432,19 +343,6 @@ impl<'a> Picks<'a> { Ok(picks) } - /// Each `--config` value, in order. - fn configs(&self) -> Vec<&'a str> { - let mut values = Vec::new(); - let mut flags = self.flags.iter().copied(); - while let Some(flag) = flags.next() { - match flag { - "--config" => values.extend(flags.next()), - _ => values.extend(flag.strip_prefix("--config=")), - } - } - values - } - /// The same picks, as cargo args. fn args(&self) -> Vec { let mut args: Vec = self.flags.iter().map(|f| f.to_string()).collect(); @@ -534,6 +432,8 @@ struct Dependency { rename: Option, /// `dev` or `build`, or none for a normal dependency. kind: Option, + /// The source it names, such as `registry+`, or none for a path dependency. + source: Option, } #[derive(serde::Deserialize)] @@ -547,63 +447,15 @@ mod tests { use super::*; #[test] - fn absolutes_override_paths() { + fn absolutes_replace_paths() { let manifest: toml::Table = "[dependencies]\nx = { path = \"x\" }\n\n\ - [patch.'https://example.com/git']\npath = \"1\"\nreal = { path = \"real\" }\n\n\ - [replace]\n\"a:1.0.0\" = { path = \"a\" }\n" - .parse() - .unwrap(); - let expected: toml::Table = - "[patch.'https://example.com/git']\npath = \"1\"\nreal = { path = \"/ws/real\" }\n\n\ - [replace]\n\"a:1.0.0\" = { path = \"/ws/a\" }\n" - .parse() - .unwrap(); - assert_eq!(overrides(&manifest, Path::new("/ws")), expected); - } - - #[test] - fn combines_patches_by_source() { - let index = "https://github.com/rust-lang/crates.io-index"; - let config: toml::Table = format!( - "[registries.mirror]\nindex = \"{index}\"\n\n\ - [registries.other]\nindex = \"https://example.com/index\"\n\n\ - [patch.crates-io]\nlost = {{ path = \"/lost\" }}\n\n\ - [patch.mirror]\na = {{ path = \"/config\" }}\n\n\ - [patch.other]\nc = {{ path = \"/other\" }}\n" - ) - .parse() - .unwrap(); - let manifest: toml::Table = - format!("[patch.'{index}']\na = {{ path = \"/manifest\" }}\nb = {{ path = \"/manifest\" }}\n") - .parse() - .unwrap(); - let patch = config["patch"].as_table().unwrap(); - // A config's later table of a source, by key, replaces an earlier one whole, a config's - // dependency outranks the manifest's of its name, and only URLs name sources. - let expected: toml::Table = format!( - "[patch.'{index}']\na = {{ path = \"/config\" }}\nb = {{ path = \"/manifest\" }}\n\n\ - [patch.'https://example.com/index']\nc = {{ path = \"/other\" }}\n" - ) - .parse() - .unwrap(); - let combined = combine(manifest["patch"].as_table().unwrap(), patch, &config).unwrap(); - assert_eq!(combined, expected["patch"].as_table().unwrap().clone()); - } - - #[test] - fn merges_config_as_cargo() { - let mut low: toml::Table = "[patch.crates-io]\na = { path = \"low\", features = [\"x\"] }\nb = \"1\"\n" + [replace]\n\"a:1.0.0\" = { path = \"a\" }\n\"b:1.0.0\" = \"1\"\n" .parse() .unwrap(); - let high: toml::Table = "[patch.crates-io]\na = { path = \"high\", features = [\"y\"] }\nc = \"2\"\n" + let expected: toml::Table = "\"a:1.0.0\" = { path = \"/ws/a\" }\n\"b:1.0.0\" = \"1\"\n" .parse() .unwrap(); - merge(&mut low, high); - let expected: toml::Table = - "[patch.crates-io]\na = { path = \"high\", features = [\"x\", \"y\"] }\nb = \"1\"\nc = \"2\"\n" - .parse() - .unwrap(); - assert_eq!(low, expected); + assert_eq!(replace(&manifest, Path::new("/ws")), Some(expected)); } #[test] @@ -633,7 +485,6 @@ mod tests { let cargo: Vec = cargo.iter().map(|s| s.to_string()).collect(); let flags = ["--all-features", "--locked", "--config", "a=1", "--config=b=2"]; assert_eq!(Picks::parse(&cargo).unwrap().args(), flags); - assert_eq!(Picks::parse(&cargo).unwrap().configs(), ["a=1", "b=2"]); for target in [ &["--target", "wasm32-unknown-unknown"][..], &["--target=wasm32-unknown-unknown"], diff --git a/ffi/src/config.rs b/ffi/src/config.rs index e0ca55008..644573a4e 100644 --- a/ffi/src/config.rs +++ b/ffi/src/config.rs @@ -116,7 +116,7 @@ pub mod env { pub const CENSUS: &str = "RUST2_FFI_CENSUS"; /// Each export's [`Package`](super::Package), a line each. pub const CRATES: &str = "RUST2_FFI_CRATES"; - /// The configured workspace's `[patch]` and `[replace]` tables, as TOML with each - /// path absolute. + /// The `[patch]` cargo applied to the configured package and its workspace's + /// `[replace]`, as TOML with each path absolute. pub const PATCH: &str = "RUST2_FFI_PATCH"; } diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 16645a77a..f4345d58f 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -47,8 +47,8 @@ pub struct Crate<'a> { pub abi: &'a Abi<'a>, /// The exported packages, the configured one first, each a dependency of the crate. pub exports: &'a [Package], - /// The configured workspace's `[patch]` and `[replace]` tables, which the crate's own - /// workspace resolves the exports with. + /// The `[patch]` and `[replace]` tables the crate's own workspace resolves the exports + /// with, as the check did. pub patch: &'a str, } diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 07cb09dcf..b8f9f1f27 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -528,8 +528,8 @@ fn patched() { /// as the check resolved them: an ancestor's file through its `include`, with paths from /// the directory holding its `.cargo`, outranking the manifest's patch of the same /// dependency under another spelling of its source, and a `--config` value's `include`, -/// from the package's directory, which names that source by a registry only the config -/// defines. +/// from the package's directory. Both name that source by a registry only the config +/// defines, whose index the environment overrides. #[test] fn config_patched() { let root = scratch().join("config-patched"); @@ -564,12 +564,12 @@ fn config_patched() { write(".cargo/config.toml", "include = [\"patch.toml\"]\n"); write( ".cargo/patch.toml", - "[patch.index]\nrust2-ffi-config-dep = { path = \"pkg/dep\" }\n", + "[registries.index]\nindex = \"https://example.com/index\"\n\n\ + [patch.index]\nrust2-ffi-config-dep = { path = \"pkg/dep\" }\n", ); write( "pkg/.cargo/cli.toml", - "[registries.index]\nindex = \"https://github.com/rust-lang/crates.io-index\"\n\n\ - [patch.index]\nrust2-ffi-config-cli = { path = \"cli\" }\n", + "[patch.index]\nrust2-ffi-config-cli = { path = \"cli\" }\n", ); write( "pkg/Cargo.toml", @@ -595,6 +595,11 @@ fn config_patched() { .arg(&out) .args(["--", "--offline", "--config", "include = [\".cargo/cli.toml\"]"]) .env("CARGO_TARGET_DIR", root.join("build")) + // What the check resolves `index` by, outranking its config. + .env( + "CARGO_REGISTRIES_INDEX_INDEX", + "https://github.com/rust-lang/crates.io-index", + ) .output() .unwrap(); assert!( diff --git a/quest/m4/fork.md b/quest/m4/fork.md index 1104246d4..0f7004f60 100644 --- a/quest/m4/fork.md +++ b/quest/m4/fork.md @@ -34,13 +34,6 @@ Each names what the fork changes, the measured gain on moq, and its source. semantics need their own before this counts as sound. Each skip saves at most about 56% of the edit: the dependents' share of its critical path, minus the final link ([relink](/quest/m3/relink.md)). -- **Cargo's resolved config, with each value's origin.** Expose the merged - `[patch]` cargo resolves with, or have `cargo metadata` report the patches it - applied. Stable cargo has no way to read either (`cargo config get` is unstable, - [rust-lang/cargo#9301](https://github.com/rust-lang/cargo/issues/9301)), so - rust2-ffi reimplements config discovery, `include`, merging, and relative paths - to carry config patches into its generated crate, and drifts when cargo's rules - change. Gain: no build speed; it deletes about 100 lines of `ffi/src/cli.rs`. ### Not reasons From 5f744bdbb1d2aaf887bc3b9cceadb748a48ec864 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:45:41 -0700 Subject: [PATCH 06/10] fix(ffi): match patched edges by library name and skip replaced ones Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ffi/src/cli.rs | 32 ++++++++-- ffi/tests/fixtures.rs | 140 ++++++++++++++++++++++++++++++++---------- 2 files changed, 135 insertions(+), 37 deletions(-) diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index a6551477f..3a8d62844 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -172,10 +172,11 @@ fn overrides(metadata: &Metadata, top: &Package) -> Result { .parse() .map_err(|err| format!("parsing {}: {err}", manifest.display()))?; let mut overrides = toml::Table::new(); - if let Some(replace) = replace(&table, &metadata.workspace_root) { + let replace = replace(&table, &metadata.workspace_root).unwrap_or_default(); + let patch = patch(metadata, top, &replace)?; + if !replace.is_empty() { overrides.insert("replace".to_string(), replace.into()); } - let patch = patch(metadata, top)?; if !patch.is_empty() { overrides.insert("patch".to_string(), patch.into()); } @@ -186,7 +187,7 @@ fn overrides(metadata: &Metadata, top: &Package) -> Result { /// declared: the manifest, cargo's config, `--config`, or the environment. A dependency /// resolved to a package of another source than the one it names was patched; it is /// keyed by the URL of the source it names, which reads the same without that config. -fn patch(metadata: &Metadata, top: &Package) -> Result { +fn patch(metadata: &Metadata, top: &Package, replaced: &toml::Table) -> Result { let packages: BTreeMap<&str, &Package> = metadata.packages.iter().map(|p| (p.id.as_str(), p)).collect(); let nodes: BTreeMap<&str, &Node> = metadata.resolve.nodes.iter().map(|n| (n.id.as_str(), n)).collect(); let mut patch = toml::Table::new(); @@ -203,10 +204,15 @@ fn patch(metadata: &Metadata, top: &Package) -> Result { let Some(resolved) = packages.get(dep.pkg.as_str()) else { continue; }; + // The workspace's `[replace]` already carries it. + if replaced.keys().any(|spec| replaces(spec, resolved)) { + continue; + } + // Unrenamed, the edge is named by the library's crate name. + let lib = (resolved.targets.iter()).find(|t| t.kind.iter().any(|k| k.contains("lib") || k == "proc-macro")); let named = (package.dependencies.iter()).find(|d| { - built(&d.kind) - && d.name == resolved.name - && d.rename.as_deref().unwrap_or(&d.name).replace('-', "_") == dep.name + let name = d.rename.as_deref().or(lib.map(|t| t.name.as_str())); + built(&d.kind) && d.name == resolved.name && name.map(|n| n.replace('-', "_")) == Some(dep.name.clone()) }); // A path dependency names no source to patch. let Some(named) = named.and_then(|d| d.source.as_deref()) else { @@ -254,6 +260,20 @@ fn patch(metadata: &Metadata, top: &Package) -> Result { Ok(patch) } +/// Whether a `[replace]` key, a package ID spec such as `foo:1.0.0` or +/// `https://github.com/rust-lang/crates.io-index#foo@1.0.0`, names `package`, which has +/// the version it replaces. +fn replaces(spec: &str, package: &Package) -> bool { + let spec = spec.rsplit_once('#').map_or(spec, |(_, spec)| spec); + match spec.split_once(['@', ':']) { + // A spec's version may name only its leading parts. + Some((name, version)) => { + name == package.name && (package.version == version || package.version.starts_with(&format!("{version}."))) + } + None => spec == package.name, + } +} + /// The `[patch]` key of a source as `cargo metadata` spells it. fn patch_key(source: &str) -> Result { if source == "registry+https://github.com/rust-lang/crates.io-index" || source == "sparse+https://index.crates.io/" diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index b8f9f1f27..540c2d558 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -474,41 +474,27 @@ fn lookalike() { .arg(package.join("glue"))); } -/// Another export patched in for a registry dependency is bound by its resolved package, -/// though the dependency names no path, and the glue builds it patched in too. -#[test] -fn patched() { - let package = package("patched"); - let write = |path: &str, text: &str| { - let path = package.join(path); +/// Writes each `(path, text)` into `dir`. +fn write_all(dir: &Path, files: &[(&str, &str)]) { + for (path, text) in files { + let path = dir.join(path); std::fs::create_dir_all(path.parent().unwrap()).unwrap(); std::fs::write(path, text).unwrap(); - }; - write( - "dep/Cargo.toml", - "[package]\nname = \"rust2-ffi-patched-dep\"\nversion = \"1.0.0\"\nedition = \"2024\"\n", - ); - write("dep/src/lib.rs", "pub struct Size {\n\tpub width: u32,\n}\n"); - write( - "Cargo.toml", - "[package]\nname = \"patched\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ - [dependencies]\nrust2-ffi-patched-dep = \"1\"\n\n\ - [patch.crates-io]\nrust2-ffi-patched-dep = { path = \"dep\" }\n\n\ - [workspace]\nexclude = [\"dep\"]\n", - ); - write( - "src/lib.rs", - "pub fn width(size: rust2_ffi_patched_dep::Size) -> u32 {\n\tsize.width\n}\n", - ); - write("ffi.toml", "package = \"patched\"\n\n[crates.rust2-ffi-patched-dep]\n"); - let out = package.join("out"); + } +} + +/// Export the package in `dir` with cargo `args`, then build its glue offline from +/// here, with the lockfile the check resolved. +fn export_and_build(dir: &Path, args: &[&str]) { + let out = dir.join("out"); let output = Command::new(env!("CARGO_BIN_EXE_rust2-ffi")) .arg("--config") - .arg(package.join("ffi.toml")) + .arg(dir.join("ffi.toml")) .arg("--out") .arg(&out) - .args(["--", "--offline"]) - .env("CARGO_TARGET_DIR", package.join("build")) + .arg("--") + .args(args) + .env("CARGO_TARGET_DIR", dir.join("build")) .output() .unwrap(); assert!( @@ -516,12 +502,104 @@ fn patched() { "the export failed:\n{}", String::from_utf8_lossy(&output.stderr) ); - // The glue's workspace patches the export in as the configured one does. + std::fs::copy(dir.join("Cargo.lock"), out.join("Cargo.lock")).unwrap(); run(cargo() .args(["build", "--quiet", "--offline", "--manifest-path"]) .arg(out.join("Cargo.toml")) .arg("--target-dir") - .arg(package.join("glue"))); + .arg(dir.join("glue"))); +} + +/// Another export patched in for a registry dependency is bound by its resolved package, +/// though the dependency names no path, and the glue builds it patched in too, though +/// the dependency goes by its library's crate name. +#[test] +fn patched() { + let package = package("patched"); + write_all( + &package, + &[ + ( + "dep/Cargo.toml", + "[package]\nname = \"rust2-ffi-patched-dep\"\nversion = \"1.0.0\"\nedition = \"2024\"\n\n\ + [lib]\nname = \"rust2_ffi_patched_core\"\n", + ), + ("dep/src/lib.rs", "pub struct Size {\n\tpub width: u32,\n}\n"), + ( + "Cargo.toml", + "[package]\nname = \"patched\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ + [dependencies]\nrust2-ffi-patched-dep = \"1\"\n\n\ + [patch.crates-io]\nrust2-ffi-patched-dep = { path = \"dep\" }\n\n\ + [workspace]\nexclude = [\"dep\"]\n", + ), + ( + "src/lib.rs", + "pub fn width(size: rust2_ffi_patched_core::Size) -> u32 {\n\tsize.width\n}\n", + ), + ("ffi.toml", "package = \"patched\"\n\n[crates.rust2-ffi-patched-dep]\n"), + ], + ); + export_and_build(&package, &["--offline"]); +} + +/// Another export the workspace `[replace]`s is carried by that `[replace]` alone, which +/// cargo refuses beside a `[patch]` of it. +#[test] +fn replaced() { + let package = package("replaced"); + // A git repository the dependency names, whose copy lacks the field the package reads. + let manifest = "[package]\nname = \"rust2-ffi-replaced-dep\"\nversion = \"1.0.0\"\nedition = \"2024\"\n"; + let git = package.join("git"); + write_all( + &git, + &[ + ("Cargo.toml", manifest), + ("src/lib.rs", "pub struct Size {\n\tpub height: u32,\n}\n"), + ], + ); + let _ = std::fs::remove_dir_all(git.join(".git")); + for args in [ + &["init", "--quiet"][..], + &["add", "."], + &[ + "-c", + "user.name=t", + "-c", + "user.email=t@t", + "commit", + "--quiet", + "-m", + "dep", + ], + ] { + run(Command::new("git").args(args).current_dir(&git)); + } + let url = format!("file://{}", git.display()); + write_all( + &package, + &[ + ("dep/Cargo.toml", manifest), + ("dep/src/lib.rs", "pub struct Size {\n\tpub width: u32,\n}\n"), + ( + "Cargo.toml", + &format!( + "[package]\nname = \"replaced\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ + [dependencies]\nrust2-ffi-replaced-dep = {{ git = \"{url}\" }}\n\n\ + [replace]\n\"{url}#rust2-ffi-replaced-dep@1.0.0\" = {{ path = \"dep\" }}\n\n\ + [workspace]\nexclude = [\"dep\", \"git\"]\n" + ), + ), + ( + "src/lib.rs", + "pub fn width(size: rust2_ffi_replaced_dep::Size) -> u32 {\n\tsize.width\n}\n", + ), + ( + "ffi.toml", + "package = \"replaced\"\n\n[crates.rust2-ffi-replaced-dep]\n", + ), + ], + ); + export_and_build(&package, &[]); } /// Patches in cargo's config, which cargo reads only where the check runs, reach the glue From 4ba7fee2fc697e04f51c969a8140065d67553499 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:56:57 -0700 Subject: [PATCH 07/10] fix(ffi): carry a [replace] as the patch cargo resolved Matching [replace] specs by hand missed cargo's spellings that name no package or carry build metadata, and then emitted both tables, which cargo refuses. A replacement keeps the package's name and version, so the [patch] read off the resolution resolves alike; carry that alone. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ffi/README.md | 2 +- ffi/src/cli.rs | 67 +++++-------------------------------------- ffi/src/config.rs | 4 +-- ffi/src/glue.rs | 4 +-- ffi/tests/fixtures.rs | 13 +++++---- 5 files changed, 19 insertions(+), 71 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index ec484995b..885c8782a 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -19,7 +19,7 @@ libraries, its runtime beside it, and the header in `include/.h`; `cargo on that crate makes a static and a shared library. That crate enables each export's features as the check did, including `--features /`; such a feature on a dependency that is no export is an error. It resolves them as the check did, wherever it -is built: its manifest carries the workspace's `[replace]`, and each patch cargo applied +is built: its manifest carries, as a `[patch]`, each patch or `[replace]` cargo applied to what the library builds with, read off cargo's resolution, wherever it was declared (the manifest, cargo's config, `--config`, or the environment), keyed by its source's URL. diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index 3a8d62844..add54924f 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -162,32 +162,23 @@ fn crates(manifest: &Path, config: &Config, cargo: &[String]) -> Result<(String, Ok((lines, overrides(&metadata, top)?)) } -/// The `[patch]` and `[replace]` the glue's workspace resolves the exports by as the -/// check did, wherever it builds, as TOML with each path absolute: the workspace's -/// `[replace]`, and each patch cargo applied to what `top` builds with. +/// The `[patch]` the glue's workspace resolves the exports by as the check did, wherever +/// it builds, as TOML with each path absolute. fn overrides(metadata: &Metadata, top: &Package) -> Result { - let manifest = metadata.workspace_root.join("Cargo.toml"); - let text = std::fs::read_to_string(&manifest).map_err(|err| format!("reading {}: {err}", manifest.display()))?; - let table: toml::Table = text - .parse() - .map_err(|err| format!("parsing {}: {err}", manifest.display()))?; + let patch = patch(metadata, top)?; let mut overrides = toml::Table::new(); - let replace = replace(&table, &metadata.workspace_root).unwrap_or_default(); - let patch = patch(metadata, top, &replace)?; - if !replace.is_empty() { - overrides.insert("replace".to_string(), replace.into()); - } if !patch.is_empty() { overrides.insert("patch".to_string(), patch.into()); } - toml::to_string(&overrides).map_err(|err| format!("writing the patches of {}: {err}", manifest.display())) + toml::to_string(&overrides).map_err(|err| format!("writing the patches: {err}")) } /// Each patch cargo applied in resolving what `top` builds with, wherever it was /// declared: the manifest, cargo's config, `--config`, or the environment. A dependency -/// resolved to a package of another source than the one it names was patched; it is +/// resolved to a package of another source than the one it names was patched, or +/// `[replace]`d by a package of the same version, which a patch resolves to alike; it is /// keyed by the URL of the source it names, which reads the same without that config. -fn patch(metadata: &Metadata, top: &Package, replaced: &toml::Table) -> Result { +fn patch(metadata: &Metadata, top: &Package) -> Result { let packages: BTreeMap<&str, &Package> = metadata.packages.iter().map(|p| (p.id.as_str(), p)).collect(); let nodes: BTreeMap<&str, &Node> = metadata.resolve.nodes.iter().map(|n| (n.id.as_str(), n)).collect(); let mut patch = toml::Table::new(); @@ -204,10 +195,6 @@ fn patch(metadata: &Metadata, top: &Package, replaced: &toml::Table) -> Result Result bool { - let spec = spec.rsplit_once('#').map_or(spec, |(_, spec)| spec); - match spec.split_once(['@', ':']) { - // A spec's version may name only its leading parts. - Some((name, version)) => { - name == package.name && (package.version == version || package.version.starts_with(&format!("{version}."))) - } - None => spec == package.name, - } -} - /// The `[patch]` key of a source as `cargo metadata` spells it. fn patch_key(source: &str) -> Result { if source == "registry+https://github.com/rust-lang/crates.io-index" || source == "sparse+https://index.crates.io/" @@ -292,19 +265,6 @@ fn patch_key(source: &str) -> Result { Err(format!("a patch of {source}, which the export's library cannot patch")) } -/// A manifest's `[replace]` dependencies, each path made absolute from `root`, the -/// manifest's directory. -fn replace(manifest: &toml::Table, root: &Path) -> Option { - let mut replace = manifest.get("replace")?.as_table()?.clone(); - // A dependency's own table: a bare string is a version requirement. - for (_, dep) in replace.iter_mut() { - if let Some(toml::Value::String(path)) = dep.get_mut("path") { - *path = root.join(path.as_str()).display().to_string(); - } - } - Some(replace) -} - /// What a cargo subcommand prints for the package, run as the check resolves: from the /// package's directory, where cargo reads its config, with the check's picks. fn run_cargo(manifest: &Path, subcommand: &[&str], picks: &Picks<'_>) -> Result, String> { @@ -404,7 +364,6 @@ impl<'a> Picks<'a> { struct Metadata { packages: Vec, resolve: Resolve, - workspace_root: PathBuf, } #[derive(serde::Deserialize)] @@ -466,18 +425,6 @@ struct Target { mod tests { use super::*; - #[test] - fn absolutes_replace_paths() { - let manifest: toml::Table = "[dependencies]\nx = { path = \"x\" }\n\n\ - [replace]\n\"a:1.0.0\" = { path = \"a\" }\n\"b:1.0.0\" = \"1\"\n" - .parse() - .unwrap(); - let expected: toml::Table = "\"a:1.0.0\" = { path = \"/ws/a\" }\n\"b:1.0.0\" = \"1\"\n" - .parse() - .unwrap(); - assert_eq!(replace(&manifest, Path::new("/ws")), Some(expected)); - } - #[test] fn reads_what_picks_a_resolution() { for spelling in [ diff --git a/ffi/src/config.rs b/ffi/src/config.rs index 644573a4e..9c9043db0 100644 --- a/ffi/src/config.rs +++ b/ffi/src/config.rs @@ -116,7 +116,7 @@ pub mod env { pub const CENSUS: &str = "RUST2_FFI_CENSUS"; /// Each export's [`Package`](super::Package), a line each. pub const CRATES: &str = "RUST2_FFI_CRATES"; - /// The `[patch]` cargo applied to the configured package and its workspace's - /// `[replace]`, as TOML with each path absolute. + /// The `[patch]` resolving the configured package as cargo did, as TOML with each + /// path absolute. pub const PATCH: &str = "RUST2_FFI_PATCH"; } diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index f4345d58f..bca9009b2 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -47,8 +47,8 @@ pub struct Crate<'a> { pub abi: &'a Abi<'a>, /// The exported packages, the configured one first, each a dependency of the crate. pub exports: &'a [Package], - /// The `[patch]` and `[replace]` tables the crate's own workspace resolves the exports - /// with, as the check did. + /// The `[patch]` table the crate's own workspace resolves the exports with, as the + /// check did. pub patch: &'a str, } diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index 540c2d558..f15f3f518 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -542,14 +542,15 @@ fn patched() { export_and_build(&package, &["--offline"]); } -/// Another export the workspace `[replace]`s is carried by that `[replace]` alone, which -/// cargo refuses beside a `[patch]` of it. +/// Another export the workspace `[replace]`s, by a spec naming no package, is carried as +/// a `[patch]` of it, which resolves alike since a replacement keeps its version. #[test] fn replaced() { let package = package("replaced"); - // A git repository the dependency names, whose copy lacks the field the package reads. + // A git repository the dependency names, whose copy lacks the field the package reads, + // named for the package so the spec names it by its URL. let manifest = "[package]\nname = \"rust2-ffi-replaced-dep\"\nversion = \"1.0.0\"\nedition = \"2024\"\n"; - let git = package.join("git"); + let git = package.join("rust2-ffi-replaced-dep"); write_all( &git, &[ @@ -585,8 +586,8 @@ fn replaced() { &format!( "[package]\nname = \"replaced\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ [dependencies]\nrust2-ffi-replaced-dep = {{ git = \"{url}\" }}\n\n\ - [replace]\n\"{url}#rust2-ffi-replaced-dep@1.0.0\" = {{ path = \"dep\" }}\n\n\ - [workspace]\nexclude = [\"dep\", \"git\"]\n" + [replace]\n\"{url}#1.0.0\" = {{ path = \"dep\" }}\n\n\ + [workspace]\nexclude = [\"dep\", \"rust2-ffi-replaced-dep\"]\n" ), ), ( From 29fa71e2a57379726f2cd619c81dabf966d0297b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:06:34 -0700 Subject: [PATCH 08/10] fix(ffi): refuse a [replace] from the source it replaces A replacement by another commit of the git source the dependency names became a [patch] from that same source, which cargo refuses, so the check passed while the glue could not build. No [patch] can carry it, so it is an error. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ffi/README.md | 1 + ffi/src/cli.rs | 10 ++++- ffi/tests/fixtures.rs | 95 ++++++++++++++++++++++++++----------------- 3 files changed, 68 insertions(+), 38 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 885c8782a..5d4901bde 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -22,6 +22,7 @@ dependency that is no export is an error. It resolves them as the check did, whe is built: its manifest carries, as a `[patch]`, each patch or `[replace]` cargo applied to what the library builds with, read off cargo's resolution, wherever it was declared (the manifest, cargo's config, `--config`, or the environment), keyed by its source's URL. +A `[replace]` from the source it replaces, which no `[patch]` can carry, is an error. ```toml # ffi.toml, beside the package's Cargo.toml unless --manifest-path says otherwise. diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index add54924f..269b91d9e 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -210,6 +210,7 @@ fn patch(metadata: &Metadata, top: &Package) -> Result { if source == Some(named) { continue; } + let key = patch_key(named)?; let replacement = match resolved.source.as_deref() { None => { let dir = resolved.manifest_path.parent().expect("a manifest path has a parent"); @@ -218,6 +219,14 @@ fn patch(metadata: &Metadata, top: &Package) -> Result { Some(git) if git.starts_with("git+") => { let (url, rev) = git["git+".len()..].split_once('#').unwrap_or((git, "")); let url = url.split('?').next().unwrap_or(url); + // Only a `[replace]` resolves a package to another commit of the source + // the dependency names; cargo refuses a patch from that source. + if url == key { + return Err(format!( + "{} {} is replaced from its own source, which a `[patch]` of the export's library cannot carry", + resolved.name, resolved.version + )); + } toml::toml! { git = url rev = rev } } Some(other) => { @@ -227,7 +236,6 @@ fn patch(metadata: &Metadata, top: &Package) -> Result { )); } }; - let key = patch_key(named)?; let into = patch.entry(key).or_insert_with(|| toml::Table::new().into()); let into = into.as_table_mut().expect("a patch's source is a table"); match into.get(&resolved.name) { diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index f15f3f518..f9400c4e0 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -543,14 +543,26 @@ fn patched() { } /// Another export the workspace `[replace]`s, by a spec naming no package, is carried as -/// a `[patch]` of it, which resolves alike since a replacement keeps its version. +/// a `[patch]` of it, which resolves alike since a replacement keeps its version. A +/// replacement from the source it replaces is an error, since no patch can carry it. #[test] fn replaced() { let package = package("replaced"); - // A git repository the dependency names, whose copy lacks the field the package reads, - // named for the package so the spec names it by its URL. + // A git repository the dependency names, whose default branch lacks the field the + // package reads, named for the package so the spec names it by its URL. let manifest = "[package]\nname = \"rust2-ffi-replaced-dep\"\nversion = \"1.0.0\"\nedition = \"2024\"\n"; let git = package.join("rust2-ffi-replaced-dep"); + let _ = std::fs::remove_dir_all(&git); + let commit = [ + "-c", + "user.name=t", + "-c", + "user.email=t@t", + "commit", + "--quiet", + "-am", + "dep", + ]; write_all( &git, &[ @@ -558,49 +570,58 @@ fn replaced() { ("src/lib.rs", "pub struct Size {\n\tpub height: u32,\n}\n"), ], ); - let _ = std::fs::remove_dir_all(git.join(".git")); for args in [ - &["init", "--quiet"][..], + &["init", "--quiet", "--initial-branch", "main"][..], &["add", "."], - &[ - "-c", - "user.name=t", - "-c", - "user.email=t@t", - "commit", - "--quiet", - "-m", - "dep", - ], + &commit, + &["checkout", "--quiet", "-b", "fixed"], ] { run(Command::new("git").args(args).current_dir(&git)); } + write_all(&git, &[("src/lib.rs", "pub struct Size {\n\tpub width: u32,\n}\n")]); + run(Command::new("git").args(commit).current_dir(&git)); let url = format!("file://{}", git.display()); + let files = |replacement: &str| { + write_all( + &package, + &[ + ("dep/Cargo.toml", manifest), + ("dep/src/lib.rs", "pub struct Size {\n\tpub width: u32,\n}\n"), + ( + "Cargo.toml", + &format!( + "[package]\nname = \"replaced\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ + [dependencies]\nrust2-ffi-replaced-dep = {{ git = \"{url}\", branch = \"main\" }}\n\n\ + [replace]\n\"{url}#1.0.0\" = {replacement}\n\n\ + [workspace]\nexclude = [\"dep\", \"rust2-ffi-replaced-dep\"]\n" + ), + ), + ( + "src/lib.rs", + "pub fn width(size: rust2_ffi_replaced_dep::Size) -> u32 {\n\tsize.width\n}\n", + ), + ( + "ffi.toml", + "package = \"replaced\"\n\n[crates.rust2-ffi-replaced-dep]\n", + ), + ], + ); + }; + files("{ path = \"dep\" }"); + export_and_build(&package, &[]); + // Not an export, which is a path package, but what the package builds with. + files(&format!("{{ git = \"{url}\", branch = \"fixed\" }}")); write_all( &package, - &[ - ("dep/Cargo.toml", manifest), - ("dep/src/lib.rs", "pub struct Size {\n\tpub width: u32,\n}\n"), - ( - "Cargo.toml", - &format!( - "[package]\nname = \"replaced\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ - [dependencies]\nrust2-ffi-replaced-dep = {{ git = \"{url}\" }}\n\n\ - [replace]\n\"{url}#1.0.0\" = {{ path = \"dep\" }}\n\n\ - [workspace]\nexclude = [\"dep\", \"rust2-ffi-replaced-dep\"]\n" - ), - ), - ( - "src/lib.rs", - "pub fn width(size: rust2_ffi_replaced_dep::Size) -> u32 {\n\tsize.width\n}\n", - ), - ( - "ffi.toml", - "package = \"replaced\"\n\n[crates.rust2-ffi-replaced-dep]\n", - ), - ], + &[("ffi.toml", "package = \"replaced\"\nexclude = [\"width\"]\n")], + ); + let output = census(&package.join("ffi.toml"), &[]).output().unwrap(); + assert!(!output.status.success(), "a replacement from its own source passed"); + let stderr = String::from_utf8_lossy(&output.stderr); + assert!( + stderr.contains("rust2-ffi-replaced-dep 1.0.0 is replaced from its own source"), + "{stderr}" ); - export_and_build(&package, &[]); } /// Patches in cargo's config, which cargo reads only where the check runs, reach the glue From 3615f9e0ad73929ba7352a128a6f6d51a61af8c7 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:15:04 -0700 Subject: [PATCH 09/10] fix(ffi): carry the patches of every platform's dependencies The patches were read off metadata filtered to the host, but cargo resolves every platform's dependencies when it builds the glue, so a patch of a dependency of another platform was lost and the glue failed to resolve offline. Read them off the unfiltered resolution. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ffi/README.md | 5 +++-- ffi/src/cli.rs | 15 +++++++++------ ffi/tests/fixtures.rs | 14 +++++++++++--- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 5d4901bde..a7579ff7d 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -20,8 +20,9 @@ on that crate makes a static and a shared library. That crate enables each expor features as the check did, including `--features /`; such a feature on a dependency that is no export is an error. It resolves them as the check did, wherever it is built: its manifest carries, as a `[patch]`, each patch or `[replace]` cargo applied -to what the library builds with, read off cargo's resolution, wherever it was declared -(the manifest, cargo's config, `--config`, or the environment), keyed by its source's URL. +to what the library builds with on any platform, read off cargo's resolution, wherever +it was declared (the manifest, cargo's config, `--config`, or the environment), keyed by +its source's URL. A `[replace]` from the source it replaces, which no `[patch]` can carry, is an error. ```toml diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index 269b91d9e..4589bf882 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -159,7 +159,10 @@ fn crates(manifest: &Path, config: &Config, cargo: &[String]) -> Result<(String, } lines.push_str(&package.line()); } - Ok((lines, overrides(&metadata, top)?)) + // Every platform's dependencies, which cargo resolves wherever the glue builds. + let output = run_cargo(manifest, &["metadata", "--format-version", "1"], &picks)?; + let resolved: Metadata = serde_json::from_slice(&output).map_err(|err| format!("reading cargo metadata: {err}"))?; + Ok((lines, overrides(&resolved, top)?)) } /// The `[patch]` the glue's workspace resolves the exports by as the check did, wherever @@ -173,11 +176,11 @@ fn overrides(metadata: &Metadata, top: &Package) -> Result { toml::to_string(&overrides).map_err(|err| format!("writing the patches: {err}")) } -/// Each patch cargo applied in resolving what `top` builds with, wherever it was -/// declared: the manifest, cargo's config, `--config`, or the environment. A dependency -/// resolved to a package of another source than the one it names was patched, or -/// `[replace]`d by a package of the same version, which a patch resolves to alike; it is -/// keyed by the URL of the source it names, which reads the same without that config. +/// Each patch cargo applied in resolving what `top` builds with on any platform, wherever +/// it was declared: the manifest, cargo's config, `--config`, or the environment. A +/// dependency resolved to a package of another source than the one it names was patched, +/// or `[replace]`d by a package of the same version, which a patch resolves to alike; it +/// is keyed by the URL of the source it names, which reads the same without that config. fn patch(metadata: &Metadata, top: &Package) -> Result { let packages: BTreeMap<&str, &Package> = metadata.packages.iter().map(|p| (p.id.as_str(), p)).collect(); let nodes: BTreeMap<&str, &Node> = metadata.resolve.nodes.iter().map(|n| (n.id.as_str(), n)).collect(); diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index f9400c4e0..c8a4c64aa 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -512,7 +512,8 @@ fn export_and_build(dir: &Path, args: &[&str]) { /// Another export patched in for a registry dependency is bound by its resolved package, /// though the dependency names no path, and the glue builds it patched in too, though -/// the dependency goes by its library's crate name. +/// the dependency goes by its library's crate name. So is a dependency of no platform's, +/// which cargo resolves all the same. #[test] fn patched() { let package = package("patched"); @@ -525,12 +526,19 @@ fn patched() { [lib]\nname = \"rust2_ffi_patched_core\"\n", ), ("dep/src/lib.rs", "pub struct Size {\n\tpub width: u32,\n}\n"), + ( + "inactive/Cargo.toml", + "[package]\nname = \"rust2-ffi-patched-inactive\"\nversion = \"1.0.0\"\nedition = \"2024\"\n", + ), + ("inactive/src/lib.rs", ""), ( "Cargo.toml", "[package]\nname = \"patched\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ [dependencies]\nrust2-ffi-patched-dep = \"1\"\n\n\ - [patch.crates-io]\nrust2-ffi-patched-dep = { path = \"dep\" }\n\n\ - [workspace]\nexclude = [\"dep\"]\n", + [target.'cfg(any())'.dependencies]\nrust2-ffi-patched-inactive = \"1\"\n\n\ + [patch.crates-io]\nrust2-ffi-patched-dep = { path = \"dep\" }\n\ + rust2-ffi-patched-inactive = { path = \"inactive\" }\n\n\ + [workspace]\nexclude = [\"dep\", \"inactive\"]\n", ), ( "src/lib.rs", From 5268e2c084574229e2f05bc1ed59e9d2302012ac Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:23:24 -0700 Subject: [PATCH 10/10] fix(ffi): pin a git patch to the version it resolved to A git repository may hold several versions of a package, which the original patch picks between by version. The carried patch named only the commit, so cargo found more than one candidate in the glue. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0115AT2ue6x9cT5e5J9SZtCn --- ffi/src/cli.rs | 4 ++- ffi/tests/fixtures.rs | 83 +++++++++++++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 23 deletions(-) diff --git a/ffi/src/cli.rs b/ffi/src/cli.rs index 4589bf882..10f94dd83 100644 --- a/ffi/src/cli.rs +++ b/ffi/src/cli.rs @@ -230,7 +230,9 @@ fn patch(metadata: &Metadata, top: &Package) -> Result { resolved.name, resolved.version )); } - toml::toml! { git = url rev = rev } + // A repository may hold several versions of the package. + let version = format!("={}", resolved.version); + toml::toml! { git = url rev = rev version = version } } Some(other) => { return Err(format!( diff --git a/ffi/tests/fixtures.rs b/ffi/tests/fixtures.rs index c8a4c64aa..d79d4b8ab 100644 --- a/ffi/tests/fixtures.rs +++ b/ffi/tests/fixtures.rs @@ -550,6 +550,62 @@ fn patched() { export_and_build(&package, &["--offline"]); } +/// A commit of all of `dir`'s files. +const COMMIT: [&str; 8] = [ + "-c", + "user.name=t", + "-c", + "user.email=t@t", + "commit", + "--quiet", + "-am", + "commit", +]; + +/// The URL of a new git repository in `dir`, whose branch `main` holds `files`. +fn repository(dir: &Path, files: &[(&str, &str)]) -> String { + let _ = std::fs::remove_dir_all(dir); + write_all(dir, files); + for args in [ + &["init", "--quiet", "--initial-branch", "main"][..], + &["add", "."], + &COMMIT, + ] { + run(Command::new("git").args(args).current_dir(dir)); + } + format!("file://{}", dir.display()) +} + +/// A dependency patched in from a git repository that holds two versions of it, the patch +/// picking one by version, is carried with the version it resolved to. +#[test] +fn git_patched() { + let package = package("git_patched"); + let manifest = + |version| format!("[package]\nname = \"rust2-ffi-git-dep\"\nversion = \"{version}\"\nedition = \"2024\"\n"); + let stale = repository( + &package.join("stale"), + &[("Cargo.toml", &manifest("1.0.0")), ("src/lib.rs", "")], + ); + let fresh = repository( + &package.join("fresh"), + &[ + ("one/Cargo.toml", &manifest("1.0.0")), + ("one/src/lib.rs", ""), + ("two/Cargo.toml", &manifest("2.0.0")), + ("two/src/lib.rs", ""), + ], + ); + let text = format!( + "[package]\nname = \"git_patched\"\nversion = \"0.0.0\"\nedition = \"2024\"\n\n\ + [dependencies]\nrust2-ffi-git-dep = {{ git = \"{stale}\" }}\n\n\ + [patch.\"{stale}\"]\nrust2-ffi-git-dep = {{ git = \"{fresh}\", version = \"1\" }}\n\n\ + [workspace]\nexclude = [\"stale\", \"fresh\"]\n" + ); + write_all(&package, &[("Cargo.toml", &text)]); + export_and_build(&package, &[]); +} + /// Another export the workspace `[replace]`s, by a spec naming no package, is carried as /// a `[patch]` of it, which resolves alike since a replacement keeps its version. A /// replacement from the source it replaces is an error, since no patch can carry it. @@ -560,35 +616,18 @@ fn replaced() { // package reads, named for the package so the spec names it by its URL. let manifest = "[package]\nname = \"rust2-ffi-replaced-dep\"\nversion = \"1.0.0\"\nedition = \"2024\"\n"; let git = package.join("rust2-ffi-replaced-dep"); - let _ = std::fs::remove_dir_all(&git); - let commit = [ - "-c", - "user.name=t", - "-c", - "user.email=t@t", - "commit", - "--quiet", - "-am", - "dep", - ]; - write_all( + let url = repository( &git, &[ ("Cargo.toml", manifest), ("src/lib.rs", "pub struct Size {\n\tpub height: u32,\n}\n"), ], ); - for args in [ - &["init", "--quiet", "--initial-branch", "main"][..], - &["add", "."], - &commit, - &["checkout", "--quiet", "-b", "fixed"], - ] { - run(Command::new("git").args(args).current_dir(&git)); - } + run(Command::new("git") + .args(["checkout", "--quiet", "-b", "fixed"]) + .current_dir(&git)); write_all(&git, &[("src/lib.rs", "pub struct Size {\n\tpub width: u32,\n}\n")]); - run(Command::new("git").args(commit).current_dir(&git)); - let url = format!("file://{}", git.display()); + run(Command::new("git").args(COMMIT).current_dir(&git)); let files = |replacement: &str| { write_all( &package,