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
26 changes: 26 additions & 0 deletions backend/windmill-worker/loader.bun.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,32 @@ const p = {

build.onLoad({ filter: /.*\.url$/ }, async (args) => {
const url = readFileSync(args.path, "utf8");

// Extract the script path from the URL to check for local file
// URL format: {base_url}/api/w/{w_id}/scripts/raw_unpinned/p/{script_path}
try {
const urlObj = new URL(url);
const pathMatch = urlObj.pathname.match(/\/scripts\/raw_unpinned\/p\/(.+)$/);

if (pathMatch) {
const scriptPath = pathMatch[1];
// Check if we have this file locally (from raw_scripts)
const localPath = resolve("./" + scriptPath);
try {
const localContent = readFileSync(localPath, "utf8");
return {
contents: replaceRelativeImports(localContent).contents,
loader: "tsx",
};
} catch {
// File not found locally, fall through to fetch from server
}
}
} catch {
// URL parsing failed, fall through to fetch from server
}

// Fallback: fetch from server
const req = await fetch(url, {
method: "GET",
headers: {
Expand Down
41 changes: 41 additions & 0 deletions backend/windmill-worker/src/bun_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ use windmill_common::{
use windmill_common::s3_helpers::attempt_fetch_bytes;

use windmill_parser::Typ;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct RawScriptForDependencies {
pub script_path: String,
pub raw_code: Option<String>,
pub language: ScriptLang,
}

const RELATIVE_BUN_LOADER: &str = include_str!("../loader.bun.js");

Expand Down Expand Up @@ -106,11 +114,42 @@ pub async fn gen_bun_lockfile(
workspace_dependencies: &WorkspaceDependenciesPrefetched,
npm_mode: bool,
occupancy_metrics: &mut Option<&mut OccupancyMetrics>,
raw_scripts: Option<&Vec<RawScriptForDependencies>>,
) -> Result<Option<String>> {
let common_bun_proc_envs: HashMap<String, String> = get_common_bun_proc_envs(None).await;

let mut empty_deps = false;

// Write raw scripts to job_dir so loader can find them locally
if let Some(scripts) = raw_scripts {
for script in scripts {
if let Some(ref raw_code) = script.raw_code {
// Normalize path: remove leading slash and u/ or f/ prefixes
let path = script.script_path
.trim_start_matches('/')
.trim_start_matches("u/")
.trim_start_matches("f/");

// Ensure .ts extension
let file_path = if path.ends_with(".ts") {
path.to_string()
} else {
format!("{}.ts", path)
};

// Create parent directories if needed
let script_file_path = format!("{}/{}", job_dir, file_path);
if let Some(parent) = std::path::Path::new(&script_file_path).parent() {
std::fs::create_dir_all(parent)?;
}

// Write the script file
write_file(job_dir, &file_path, raw_code)?;
tracing::debug!("Wrote raw_script to {}/{}", job_dir, file_path);
}
}
}

if let Some(package_json_content) = workspace_dependencies.get_bun()? {
gen_bunfig(job_dir).await?;
write_file(job_dir, "package.json", package_json_content.as_str())?;
Expand Down Expand Up @@ -987,6 +1026,7 @@ pub async fn handle_bun_job(
workspace_dependencies,
annotation.npm,
&mut Some(occupancy_metrics),
None, // raw_scripts - will be provided by worker.rs later
)
.await?;

Expand Down Expand Up @@ -1700,6 +1740,7 @@ pub async fn start_worker(
.await?,
annotation.npm,
&mut None,
None, // raw_scripts - will be provided by worker.rs later
)
.await?;
}
Expand Down
1 change: 1 addition & 0 deletions backend/windmill-worker/src/worker_lockfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2743,6 +2743,7 @@ async fn capture_dependency_job(
&workspace_dependencies,
windmill_common::worker::TypeScriptAnnotations::parse(job_raw_code).npm,
&mut Some(occupancy_metrics),
None, // raw_scripts - will be provided by worker.rs later
)
.await?
{
Expand Down
Loading