fix(cli): correct async-related panic

This commit is contained in:
daimond113 2024-11-12 16:00:37 +01:00
parent 72eb48de07
commit dcd6a2a107
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C

View file

@ -9,7 +9,6 @@ use pesde::{
}; };
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use std::{env::current_dir, ffi::OsString, io::Write, path::PathBuf, process::Command}; use std::{env::current_dir, ffi::OsString, io::Write, path::PathBuf, process::Command};
use tokio::runtime::Handle;
#[derive(Debug, Args)] #[derive(Debug, Args)]
pub struct RunCommand { pub struct RunCommand {
@ -24,49 +23,45 @@ pub struct RunCommand {
impl RunCommand { impl RunCommand {
pub async fn run(self, project: Project) -> anyhow::Result<()> { pub async fn run(self, project: Project) -> anyhow::Result<()> {
let project_2 = project.clone();
let update_scripts_handle = tokio::spawn(async move { update_scripts(&project_2).await });
let run = |path: PathBuf| { let run = |path: PathBuf| {
Handle::current() let package_dir = project.package_dir().to_path_buf();
.block_on(update_scripts_handle) let fut = update_scripts(&project);
.unwrap() async move {
.expect("failed to update scripts"); fut.await.expect("failed to update scripts");
let mut caller = tempfile::NamedTempFile::new().expect("failed to create tempfile"); let mut caller = tempfile::NamedTempFile::new().expect("failed to create tempfile");
caller caller
.write_all( .write_all(
generate_bin_linking_module( generate_bin_linking_module(
project.package_dir(), package_dir,
&format!("{:?}", path.to_string_lossy()), &format!("{:?}", path.to_string_lossy()),
)
.as_bytes(),
) )
.as_bytes(), .expect("failed to write to tempfile");
)
.expect("failed to write to tempfile");
let status = Command::new("lune") let status = Command::new("lune")
.arg("run") .arg("run")
.arg(caller.path()) .arg(caller.path())
.arg("--") .arg("--")
.args(&self.args) .args(&self.args)
.current_dir(current_dir().expect("failed to get current directory")) .current_dir(current_dir().expect("failed to get current directory"))
.status() .status()
.expect("failed to run script"); .expect("failed to run script");
drop(caller); drop(caller);
std::process::exit(status.code().unwrap_or(1)) std::process::exit(status.code().unwrap_or(1))
}
}; };
let package_or_script = match self.package_or_script { let Some(package_or_script) = self.package_or_script else {
Some(package_or_script) => package_or_script, if let Some(script_path) = project.deser_manifest().await?.target.bin_path() {
None => { run(script_path.to_path(project.package_dir())).await;
if let Some(script_path) = project.deser_manifest().await?.target.bin_path() { return Ok(());
run(script_path.to_path(project.package_dir()));
}
anyhow::bail!("no package or script specified")
} }
anyhow::bail!("no package or script specified, and no bin path found in manifest")
}; };
if let Ok(pkg_name) = package_or_script.parse::<PackageName>() { if let Ok(pkg_name) = package_or_script.parse::<PackageName>() {
@ -102,14 +97,14 @@ impl RunCommand {
version_id.version(), version_id.version(),
); );
run(bin_path.to_path(&container_folder)); run(bin_path.to_path(&container_folder)).await;
return Ok(()); return Ok(());
} }
} }
if let Ok(manifest) = project.deser_manifest().await { if let Ok(manifest) = project.deser_manifest().await {
if let Some(script_path) = manifest.scripts.get(&package_or_script) { if let Some(script_path) = manifest.scripts.get(&package_or_script) {
run(script_path.to_path(project.package_dir())); run(script_path.to_path(project.package_dir())).await;
return Ok(()); return Ok(());
} }
}; };
@ -118,10 +113,10 @@ impl RunCommand {
let path = relative_path.to_path(project.package_dir()); let path = relative_path.to_path(project.package_dir());
if !path.exists() { if !path.exists() {
anyhow::bail!("path does not exist: {}", path.display()); anyhow::bail!("path `{}` does not exist", path.display());
} }
run(path); run(path).await;
Ok(()) Ok(())
} }