host: review fixes for cargo.build, the test runner, the console, and main; tests for every PROTOCOL.md rule
cargo.build and the runner (review T22R): builds use the network again; core.unlock and core.notify in order under the runner; quiet test output; manifest preparation keeps [workspace], target tables, renamed and optional entries; cargo.rpc.discover; -32004 with data.output; retention of 10 builds; light, empty test worlds with an in-memory journal; only CARGO_/RUST_/SAUSAGE_ variables; stdin is null; the runner through --config. A compile cache that cannot be created is not fatal. Activation replaces plugin.wasm without a gap. Console and main: init ships the workspace Cargo.lock and creates the world atomically; each console command runs in its own task, so ps, stop, and kill work during a slow call; SAUSAGE_HEADLESS; the terminal mode is restored; signals during boot; chat text is not taken as a command. Tests: 20 more tests for PROTOCOL.md sections 2 to 10; the cargo tests choose the libtest binary from cargo's JSON output and time out.
This commit is contained in:
+142
-71
@@ -36,17 +36,19 @@ pub struct BuildOutput {
|
||||
pub struct BuildFailure {
|
||||
pub reason: String,
|
||||
pub tests: TestResults,
|
||||
pub output: String,
|
||||
}
|
||||
impl std::fmt::Display for BuildFailure {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(formatter, "{}\n{}", self.reason, self.tests.output)
|
||||
write!(formatter, "{}\n{}", self.reason, self.output)
|
||||
}
|
||||
}
|
||||
impl std::error::Error for BuildFailure {}
|
||||
impl From<anyhow::Error> for BuildFailure {
|
||||
fn from(error: anyhow::Error) -> Self {
|
||||
Self {
|
||||
reason: format!("{error:#}"),
|
||||
reason: "build preparation failed".into(),
|
||||
output: format!("{error:#}"),
|
||||
tests: TestResults::default(),
|
||||
}
|
||||
}
|
||||
@@ -54,7 +56,7 @@ impl From<anyhow::Error> for BuildFailure {
|
||||
/// A host-independent builder. Each builder serializes access to its work and target folders.
|
||||
pub struct Cargo {
|
||||
world: World,
|
||||
builds: tokio::sync::Mutex<()>,
|
||||
builds: Arc<tokio::sync::Mutex<()>>,
|
||||
runner: PathBuf,
|
||||
}
|
||||
impl Cargo {
|
||||
@@ -93,7 +95,9 @@ impl Cargo {
|
||||
),
|
||||
] {
|
||||
let mut document: toml_edit::DocumentMut = manifest.parse()?;
|
||||
document["workspace"] = toml_edit::table();
|
||||
if !document.contains_key("workspace") {
|
||||
document["workspace"] = toml_edit::table();
|
||||
}
|
||||
if let Some(path) = interface_path {
|
||||
for section in ["dependencies", "dev-dependencies"] {
|
||||
if let Some(dependency) = document
|
||||
@@ -109,7 +113,7 @@ impl Cargo {
|
||||
Ok(Self {
|
||||
world,
|
||||
runner,
|
||||
builds: tokio::sync::Mutex::new(()),
|
||||
builds: Arc::new(tokio::sync::Mutex::new(())),
|
||||
})
|
||||
}
|
||||
/// Registers cargo.build as a cancellable native process.
|
||||
@@ -118,43 +122,61 @@ impl Cargo {
|
||||
host.register_native(ID,Arc::new(move |_context,method,params| {
|
||||
let builder=builder.clone();
|
||||
Box::pin(async move {
|
||||
if method=="rpc.discover" { return Ok(json!({"openrpc":"1.2.6","info":{"title":"cargo","version":"native"},"methods":[{"name":"build","paramStructure":"by-name","params":[{"name":"plugin","required":true,"schema":{"type":"string"}}],"result":{"name":"build","schema":{"type":"object","required":["wasm","tests","warnings"],"properties":{"wasm":{"type":"string"},"warnings":{"type":"string"},"tests":{"type":"object","required":["passed","failed","output"],"properties":{"passed":{"type":"integer","minimum":0},"failed":{"type":"integer","minimum":0},"output":{"type":"string"}}}}}},"errors":[{"code":-32004,"message":"Build failed"}]}]})); }
|
||||
if method!="build" { return Err(crate::error(-32601,"cargo method not found")); }
|
||||
let plugin=params["plugin"].as_str().ok_or_else(||crate::error(-32602,"plugin must be a string"))?;
|
||||
match builder.build(&builder.world.plugin_dir(plugin).join("source")).await {
|
||||
Ok(output)=>Ok(json!({"wasm":format!("/{}",output.wasm.strip_prefix(&builder.world.root).unwrap().display()),"tests":output.tests,"warnings":output.warnings})),
|
||||
Err(failure)=>Err(json!({"code":-32004,"message":failure.reason,"data":{"tests":failure.tests}})),
|
||||
Err(failure)=>Err(json!({"code":-32004,"message":failure.reason,"data":{"tests":failure.tests,"output":failure.output}})),
|
||||
}
|
||||
})
|
||||
}));
|
||||
}
|
||||
/// Copies source once, runs WASI tests, then publishes a release component under state/cargo/builds.
|
||||
pub async fn build(&self, source: &Path) -> std::result::Result<BuildOutput, BuildFailure> {
|
||||
let _build = self.builds.lock().await;
|
||||
let build_guard = self.builds.clone().lock_owned().await;
|
||||
let work = self.world.state_dir(ID).join("work");
|
||||
let target = self.world.state_dir(ID).join("target");
|
||||
let temporary = self.world.state_dir(ID).join("tmp");
|
||||
let cleanup = temporary.clone();
|
||||
let source = source.to_path_buf();
|
||||
let copy = work.clone();
|
||||
let sdk = self.world.plugin_dir(ID).join("sdk");
|
||||
tokio::task::spawn_blocking(move || -> Result<()> {
|
||||
if copy.exists() {
|
||||
std::fs::remove_dir_all(©)?;
|
||||
let build_guard = tokio::task::spawn_blocking(move || -> Result<_> {
|
||||
if cleanup.exists() {
|
||||
std::fs::remove_dir_all(&cleanup)
|
||||
.with_context(|| format!("remove {}", cleanup.display()))?;
|
||||
}
|
||||
copy_source(&source, ©)?;
|
||||
std::fs::create_dir_all(&cleanup)
|
||||
.with_context(|| format!("create {}", cleanup.display()))?;
|
||||
if copy.exists() {
|
||||
std::fs::remove_dir_all(©)
|
||||
.with_context(|| format!("remove {}", copy.display()))?;
|
||||
}
|
||||
copy_source(&source, ©, true)?;
|
||||
let manifest = copy.join("Cargo.toml");
|
||||
let text = std::fs::read_to_string(&manifest)?;
|
||||
std::fs::write(manifest, prepare_manifest(&text, &sdk)?)?;
|
||||
Ok(())
|
||||
let text = std::fs::read_to_string(&manifest)
|
||||
.with_context(|| format!("read {}", manifest.display()))?;
|
||||
std::fs::write(
|
||||
&manifest,
|
||||
prepare_manifest(&text, &sdk)
|
||||
.with_context(|| format!("prepare {}", manifest.display()))?,
|
||||
)
|
||||
.with_context(|| format!("write {}", manifest.display()))?;
|
||||
Ok(build_guard)
|
||||
})
|
||||
.await
|
||||
.map_err(anyhow::Error::from)??;
|
||||
let mut test = Command::new("cargo");
|
||||
test.args(["test", "--offline", "--target", TARGET, "--target-dir"])
|
||||
test.args(["test", "--target", TARGET, "--target-dir"])
|
||||
.arg(&target)
|
||||
.current_dir(&work)
|
||||
.env(
|
||||
"CARGO_TARGET_WASM32_WASIP2_RUNNER",
|
||||
format!("{} test-runner", self.runner.display()),
|
||||
);
|
||||
.env("TMPDIR", &temporary)
|
||||
.arg("--config")
|
||||
.arg(format!(
|
||||
"target.wasm32-wasip2.runner={}",
|
||||
json!([self.runner, "test-runner"])
|
||||
));
|
||||
let output = run_command(test).await?;
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
@@ -165,11 +187,12 @@ impl Cargo {
|
||||
} else {
|
||||
test_count(&stdout, "failed").max(1)
|
||||
},
|
||||
output: format!("{stdout}\n{stderr}"),
|
||||
output: strip_progress(&format!("{stdout}\n{stderr}")),
|
||||
};
|
||||
if !output.status.success() {
|
||||
return Err(BuildFailure {
|
||||
reason: "WASI tests failed".into(),
|
||||
output: tests.output.clone(),
|
||||
tests,
|
||||
});
|
||||
}
|
||||
@@ -178,7 +201,6 @@ impl Cargo {
|
||||
build
|
||||
.args([
|
||||
"build",
|
||||
"--offline",
|
||||
"--release",
|
||||
"--target",
|
||||
TARGET,
|
||||
@@ -190,35 +212,35 @@ impl Cargo {
|
||||
let output = run_command(build).await?;
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
let mut diagnostics = stderr.to_string();
|
||||
let mut artifact = None;
|
||||
let diagnostics = stderr.to_string();
|
||||
let mut artifacts = Vec::new();
|
||||
for line in stdout.lines() {
|
||||
if let Ok(message) = serde_json::from_str::<Value>(line) {
|
||||
if message["reason"] == "compiler-artifact"
|
||||
&& let Some(executable) = message["executable"].as_str()
|
||||
{
|
||||
artifact = Some(PathBuf::from(executable));
|
||||
}
|
||||
if let Some(rendered) = message["message"]["rendered"].as_str() {
|
||||
diagnostics.push_str(rendered);
|
||||
}
|
||||
if let Ok(message) = serde_json::from_str::<Value>(line)
|
||||
&& message["reason"] == "compiler-artifact"
|
||||
&& message["target"]["kind"]
|
||||
.as_array()
|
||||
.is_some_and(|kinds| kinds.iter().any(|kind| kind == "bin"))
|
||||
&& let Some(executable) = message["executable"].as_str()
|
||||
{
|
||||
artifacts.push(PathBuf::from(executable));
|
||||
}
|
||||
}
|
||||
if !output.status.success() {
|
||||
bail!("{diagnostics}");
|
||||
}
|
||||
let artifact = artifact.context("cargo built no binary")?;
|
||||
if artifacts.len() != 1 {
|
||||
bail!(
|
||||
"cargo must build exactly one binary, found {}",
|
||||
artifacts.len()
|
||||
);
|
||||
}
|
||||
let artifact = artifacts.pop().unwrap();
|
||||
let builds = self.world.state_dir(ID).join("builds");
|
||||
std::fs::create_dir_all(&builds)?;
|
||||
let number = std::fs::read_dir(&builds)?
|
||||
.filter_map(|entry| entry.ok()?.file_name().to_str()?.parse::<u64>().ok())
|
||||
.max()
|
||||
.unwrap_or(0)
|
||||
+ 1;
|
||||
let destination = builds
|
||||
.join(number.to_string())
|
||||
.join(artifact.file_name().unwrap());
|
||||
crate::world::write_atomic(&destination, &tokio::fs::read(artifact).await?)?;
|
||||
let destination = tokio::task::spawn_blocking(move || {
|
||||
let _guard = build_guard;
|
||||
publish(&builds, &artifact)
|
||||
})
|
||||
.await??;
|
||||
Ok(BuildOutput {
|
||||
wasm: destination,
|
||||
warnings: strip_progress(&diagnostics),
|
||||
@@ -227,7 +249,8 @@ impl Cargo {
|
||||
}
|
||||
.await;
|
||||
result.map_err(|error: anyhow::Error| BuildFailure {
|
||||
reason: format!("{error:#}"),
|
||||
reason: "release build failed".into(),
|
||||
output: format!("{error:#}"),
|
||||
tests,
|
||||
})
|
||||
}
|
||||
@@ -247,6 +270,7 @@ async fn run_command(mut command: Command) -> Result<std::process::Output> {
|
||||
command
|
||||
.process_group(0)
|
||||
.kill_on_drop(true)
|
||||
.stdin(std::process::Stdio::null())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped());
|
||||
let child = command.spawn().context("start cargo")?;
|
||||
@@ -254,17 +278,21 @@ async fn run_command(mut command: Command) -> Result<std::process::Output> {
|
||||
Ok(child.wait_with_output().await?)
|
||||
}
|
||||
/// Copies only source, excluding build outputs and git internals at every level.
|
||||
fn copy_source(source: &Path, destination: &Path) -> Result<()> {
|
||||
std::fs::create_dir_all(destination)?;
|
||||
for entry in std::fs::read_dir(source)? {
|
||||
let entry = entry?;
|
||||
if matches!(entry.file_name().to_str(), Some("target" | ".git")) {
|
||||
fn copy_source(source: &Path, destination: &Path, root: bool) -> Result<()> {
|
||||
std::fs::create_dir_all(destination)
|
||||
.with_context(|| format!("create {}", destination.display()))?;
|
||||
for entry in std::fs::read_dir(source).with_context(|| format!("read {}", source.display()))? {
|
||||
let entry = entry.with_context(|| format!("read entry in {}", source.display()))?;
|
||||
if root && matches!(entry.file_name().to_str(), Some("target" | ".git")) {
|
||||
continue;
|
||||
}
|
||||
if entry.file_type()?.is_dir() {
|
||||
copy_source(&entry.path(), &destination.join(entry.file_name()))?;
|
||||
let from = entry.path();
|
||||
let to = destination.join(entry.file_name());
|
||||
if from.is_dir() {
|
||||
copy_source(&from, &to, false)?;
|
||||
} else {
|
||||
std::fs::copy(entry.path(), destination.join(entry.file_name()))?;
|
||||
std::fs::copy(&from, &to)
|
||||
.with_context(|| format!("copy {} to {}", from.display(), to.display()))?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -272,32 +300,75 @@ fn copy_source(source: &Path, destination: &Path) -> Result<()> {
|
||||
/// Repoints SDK dependencies without discarding feature flags and detaches the copied crate's workspace.
|
||||
fn prepare_manifest(text: &str, sdk: &Path) -> Result<String> {
|
||||
let mut document: toml_edit::DocumentMut = text.parse()?;
|
||||
for section in ["dependencies", "dev-dependencies", "build-dependencies"] {
|
||||
if let Some(dependency) = document
|
||||
.get_mut(section)
|
||||
.and_then(|table| table.get_mut("sausage-plugin"))
|
||||
{
|
||||
let features = dependency.get("features").cloned();
|
||||
let defaults = dependency.get("default-features").cloned();
|
||||
let mut replacement = toml_edit::InlineTable::new();
|
||||
replacement.insert("path", sdk.display().to_string().into());
|
||||
if let Some(features) = features.and_then(|value| value.as_value().cloned()) {
|
||||
replacement.insert("features", features);
|
||||
rewrite_dependencies(document.as_table_mut(), sdk);
|
||||
if !document.contains_key("workspace") {
|
||||
document["workspace"] = toml_edit::table();
|
||||
}
|
||||
Ok(document.to_string())
|
||||
}
|
||||
/// Rewrites dependency entries recursively, including target and workspace tables.
|
||||
fn rewrite_dependencies(table: &mut dyn toml_edit::TableLike, sdk: &Path) {
|
||||
for (name, item) in table.iter_mut() {
|
||||
if matches!(
|
||||
&*name,
|
||||
"dependencies" | "dev-dependencies" | "build-dependencies"
|
||||
) {
|
||||
if let Some(dependencies) = item.as_table_like_mut() {
|
||||
for (name, dependency) in dependencies.iter_mut() {
|
||||
if dependency
|
||||
.get("workspace")
|
||||
.and_then(toml_edit::Item::as_bool)
|
||||
== Some(true)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if &*name == "sausage-plugin"
|
||||
|| dependency.get("package").and_then(toml_edit::Item::as_str)
|
||||
== Some("sausage-plugin")
|
||||
{
|
||||
if dependency.is_str() {
|
||||
let version = dependency.as_str().unwrap().to_string();
|
||||
*dependency = toml_edit::value(toml_edit::InlineTable::new());
|
||||
dependency["version"] = toml_edit::value(version);
|
||||
}
|
||||
dependency["path"] = toml_edit::value(sdk.display().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(defaults) = defaults.and_then(|value| value.as_value().cloned()) {
|
||||
replacement.insert("default-features", defaults);
|
||||
}
|
||||
*dependency = toml_edit::value(replacement);
|
||||
} else if let Some(nested) = item.as_table_like_mut() {
|
||||
rewrite_dependencies(nested, sdk);
|
||||
}
|
||||
}
|
||||
document["workspace"] = toml_edit::table();
|
||||
Ok(document.to_string())
|
||||
}
|
||||
/// Publishes a complete artifact and retains only the ten newest numeric build folders.
|
||||
fn publish(builds: &Path, artifact: &Path) -> Result<PathBuf> {
|
||||
std::fs::create_dir_all(builds).with_context(|| format!("create {}", builds.display()))?;
|
||||
let mut numbers: Vec<u64> = std::fs::read_dir(builds)
|
||||
.with_context(|| format!("read {}", builds.display()))?
|
||||
.filter_map(|entry| entry.ok()?.file_name().to_str()?.parse().ok())
|
||||
.collect();
|
||||
numbers.sort_unstable();
|
||||
let number = numbers.last().copied().unwrap_or(0) + 1;
|
||||
let destination = builds
|
||||
.join(number.to_string())
|
||||
.join(artifact.file_name().unwrap());
|
||||
crate::world::write_atomic(
|
||||
&destination,
|
||||
&std::fs::read(artifact).with_context(|| format!("read {}", artifact.display()))?,
|
||||
)
|
||||
.with_context(|| format!("publish {}", destination.display()))?;
|
||||
for old in numbers.iter().take(numbers.len().saturating_sub(9)) {
|
||||
let path = builds.join(old.to_string());
|
||||
std::fs::remove_dir_all(&path).with_context(|| format!("remove {}", path.display()))?;
|
||||
}
|
||||
Ok(destination)
|
||||
}
|
||||
/// Writes embedded sources only when their bytes changed.
|
||||
fn write_changed(path: &Path, bytes: &[u8]) -> Result<()> {
|
||||
if std::fs::read(path).ok().as_deref() != Some(bytes) {
|
||||
std::fs::create_dir_all(path.parent().unwrap())?;
|
||||
std::fs::write(path, bytes)?;
|
||||
std::fs::create_dir_all(path.parent().unwrap())
|
||||
.with_context(|| format!("create parent of {}", path.display()))?;
|
||||
std::fs::write(path, bytes).with_context(|| format!("write {}", path.display()))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user