diff --git a/cli/msg.fbs b/cli/msg.fbs index 4eafe31ba5f2de..1827b23670e387 100644 --- a/cli/msg.fbs +++ b/cli/msg.fbs @@ -72,8 +72,6 @@ union Any { Truncate, HomeDir, HomeDirRes, - ExecPath, - ExecPathRes, Utime, WorkerGetMessage, WorkerGetMessageRes, @@ -454,12 +452,6 @@ table HomeDirRes { path: string; } -table ExecPath {} - -table ExecPathRes { - path: string; -} - table Utime { filename: string; atime: uint64; diff --git a/cli/ops/dispatch_flatbuffers.rs b/cli/ops/dispatch_flatbuffers.rs index c5ca61d6bf47a0..98a6153b3a10f1 100644 --- a/cli/ops/dispatch_flatbuffers.rs +++ b/cli/ops/dispatch_flatbuffers.rs @@ -17,7 +17,7 @@ use super::fs::{ }; use super::metrics::op_metrics; use super::net::{op_accept, op_dial, op_listen, op_shutdown}; -use super::os::{op_exec_path, op_home_dir, op_set_env, op_start}; +use super::os::{op_home_dir, op_set_env, op_start}; use super::performance::op_now; use super::permissions::{op_permissions, op_revoke_permission}; use super::process::{op_kill, op_run, op_run_status}; @@ -160,7 +160,6 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option { msg::Any::CreateWorker => Some(op_create_worker), msg::Any::Cwd => Some(op_cwd), msg::Any::Dial => Some(op_dial), - msg::Any::ExecPath => Some(op_exec_path), msg::Any::Fetch => Some(op_fetch), msg::Any::FetchSourceFile => Some(op_fetch_source_file), msg::Any::FormatError => Some(op_format_error), diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index 9d1b096526bd34..f013381ff5df21 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -32,6 +32,7 @@ pub const OP_WRITE: OpId = 2; pub const OP_EXIT: OpId = 3; pub const OP_IS_TTY: OpId = 4; pub const OP_ENV: OpId = 5; +pub const OP_EXEC_PATH: OpId = 6; pub fn dispatch( state: &ThreadSafeState, @@ -54,6 +55,9 @@ pub fn dispatch( dispatch_json::dispatch(os::op_is_tty, state, control, zero_copy) } OP_ENV => dispatch_json::dispatch(os::op_env, state, control, zero_copy), + OP_EXEC_PATH => { + dispatch_json::dispatch(os::op_exec_path, state, control, zero_copy) + } OP_FLATBUFFER => dispatch_flatbuffers::dispatch(state, control, zero_copy), _ => panic!("bad op_id"), }; diff --git a/cli/ops/os.rs b/cli/ops/os.rs index 99b03c720ea6e4..af635140d67047 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -110,32 +110,16 @@ pub fn op_home_dir( pub fn op_exec_path( state: &ThreadSafeState, - base: &msg::Base<'_>, - data: Option, -) -> CliOpResult { - assert!(data.is_none()); - let cmd_id = base.cmd_id(); - + _args: Value, + _zero_copy: Option, +) -> Result { state.check_env()?; - - let builder = &mut FlatBufferBuilder::new(); let current_exe = std::env::current_exe().unwrap(); - // Now apply URL parser to current exe to get fully resolved path, otherwise we might get - // `./` and `../` bits in `exec_path` + // Now apply URL parser to current exe to get fully resolved path, otherwise + // we might get `./` and `../` bits in `exec_path` let exe_url = Url::from_file_path(current_exe).unwrap(); - let path = exe_url.to_file_path().unwrap().to_str().unwrap().to_owned(); - let path = Some(builder.create_string(&path)); - let inner = msg::ExecPathRes::create(builder, &msg::ExecPathResArgs { path }); - - ok_buf(serialize_response( - cmd_id, - builder, - msg::BaseArgs { - inner: Some(inner.as_union_value()), - inner_type: msg::Any::ExecPathRes, - ..Default::default() - }, - )) + let path = exe_url.to_file_path().unwrap(); + Ok(JsonOp::Sync(json!(path))) } pub fn op_set_env( diff --git a/js/dispatch.ts b/js/dispatch.ts index 6ad54891dc7538..4b3fbdc780ac24 100644 --- a/js/dispatch.ts +++ b/js/dispatch.ts @@ -10,6 +10,7 @@ export const OP_WRITE = 2; export const OP_EXIT = 3; export const OP_IS_TTY = 4; export const OP_ENV = 5; +export const OP_EXEC_PATH = 6; export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void { switch (opId) { @@ -23,6 +24,7 @@ export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void { case OP_EXIT: case OP_IS_TTY: case OP_ENV: + case OP_EXEC_PATH: json.handleAsyncMsgFromRust(opId, ui8); break; default: diff --git a/js/dispatch_json.ts b/js/dispatch_json.ts index f8c739682e6c4d..af010a196d7ea7 100644 --- a/js/dispatch_json.ts +++ b/js/dispatch_json.ts @@ -13,7 +13,7 @@ function nextPromiseId(): number { } */ -export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void { +export function handleAsyncMsgFromRust(_opId: number, _ui8: Uint8Array): void { return util.unreachable(); /* const { promiseId, result } = record; @@ -23,8 +23,9 @@ export function handleAsyncMsgFromRust(opId: number, ui8: Uint8Array): void { */ } -export function sendSync(opId: number, obj: object = {}): undefined | object { - const s = JSON.stringify(obj); +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function sendSync(opId: number, req: any): any { + const s = JSON.stringify(req); const msg = new TextEncoder().encode(s); const resUi8 = core.dispatch(opId, msg); if (!resUi8) { diff --git a/js/os.ts b/js/os.ts index a6d7a3747e3e10..d874340ed37e86 100644 --- a/js/os.ts +++ b/js/os.ts @@ -24,7 +24,7 @@ function setGlobals(pid_: number, noColor_: boolean): void { * console.log(Deno.isTTY().stdout); */ export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } { - return dispatchJson.sendSync(dispatch.OP_IS_TTY) as any; + return dispatchJson.sendSync(dispatch.OP_IS_TTY); } /** Exit the Deno process with optional exit code. */ @@ -45,7 +45,7 @@ export function exit(code = 0): never { * console.log(myEnv.TEST_VAR == newEnv.TEST_VAR); */ export function env(): { [index: string]: string } { - return dispatchJson.sendSync(dispatch.OP_ENV) as any; + return dispatchJson.sendSync(dispatch.OP_ENV); } /** Send to the privileged side that we have setup and are ready. */ @@ -119,12 +119,5 @@ export function homeDir(): string { * Requires the `--allow-env` flag. */ export function execPath(): string { - const builder = flatbuffers.createBuilder(); - const inner = msg.ExecPath.createExecPath(builder); - const baseRes = sendSync(builder, msg.Any.ExecPath, inner)!; - assert(msg.Any.ExecPathRes === baseRes.innerType()); - const res = new msg.ExecPathRes(); - assert(baseRes.inner(res) != null); - const path = res.path()!; - return path; + return dispatchJson.sendSync(dispatch.OP_EXEC_PATH); }