Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions .github/workflows/bazel-cargo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: Bazel patch compatibility (Cargo)
# The Cargo flows of the real-Bazel tests (`cargo::` in `e2e_bazel_build`):
# hosted and vendored mode for a crate that rules_rust's crate_universe
# resolves from a Cargo workspace (`crate.from_cargo`, with the project's
# cargo config and no cargo-bazel lock), on each Bazel release below.
# cargo config and no cargo-bazel lock), and hosted mode on the hub that
# names no cargo config, on each Bazel release below.
# bazel-compatibility.yml holds the legs and what every ecosystem shares;
# this file says when the Cargo legs run and on which releases.
#
Expand All @@ -22,13 +23,15 @@ name: Bazel patch compatibility (Cargo)
# No leg asks crates.io about the crate under test. cargo-bazel's cargo
# reads no downloader config, so the test's workspace names a mirror of
# crates.io in its `.cargo/config.toml`, the loopback stand-in, and the
# crate has a name crates.io cannot hold. A crate file under test passes
# through the repository cache as well: Bazel's downloader fetches it and
# keeps it under its sha256. So a test builds the upstream crate file and
# each patched one anew, with an id of the run in the library, and writes
# the upstream checksum into the Cargo.lock of its workspace copy. No cache
# holds a crate file of the run, and the test counts the requests for the
# upstream one.
# crate has a name crates.io cannot hold. The flow of the hub without a
# cargo config names the mirror in the cargo home of the test instead, which
# that hub's cargo is run with (`CARGO_BAZEL_ISOLATED=false`). A crate file
# under test passes through the repository cache as well: Bazel's
# downloader fetches it and keeps it under its sha256. So a test builds the
# upstream crate file and each patched one anew, with an id of the run in
# the library, and writes the upstream checksum into the Cargo.lock of its
# workspace copy. No cache holds a crate file of the run, and the test
# counts the requests for the upstream one.
#
# Not required for merge: no `merge_group` trigger, and no job here is a
# `needs` of ci.yml's `ci-ok`.
Expand Down
3 changes: 2 additions & 1 deletion crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

528 changes: 515 additions & 13 deletions crates/socket-patch-cli/tests/e2e_bazel_build/cargo.rs

Large diffs are not rendered by default.

832 changes: 832 additions & 0 deletions crates/socket-patch-core/src/bazel/cargo.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/socket-patch-core/src/bazel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub const WORKSPACE_FILES: [&str; 4] =
["MODULE.bazel", "REPO.bazel", "WORKSPACE", "WORKSPACE.bazel"];

pub mod built;
pub mod cargo;
pub mod ignore;
pub mod maven;
pub mod rc;
Expand Down
98 changes: 96 additions & 2 deletions crates/socket-patch-core/src/hosted/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1449,12 +1449,29 @@ pub async fn read_candidate_files_in(
// dependencies of their own: a member's direct `cfg-if = "1"` must be
// pinned alongside the root's, or the redirected lock entry is
// unsatisfiable. Keyed `<dir>/Cargo.toml` for the cargo rewriter.
if out.files.contains_key("Cargo.toml") && candidates.iter().any(|c| c.dep.ecosystem == "cargo")
{
let cargo = candidates.iter().any(|c| c.dep.ecosystem == "cargo");
if out.files.contains_key("Cargo.toml") && cargo {
for rel in crate::utils::cargo_workspace::member_manifests_in(view).await {
out.read(view, unreadable, &rel).await;
}
}
// In a Bazel workspace on disk a rules_rust hub may run cargo over
// these files without the cargo config that defines the registry of a
// pin. What stands against a pin is read from the checkout here and
// rides a synthetic key to the cargo rewriter. A project in no Bazel
// workspace, or one that no such hub is seen to read, gets none, and
// its run is the one it was.
if let Some(root) = view.disk_root().filter(|_| cargo) {
use crate::bazel::cargo::{unread, KEY};
let member = |rel: &&String| crate::patch::redirect::is_cargo_member_manifest_key(rel);
let mut files = vec!["Cargo.lock".to_string(), "Cargo.toml".to_string()];
files.extend(out.files.keys().filter(member).cloned());
let config = crate::patch::redirect::cargo_config_key(&out.files);
let root = root.to_path_buf();
let read = crate::utils::fs::run_blocking(move || unread(&root, &files, config));
out.files
.extend(read.await.map(|why| (KEY.to_string(), why)));
}

// Go: the workspace in effect decides which version a replace must pin,
// whether it takes effect at all, and whether it belongs in go.work.
Expand Down Expand Up @@ -6746,6 +6763,83 @@ snapshots:
}
}

/// In a Bazel workspace on disk the read of a run with a cargo
/// candidate hands the rewriter what stands against a pin: the hub
/// that reads the project's manifests, a member's among them, and not
/// the config the rewriter writes, which is the legacy file where the
/// project has one. Nothing is handed over in no Bazel workspace, for a
/// hub that names that config, or to a run without a cargo candidate.
#[tokio::test]
async fn a_bazel_hub_without_the_cargo_config_rides_to_the_cargo_rewriter() {
use crate::bazel::cargo::KEY;
let tmp = tempfile::tempdir().unwrap();
let write = |rel: &str, text: &str| {
let path = tmp.path().join(rel);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
};
let hub = |attributes: &str| {
format!(
"bazel_dep(name = \"rules_rust\", version = \"0.75.0\")\ncrate = use_extension(\
\"@rules_rust//crate_universe:extensions.bzl\", \"crate\")\n\
crate.from_cargo({attributes})\n"
)
};
write("Cargo.toml", "[workspace]\nmembers = [\"member\"]\n");
write(
"member/Cargo.toml",
"[package]\nname = \"member\"\nversion = \"0.1.0\"\n",
);
let cargo = Candidate {
purl: "pkg:cargo/serde@1.0.190".into(),
dep: grant_dep("cargo", "https://patch.test/serde-1.0.190.crate".into()),
};
let view = ProjectView::Disk(tmp.path());
let handed = |candidate: &Candidate| {
let candidates = [candidate.clone()];
async move {
let read = read_candidate_files(&view, &BTreeSet::new(), &candidates).await;
read.files.get(KEY).cloned()
}
};
assert_eq!(handed(&cargo).await, None, "no Bazel workspace");

write(
"MODULE.bazel",
&hub("manifests = [\"//member:Cargo.toml\"]"),
);
let why = handed(&cargo).await.expect("the hub reads a member");
assert!(
why.starts_with("the rules_rust hub at MODULE.bazel:3 reads"),
"{why}"
);
assert!(why.contains("without .cargo/config.toml, where"), "{why}");

let root = "manifests = [\"//:Cargo.toml\"]";
write("MODULE.bazel", &hub(root));
assert!(handed(&cargo).await.is_some(), "the hub reads the root");
let lock = "cargo_lockfile = \"//:Cargo.lock\", manifests = [\"//x:Cargo.toml\"]";
write("MODULE.bazel", &hub(lock));
assert!(handed(&cargo).await.is_some(), "the hub reads the lock");
write("MODULE.bazel", &hub(root));
assert_eq!(
handed(&left_pad_candidate()).await,
None,
"no cargo candidate"
);
let named = format!("{root}, cargo_config = \"//:.cargo/config.toml\"");
write("MODULE.bazel", &hub(&named));
assert_eq!(handed(&cargo).await, None, "the hub names the config");
write(".cargo/config", "");
let why = handed(&cargo)
.await
.expect("the rewriter writes the legacy file");
assert!(
why.contains("is not the label of .cargo/config as one string"),
"{why}"
);
}

fn gem_candidate() -> Candidate {
use crate::patch::redirect::{Integrity, RegistryOverride, RegistryOverrideIdentifiers};
Candidate {
Expand Down
120 changes: 109 additions & 11 deletions crates/socket-patch-core/src/patch/redirect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,23 @@ fn rewrite_cargo(
rewrite_cargo_with(files, overrides, &CratesIoSource::Registry, result);
}

/// The cargo config the cargo rewriter writes for a project with `files`,
/// from the project: the one the project's cargo reads.
///
/// Cargo reads the LEGACY extensionless `.cargo/config` in preference to
/// `config.toml` when both exist (it warns about the duplicate), so a
/// managed `[registries.…]` block written to `config.toml` there is
/// silently inert and the `registry = "socket-patch-…"` the rewriter puts
/// in Cargo.toml then names an undefined registry. Same preference
/// `vendor::cargo_config::config_path` applies on the vendor path.
pub(crate) fn cargo_config_key(files: &BTreeMap<String, String>) -> &'static str {
if files.contains_key(".cargo/config") {
".cargo/config"
} else {
".cargo/config.toml"
}
}

/// [`rewrite_cargo`] with `cargo_outer`: where the cargo configs read for a
/// build in the project (its own, every parent directory's and
/// `$CARGO_HOME`'s) take crates.io from. The project's own config is also
Expand Down Expand Up @@ -2114,17 +2131,7 @@ fn rewrite_cargo_with(
.get("Cargo.lock")
.cloned()
.map(|t| CargoHostedLock::new(to_lf("Cargo.lock", t)));
// Cargo reads the LEGACY extensionless `.cargo/config` in preference to
// `config.toml` when both exist (it warns about the duplicate), so a
// managed `[registries.…]` block written to `config.toml` there is
// silently inert and the `registry = "socket-patch-…"` this rewriter puts
// in Cargo.toml then names an undefined registry. Same preference
// `vendor::cargo_config::config_path` applies on the vendor path.
let cargo_config_key = if files.contains_key(".cargo/config") {
".cargo/config"
} else {
".cargo/config.toml"
};
let cargo_config_key = cargo_config_key(files);
let mut cargo_config = files
.get(cargo_config_key)
.cloned()
Expand Down Expand Up @@ -2476,6 +2483,21 @@ fn rewrite_cargo_with(
// when a healthy block is already wired in).
let config_plan = plan_cargo_config(&cargo_config, cargo_config_key, &reg, index_url);

// In a Bazel workspace, a rules_rust hub that reads these files
// and not the config would fail on the pin, and `bazel build` with
// it ([`crate::bazel::cargo`]): nothing is written for the crate.
// A pin that is in place already stays what it was.
let writes = config_plan.is_some()
|| toml_plans.iter().any(|(_, plan)| plan.changed)
|| matches!(lock_commit, LockCommit::Write(_));
if let Some(why) = files.get(crate::bazel::cargo::KEY).filter(|_| writes) {
result.warnings.push(RewriteWarning {
code: crate::bazel::cargo::UNREAD.into(),
detail: format!("{}@{}: {why}", dep.name, dep.version),
});
continue;
}

// COMMIT — everything planned, nothing can fail past this point, so
// the three files change together or not at all. Edit order matches
// the historical ledger order: config, manifest, lock.
Expand Down Expand Up @@ -19823,6 +19845,82 @@ mod tests {
assert!(r.confirmed_cargo_uuids.contains(CARGO_UUID));
}

/// In a Bazel workspace whose rules_rust hub would not read the config
/// that defines the pin's registry, the engine hands the reason in
/// under [`crate::bazel::cargo::KEY`]: a crate the rewriter would pin
/// is skipped with it and nothing is written. A refusal of the
/// rewriter's own answers first, under its own code, and a pin that an
/// earlier run left in place stays confirmed, with no warning and no
/// write, exactly as without the key.
#[test]
fn cargo_pin_is_withheld_for_a_bazel_hub_that_does_not_read_the_config() {
use crate::bazel::cargo::{KEY, UNREAD};
let toml = "[package]\nname = \"app\"\nversion = \"0.1.0\"\n\n\
[dependencies]\nserde = \"1.0.190\"\n";
let plain = cargo_files(toml);
let pinned = rewrite_registry_redirect(&plain, &[cargo_sparse_override()]);
assert_eq!(pinned.files.len(), 3, "{:?}", pinned.files.keys());
let codes = |r: &RewriteResult| -> Vec<String> {
r.warnings.iter().map(|w| w.code.clone()).collect()
};

let mut hub = plain.clone();
hub.insert(
KEY.to_string(),
"the hub at MODULE.bazel:4 is in the way".into(),
);
let r = rewrite_registry_redirect(&hub, &[cargo_sparse_override()]);
assert!(r.files.is_empty() && r.edits.is_empty(), "{:?}", r.files);
assert!(r.confirmed_cargo_uuids.is_empty());
assert_eq!(codes(&r), [UNREAD]);
let detail = &r.warnings[0].detail;
assert_eq!(
detail,
"serde@1.0.190: the hub at MODULE.bazel:4 is in the way"
);

// Each file the rewriter would write is enough to withhold: a
// project without a lock (config and manifest), a pin whose
// registry the config no longer defines (config), and a config
// that defines it already (manifest and lock). Without the key
// each is written.
let config = ".cargo/config.toml";
let (mut lockless, mut undefined, mut defined) = (hub.clone(), hub.clone(), hub.clone());
lockless.remove("Cargo.lock");
undefined.extend(pinned.files.clone());
undefined.remove(config);
defined.insert(config.to_string(), pinned.files[config].clone());
for mut files in [lockless, undefined, defined] {
let r = rewrite_registry_redirect(&files, &[cargo_sparse_override()]);
assert!(r.files.is_empty() && r.edits.is_empty(), "{:?}", r.files);
assert!(r.confirmed_cargo_uuids.is_empty());
assert_eq!(codes(&r), [UNREAD]);
files.remove(KEY);
let r = rewrite_registry_redirect(&files, &[cargo_sparse_override()]);
assert!(!r.files.is_empty() && r.warnings.is_empty(), "{files:?}");
}

// The rewriter's own refusal: the crate is in the lock and in no
// manifest.
let mut undeclared = hub.clone();
let other = toml.replace("serde = \"1.0.190\"", "itoa = \"1\"");
undeclared.insert("Cargo.toml".to_string(), other);
let r = rewrite_registry_redirect(&undeclared, &[cargo_sparse_override()]);
assert_eq!(codes(&r), ["redirect_cargo_toml_dep_not_found"]);

// The three files as the earlier run left them.
let mut again = hub.clone();
again.extend(pinned.files.clone());
let with = rewrite_registry_redirect(&again, &[cargo_sparse_override()]);
again.remove(KEY);
let without = rewrite_registry_redirect(&again, &[cargo_sparse_override()]);
for r in [&with, &without] {
assert!(r.files.is_empty() && r.edits.is_empty(), "{:?}", r.files);
assert!(r.warnings.is_empty(), "{:?}", r.warnings);
assert!(r.confirmed_cargo_uuids.contains(CARGO_UUID));
}
}

/// #455 review: the offline source may come from outside the project
/// files (a monorepo root's `.cargo/config.toml`, `$CARGO_HOME`'s), or
/// be a `cargo local-registry`; both are refused like a project
Expand Down
Loading
Loading