fix: support npmjs mode for raw deps of package.json from CLI

This commit is contained in:
Ruben Fiszel
2024-07-25 09:53:18 +02:00
parent 9300ee6f47
commit 8e615c900d
4 changed files with 52 additions and 27 deletions
+11 -6
View File
@@ -3875,22 +3875,27 @@ async fn run_dependencies_job(
let raw_script = req.raw_scripts[0].clone();
let script_path = raw_script.script_path;
let ehm = HashMap::new();
let raw_code = raw_script.raw_code.unwrap_or_else(|| "".to_string());
let language = raw_script.language;
let (args, raw_code) = if let Some(deps) = req.raw_deps {
let mut hm = HashMap::new();
hm.insert(
"raw_deps".to_string(),
JsonRawValue::from_string("true".to_string()).unwrap(),
);
if language == ScriptLang::Bun {
let annotation = windmill_common::worker::get_annotation(&raw_code);
hm.insert(
"npm_mode".to_string(),
JsonRawValue::from_string(annotation.npm_mode.to_string()).unwrap(),
);
}
(PushArgs { extra: Some(hm), args: &ehm }, deps)
} else {
(
PushArgs::from(&ehm),
raw_script.raw_code.unwrap_or_else(|| "".to_string()),
)
(PushArgs::from(&ehm), raw_code)
};
let language = raw_script.language;
let (uuid, tx) = push(
&db,
PushIsolationLevel::IsolatedRoot(db.clone(), rsmq),
+17
View File
@@ -153,6 +153,23 @@ fn parse_file<T: FromStr>(path: &str) -> Option<T> {
.flatten()
}
pub struct Annotations {
pub npm_mode: bool,
pub nodejs_mode: bool,
}
pub fn get_annotation(inner_content: &str) -> Annotations {
let annotations = inner_content
.lines()
.take_while(|x| x.starts_with("//"))
.map(|x| x.to_string().replace("//", "").trim().to_string())
.collect_vec();
let nodejs_mode: bool = annotations.contains(&"nodejs".to_string());
let npm_mode: bool = annotations.contains(&"npm".to_string());
Annotations { npm_mode, nodejs_mode }
}
fn get_cgroupv2_path() -> Option<String> {
let cgroup_path: String = parse_file("/proc/self/cgroup")?;
+2 -19
View File
@@ -281,23 +281,6 @@ pub async fn install_lockfile(
Ok(())
}
pub struct Annotations {
pub npm_mode: bool,
pub nodejs_mode: bool,
}
pub fn get_annotation(inner_content: &str) -> Annotations {
let annotations = inner_content
.lines()
.take_while(|x| x.starts_with("//"))
.map(|x| x.to_string().replace("//", "").trim().to_string())
.collect_vec();
let nodejs_mode: bool = annotations.contains(&"nodejs".to_string());
let npm_mode: bool = annotations.contains(&"npm".to_string());
Annotations { npm_mode, nodejs_mode }
}
pub async fn build_loader(
job_dir: &str,
base_internal_url: &str,
@@ -512,7 +495,7 @@ pub async fn handle_bun_job(
let common_bun_proc_envs: HashMap<String, String> =
get_common_bun_proc_envs(&base_internal_url).await;
let mut annotation = get_annotation(inner_content);
let mut annotation = windmill_common::worker::get_annotation(inner_content);
if codebase.is_some() {
annotation.nodejs_mode = true
@@ -968,7 +951,7 @@ pub async fn start_worker(
let common_bun_proc_envs: HashMap<String, String> =
get_common_bun_proc_envs(&base_internal_url).await;
let mut annotation = get_annotation(inner_content);
let mut annotation = windmill_common::worker::get_annotation(inner_content);
//TODO: remove this when bun dedicated workers work without issues
annotation.nodejs_mode = true;
@@ -232,6 +232,21 @@ pub async fn handle_dependency_job<R: rsmq_async::RsmqConnection + Send + Sync +
.is_some_and(|y| y.to_string().as_str() == "true")
})
.unwrap_or(false);
let npm_mode = if job
.language
.as_ref()
.map(|v| v == &ScriptLang::Bun)
.unwrap_or(false)
{
Some(
job.args
.as_ref()
.map(|x| x.get("npm_mode").is_some())
.unwrap_or(false),
)
} else {
None
};
let content = capture_dependency_job(
&job.id,
@@ -252,6 +267,7 @@ pub async fn handle_dependency_job<R: rsmq_async::RsmqConnection + Send + Sync +
token,
script_path,
raw_deps,
npm_mode,
)
.await;
@@ -858,6 +874,7 @@ async fn lock_modules<'c>(
&path.clone().unwrap_or_else(|| job_path.to_string())
),
false,
None,
)
.await;
//
@@ -986,6 +1003,7 @@ async fn lock_modules_app(
token,
&format!("{}/app", job.script_path()),
false,
None,
)
.await;
match new_lock {
@@ -1177,6 +1195,7 @@ async fn capture_dependency_job(
token: &str,
script_path: &str,
raw_deps: bool,
npm_mode: Option<bool>,
) -> error::Result<String> {
match job_language {
ScriptLang::Python3 => {
@@ -1272,7 +1291,8 @@ async fn capture_dependency_job(
.await
}
ScriptLang::Bun => {
let annotation = crate::bun_executor::get_annotation(job_raw_code);
let npm_mode = npm_mode
.unwrap_or_else(|| windmill_common::worker::get_annotation(job_raw_code).npm_mode);
if !raw_deps {
let _ = write_file(job_dir, "main.ts", job_raw_code).await?;
}
@@ -1293,7 +1313,7 @@ async fn capture_dependency_job(
} else {
None
},
annotation.npm_mode,
npm_mode,
)
.await?;
Ok(req.unwrap_or_else(String::new))