Put a lock around cargo build invocations for plugin tests (#8333)

I think this _might_ fix the issues we've been seeing with plugin tests.

In a nutshell, the plugin tests run `cargo build` to ensure that plugins
have been built:
f6ca62384e/crates/nu-test-support/src/commands.rs (L6)

This PR adds a mutex to ensure that we're never running `cargo build`
concurrently. It also uses an atomic bool to signal when plugins have
already been built, so we can avoid invoking `cargo build` multiple
times unnecessarily.

I can't be certain yet, but I'm guessing the macOS CI problems we've
been seeing come from plugin tests clobbering each other (something
like: test 1 builds the `foo` plugin, then test2 invokes `cargo build`
again and deletes the `foo` plugin from disk).
This commit is contained in:
Reilly Wood 2023-03-05 19:04:12 -08:00 committed by GitHub
parent f6ca62384e
commit a4952bc029
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 3 deletions

View file

@ -1,11 +1,27 @@
use std::{
io::Read,
process::{Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering::Relaxed},
Mutex,
},
};
pub fn ensure_binary_present(package: &str) {
static CARGO_BUILD_LOCK: Mutex<()> = Mutex::new(());
static PLUGINS_BUILT: AtomicBool = AtomicBool::new(false);
// This runs `cargo build --package nu_plugin_*` to ensure that all plugins
// have been built before plugin tests run. We use a lock to avoid multiple
// simultaneous `cargo build` invocations clobbering each other.
pub fn ensure_plugins_built() {
let _guard = CARGO_BUILD_LOCK.lock().expect("could not get mutex lock");
if PLUGINS_BUILT.load(Relaxed) {
return;
}
let cargo_path = env!("CARGO");
let mut arguments = vec!["build", "--package", package, "--quiet"];
let mut arguments = vec!["build", "--package", "nu_plugin_*", "--quiet"];
let profile = std::env::var("NUSHELL_CARGO_TARGET");
if let Ok(profile) = &profile {
@ -40,4 +56,6 @@ pub fn ensure_binary_present(package: &str) {
if !success {
panic!("cargo build failed");
}
PLUGINS_BUILT.store(true, Relaxed);
}

View file

@ -252,7 +252,7 @@ macro_rules! nu_with_plugins {
let temp_plugin_file = temp.path().join("plugin.nu");
std::fs::File::create(&temp_plugin_file).expect("couldn't create temporary plugin file");
$($crate::commands::ensure_binary_present($plugin_name);)+
$crate::commands::ensure_plugins_built();
// TODO: the `$format` is a dummy empty string, but `plugin_name` is repeatable
// just keep it here for now. Need to find a way to remove it.