From 56a82e72d9867a9b5f8a10bc8e4b81b86cd815c9 Mon Sep 17 00:00:00 2001 From: andy finch Date: Thu, 8 Aug 2019 17:38:53 -0400 Subject: [PATCH] Resolve worker specifiers relative to main module of host. (#2751) --- cli/ops.rs | 16 +++++++++++++--- tests/026_workers.ts | 4 ++-- tests/039_worker_deno_ns.ts | 4 ++-- tests/039_worker_deno_ns/has_ns.ts | 2 +- tests/039_worker_deno_ns/no_ns.ts | 2 +- tests/workers_round_robin_bench.ts | 2 +- tests/workers_startup_bench.ts | 2 +- 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cli/ops.rs b/cli/ops.rs index d75e7bd201..d5da905678 100644 --- a/cli/ops.rs +++ b/cli/ops.rs @@ -2095,9 +2095,21 @@ fn op_create_worker( let parent_state = state.clone(); + let mut module_specifier = ModuleSpecifier::resolve_url_or_path(specifier)?; + + let mut child_argv = parent_state.argv.clone(); + + if !has_source_code { + if let Some(module) = state.main_module() { + module_specifier = + ModuleSpecifier::resolve_import(specifier, &module.to_string())?; + child_argv[1] = module_specifier.to_string(); + } + } + let child_state = ThreadSafeState::new( parent_state.flags.clone(), - parent_state.argv.clone(), + child_argv, op_selector_std, parent_state.progress.clone(), include_deno_namespace, @@ -2134,8 +2146,6 @@ fn op_create_worker( return ok_buf(exec_cb(worker)); } - let module_specifier = ModuleSpecifier::resolve_url_or_path(specifier)?; - let op = worker .execute_mod_async(&module_specifier, false) .and_then(move |()| Ok(exec_cb(worker))); diff --git a/tests/026_workers.ts b/tests/026_workers.ts index e001f03e66..f45fc4b77e 100644 --- a/tests/026_workers.ts +++ b/tests/026_workers.ts @@ -1,5 +1,5 @@ -const jsWorker = new Worker("./tests/subdir/test_worker.js"); -const tsWorker = new Worker("./tests/subdir/test_worker.ts"); +const jsWorker = new Worker("./subdir/test_worker.js"); +const tsWorker = new Worker("./subdir/test_worker.ts"); tsWorker.onmessage = (e): void => { console.log("Received ts: " + e.data); diff --git a/tests/039_worker_deno_ns.ts b/tests/039_worker_deno_ns.ts index 1c6830c226..80ada43431 100644 --- a/tests/039_worker_deno_ns.ts +++ b/tests/039_worker_deno_ns.ts @@ -1,5 +1,5 @@ -const w1 = new Worker("./tests/039_worker_deno_ns/has_ns.ts"); -const w2 = new Worker("./tests/039_worker_deno_ns/no_ns.ts", { +const w1 = new Worker("./039_worker_deno_ns/has_ns.ts"); +const w2 = new Worker("./039_worker_deno_ns/no_ns.ts", { noDenoNamespace: true }); let w1MsgCount = 0; diff --git a/tests/039_worker_deno_ns/has_ns.ts b/tests/039_worker_deno_ns/has_ns.ts index 15e729f63a..8d25071226 100644 --- a/tests/039_worker_deno_ns/has_ns.ts +++ b/tests/039_worker_deno_ns/has_ns.ts @@ -2,7 +2,7 @@ onmessage = (msg): void => { if (msg.data === "START") { postMessage("has_ns.ts: is window.Deno available: " + !!window.Deno); } else { - const worker = new Worker("./tests/039_worker_deno_ns/maybe_ns.ts"); + const worker = new Worker("./maybe_ns.ts"); worker.onmessage = (msg): void => { postMessage("[SPAWNED BY has_ns.ts] " + msg.data); }; diff --git a/tests/039_worker_deno_ns/no_ns.ts b/tests/039_worker_deno_ns/no_ns.ts index 4cee98bea3..0489a00a39 100644 --- a/tests/039_worker_deno_ns/no_ns.ts +++ b/tests/039_worker_deno_ns/no_ns.ts @@ -2,7 +2,7 @@ onmessage = (msg): void => { if (msg.data === "START") { postMessage("no_ns.ts: is window.Deno available: " + !!window.Deno); } else { - const worker = new Worker("./tests/039_worker_deno_ns/maybe_ns.ts"); + const worker = new Worker("./maybe_ns.ts"); worker.onmessage = (msg): void => { postMessage("[SPAWNED BY no_ns.ts] " + msg.data); }; diff --git a/tests/workers_round_robin_bench.ts b/tests/workers_round_robin_bench.ts index d8bbbb2de8..308ed05d48 100644 --- a/tests/workers_round_robin_bench.ts +++ b/tests/workers_round_robin_bench.ts @@ -39,7 +39,7 @@ function handleAsyncMsgFromWorker( async function main(): Promise { const workers: Array<[Map>, Worker]> = []; for (var i = 1; i <= workerCount; ++i) { - const worker = new Worker("tests/subdir/bench_worker.ts"); + const worker = new Worker("./subdir/bench_worker.ts"); const promise = new Promise( (resolve): void => { worker.onmessage = (e): void => { diff --git a/tests/workers_startup_bench.ts b/tests/workers_startup_bench.ts index 53232ed566..2e529c01f0 100644 --- a/tests/workers_startup_bench.ts +++ b/tests/workers_startup_bench.ts @@ -4,7 +4,7 @@ const workerCount = 50; async function bench(): Promise { const workers: Worker[] = []; for (var i = 1; i <= workerCount; ++i) { - const worker = new Worker("tests/subdir/bench_worker.ts"); + const worker = new Worker("./subdir/bench_worker.ts"); const promise = new Promise( (resolve): void => { worker.onmessage = (e): void => {