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
25 changes: 17 additions & 8 deletions .github/workflows/bazel-composer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ name: Bazel patch compatibility (Composer)
# The Composer flows of the real-Bazel tests (`composer::` in
# `e2e_bazel_build`), on each Bazel release below: hosted mode where a
# repository rule runs `composer install` from the committed lock, hosted
# mode where a genrule does, and agent mode on a committed `vendor/` tree.
# mode where a genrule does, agent mode on a committed `vendor/` tree, and
# vendored mode where the repository rule installs and on the committed tree.
# No Bazel ruleset builds PHP or takes Composer packages, so the three
# shapes are the fixture's own, as a PHP project under Bazel writes them by
# hand. bazel-compatibility.yml holds the legs and what every ecosystem
Expand Down Expand Up @@ -46,13 +47,15 @@ on:
# with `composer` in its name. The flows run through most of them: the
# lock's model and its hosted rewrite (formats/composer), the crawler
# that finds an installed package, the lock inventory, the discovery of
# a hosted pin for `vex`, the upstream restore of `rollback`, and the
# hints a command prints. The vendored backend (vendor/composer_lock)
# matches the patterns too, and no flow runs vendored mode. One more
# file is here: patch/user_patches.rs, the inspection of a project's own
# patches, whose Composer part every scan of a Composer project runs.
# That file also holds the npm, Cargo and Go parts, so a change to
# those starts the Composer legs too.
# a hosted pin for `vex`, the upstream restore of `rollback`, the hints
# a command prints, and the vendored backend (vendor/composer_lock), with
# the refusal of vendored mode in a Bazel workspace. Four more are here.
# patch/user_patches.rs is the inspection of a project's own patches,
# whose Composer part every scan of a Composer project runs; it also
# holds the npm, Cargo and Go parts, so a change to those starts the
# Composer legs too. bazel/mod.rs finds the Bazel root that refusal asks
# for, on the walk of utils/repo_root.rs, and vendor/refusals.rs hands
# the refusal to every command.
#
# What it does not cover: shared engine code (patch/redirect/mod.rs,
# hosted/, the apply and rollback engines of agent mode, vex/,
Expand All @@ -72,6 +75,9 @@ on:
- 'crates/*/src/**/*composer*'
- 'crates/*/src/**/*composer*/**'
- 'crates/socket-patch-core/src/patch/user_patches.rs'
- 'crates/socket-patch-core/src/bazel/mod.rs'
- 'crates/socket-patch-core/src/utils/repo_root.rs'
- 'crates/socket-patch-core/src/vendor/refusals.rs'
push:
branches: [main]
# The pull request paths, and what only a push to main covers: the
Expand All @@ -91,6 +97,9 @@ on:
- 'crates/*/src/**/*composer*'
- 'crates/*/src/**/*composer*/**'
- 'crates/socket-patch-core/src/patch/user_patches.rs'
- 'crates/socket-patch-core/src/bazel/mod.rs'
- 'crates/socket-patch-core/src/utils/repo_root.rs'
- 'crates/socket-patch-core/src/vendor/refusals.rs'
- 'crates/socket-patch-cli/tests/e2e_bazel_build/main.rs'
- 'crates/socket-patch-core/src/patch/redirect/**'
- 'crates/socket-patch-core/src/patch/apply.rs'
Expand Down
5 changes: 4 additions & 1 deletion crates/socket-patch-cli/CLI_CONTRACT.md

Large diffs are not rendered by default.

27 changes: 25 additions & 2 deletions crates/socket-patch-cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,23 @@ pub struct GlobalArgs {
value_parser = parse_bool_flag,
)]
pub gem_refresh_shared: bool,

/// Vendored mode (`vendor`, `scan`/`get --mode vendored`): write for a
/// package although the project is in a Bazel workspace, where the
/// build may not find the vendored copy. Vendored mode declines such
/// a package by default; today that is a Composer package
/// (`vendor_composer_bazel_workspace`), whose lock names the copy by a
/// path that a Bazel rule running `composer install` in another
/// directory does not find. Pass it when the build does find the
/// copy. Other subcommands accept it silently.
#[arg(
help_heading = GLOBAL_OPTIONS,
long = "allow-vendored-in-bazel",
env = "SOCKET_ALLOW_VENDORED_IN_BAZEL",
default_value_t = false,
value_parser = parse_bool_flag,
)]
pub allow_vendored_in_bazel: bool,
}

impl GlobalArgs {
Expand Down Expand Up @@ -742,8 +759,9 @@ pub(crate) fn socket_dir_of(manifest_path: &Path, cwd: &Path) -> PathBuf {

/// Mirror the flags that core reads from the env (`SOCKET_OFFLINE`,
/// `SOCKET_DEBUG`, `SOCKET_TELEMETRY_DISABLED`, `SOCKET_API_URL`,
/// `SOCKET_PROXY_URL`) into it. Core's own parse of `SOCKET_OFFLINE` and
/// `SOCKET_DEBUG` matches only `"1" | "true"`,
/// `SOCKET_PROXY_URL`, `SOCKET_ALLOW_VENDORED_IN_BAZEL`) into it. Core's
/// own parse of `SOCKET_OFFLINE` and `SOCKET_DEBUG` matches only
/// `"1" | "true"`,
/// and the telemetry kill-switch and endpoint resolvers never see the parsed
/// flags: without the mirror `--offline` still sent telemetry, and an
/// on-prem `--api-url` run sent it, token included, to `api.socket.dev`.
Expand All @@ -762,6 +780,9 @@ pub(crate) fn apply_env_toggles(common: &GlobalArgs) {
if common.no_telemetry {
std::env::set_var("SOCKET_TELEMETRY_DISABLED", "1");
}
if common.allow_vendored_in_bazel {
std::env::set_var(socket_patch_core::vendor::refusals::ALLOW_IN_BAZEL_ENV, "1");
}
if let Some(url) = common.api_url.as_deref().filter(|s| !s.is_empty()) {
std::env::set_var("SOCKET_API_URL", url);
}
Expand Down Expand Up @@ -803,6 +824,7 @@ pub const GLOBAL_ARG_ENV_VARS: &[&str] = &[
"SOCKET_NO_GEM_NATIVE_REBUILD",
"SOCKET_NO_GEM_INSTALL_REFRESH",
"SOCKET_GEM_REFRESH_SHARED",
"SOCKET_ALLOW_VENDORED_IN_BAZEL",
];

/// Every env var a subcommand-local flag binds (one per `env = "..."`
Expand Down Expand Up @@ -877,6 +899,7 @@ impl Default for GlobalArgs {
no_gem_native_rebuild: false,
no_gem_install_refresh: false,
gem_refresh_shared: false,
allow_vendored_in_bazel: false,
}
}
}
Expand Down
116 changes: 81 additions & 35 deletions crates/socket-patch-cli/src/commands/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,57 @@ impl EjectSnapshot {
}
}

/// An eject refused whole, with nothing changed: each pin of `refused` is
/// a `failed` event with its code and its reason, the envelope's error is
/// `eject_refused`, and the exit code is 1.
async fn eject_refused(
common: &GlobalArgs,
run_warnings: Vec<(String, String)>,
client: &socket_patch_core::api::client::ApiClient,
telemetry: &TelemetryAuth,
eject_uuids: &HashMap<String, String>,
refused: &[(String, &'static str, String)],
) -> i32 {
let mut env = Envelope::new(Command::Vendor);
env.warnings.extend(
run_warnings
.into_iter()
.map(|(code, detail)| RunWarning { code, detail }),
);
env.dry_run = common.dry_run;
if common.json {
env.warnings
.extend(crate::commands::vex_sources::api_auth_fallback_warning(
client,
));
}
for (purl, code, why) in refused {
report_vendor_failure(common, purl, why);
env.record(
PatchEvent::new(PatchAction::Failed, purl.clone()).with_error(*code, why.clone()),
);
}
env.mark_error(EnvelopeError::new(
"eject_refused",
"not every hosted pin can be restored to its upstream registry entry and vendored; \
nothing was changed",
));
if common.json {
println!("{}", env.to_pretty_json());
}
track_outcomes_for_vendor(
"vendor",
true,
&env,
common.dry_run,
eject_uuids,
&[],
telemetry,
)
.await;
1
}

/// Standalone `vendor` with hosted pins in scope: EJECT. The patch set comes
/// only from the lockfile pins (purl + the uuid in each hosted URL), even
/// when `.socket/manifest.json` exists: manifest-only patches are not
Expand Down Expand Up @@ -2029,6 +2080,30 @@ async fn run_eject(
get_api_client_with_overrides(common.api_client_overrides()).await;
let telemetry = TelemetryAuth::for_client(&client);
let service = common.vendor_service_config(Some(client.clone()), use_public_proxy);
// The refusals that rest on the Bazel workspace need no record and no
// registry, so they are asked before either is fetched: the pins stay
// hosted whatever the network says.
let pairs: Vec<(&str, &str)> = pins
.iter()
.map(|pin| (pin.purl.as_str(), pin.uuid.as_str()))
.collect();
let declined: Vec<(String, &'static str, String)> =
vendor::refusals::bazel_workspace_refusals(&common.cwd, &pairs)
.await
.into_iter()
.map(|(purl, code, detail)| (purl.to_string(), code, format!("kept hosted: {detail}")))
.collect();
if !declined.is_empty() {
return eject_refused(
common,
run_warnings,
&client,
&telemetry,
&eject_uuids,
&declined,
)
.await;
}
if !common.json && !common.silent {
println!(
"{} {} into .socket/vendor/...",
Expand Down Expand Up @@ -2196,44 +2271,15 @@ async fn run_eject(
}
}
if !refused.is_empty() {
let mut env = Envelope::new(Command::Vendor);
env.warnings.extend(
run_warnings
.into_iter()
.map(|(code, detail)| RunWarning { code, detail }),
);
env.dry_run = common.dry_run;
if common.json {
env.warnings
.extend(crate::commands::vex_sources::api_auth_fallback_warning(
&client,
));
}
for (purl, code, why) in &refused {
report_vendor_failure(common, purl, why);
env.record(
PatchEvent::new(PatchAction::Failed, purl.clone()).with_error(*code, why.clone()),
);
}
env.mark_error(EnvelopeError::new(
"eject_refused",
"not every hosted pin can be restored to its upstream registry entry and vendored; \
nothing was changed",
));
if common.json {
println!("{}", env.to_pretty_json());
}
track_outcomes_for_vendor(
"vendor",
true,
&env,
common.dry_run,
&eject_uuids,
&[],
return eject_refused(
common,
run_warnings,
&client,
&telemetry,
&eject_uuids,
&refused,
)
.await;
return 1;
}

// A dry run stops at the verified plan: restoring the live lock to
Expand Down
12 changes: 11 additions & 1 deletion crates/socket-patch-cli/tests/cli_global_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ fn global_flag_cases() -> Vec<(&'static str, Option<&'static str>, fn(&GlobalArg
("--gem-refresh-shared", None, |c| {
assert!(c.gem_refresh_shared)
}),
("--allow-vendored-in-bazel", None, |c| {
assert!(c.allow_vendored_in_bazel)
}),
("--lock-timeout", Some("30"), |c| {
assert_eq!(c.lock_timeout, Some(30))
}),
Expand Down Expand Up @@ -259,6 +262,7 @@ fn global_flag_cases_cover_every_global_field() {
no_gem_native_rebuild: _,
no_gem_install_refresh: _,
gem_refresh_shared: _,
allow_vendored_in_bazel: _,
strict: _,
allow_patch_conflicts: _,
vendor_source: _,
Expand Down Expand Up @@ -589,6 +593,9 @@ fn bool_env_vars_reject_zero_and_falsey() {
("SOCKET_DEBUG", |c| c.debug),
("SOCKET_TELEMETRY_DISABLED", |c| c.no_telemetry),
("SOCKET_JSON", |c| c.json),
("SOCKET_ALLOW_VENDORED_IN_BAZEL", |c| {
c.allow_vendored_in_bazel
}),
];

let parse_list = |var: &str, value: &str| {
Expand Down Expand Up @@ -627,7 +634,7 @@ fn bool_env_vars_reject_zero_and_falsey() {
#[serial_test::serial]
fn empty_bool_env_var_resolves_to_false_not_crash() {
// (env var, accessor) for every boolean global.
let bool_vars: [(&str, fn(&GlobalArgs) -> bool); 16] = [
let bool_vars: [(&str, fn(&GlobalArgs) -> bool); 17] = [
("SOCKET_OFFLINE", |c| c.offline),
("SOCKET_STRICT", |c| c.strict),
("SOCKET_GLOBAL", |c| c.global),
Expand All @@ -652,6 +659,9 @@ fn empty_bool_env_var_resolves_to_false_not_crash() {
c.no_gem_install_refresh
}),
("SOCKET_GEM_REFRESH_SHARED", |c| c.gem_refresh_shared),
("SOCKET_ALLOW_VENDORED_IN_BAZEL", |c| {
c.allow_vendored_in_bazel
}),
];

for (var, accessor) in bool_vars {
Expand Down
1 change: 1 addition & 0 deletions crates/socket-patch-cli/tests/common/clap_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ pub fn global_defaults() -> Globals {
no_gem_native_rebuild: false,
no_gem_install_refresh: false,
gem_refresh_shared: false,
allow_vendored_in_bazel: false,
})
}

Expand Down
Loading
Loading