From 958f21e7abc36f0a5abaa381ed8d7f94c723f3fb Mon Sep 17 00:00:00 2001 From: Adam Odziemkowski Date: Fri, 29 May 2020 02:43:31 -0400 Subject: [PATCH] fix(cli): write lock file before running any code (#5794) --- cli/main.rs | 33 +++++++++++++++------------------ cli/tests/file_exists.ts | 6 ++++++ cli/tests/lock_write_fetch.ts | 7 ++++++- 3 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 cli/tests/file_exists.ts diff --git a/cli/main.rs b/cli/main.rs index 68ba5e7763..16ca41fe43 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -139,6 +139,19 @@ fn write_to_stdout_ignore_sigpipe(bytes: &[u8]) -> Result<(), std::io::Error> { } } +fn write_lockfile(global_state: GlobalState) -> Result<(), std::io::Error> { + if global_state.flags.lock_write { + if let Some(ref lockfile) = global_state.lockfile { + let g = lockfile.lock().unwrap(); + g.write()?; + } else { + eprintln!("--lock flag must be specified when using --lock-write"); + std::process::exit(11); + } + } + Ok(()) +} + fn create_main_worker( global_state: GlobalState, main_module: ModuleSpecifier, @@ -330,15 +343,7 @@ async fn cache_command(flags: Flags, files: Vec) -> Result<(), ErrBox> { worker.preload_module(&specifier).await.map(|_| ())?; } - if global_state.flags.lock_write { - if let Some(ref lockfile) = global_state.lockfile { - let g = lockfile.lock().unwrap(); - g.write()?; - } else { - eprintln!("--lock flag must be specified when using --lock-write"); - std::process::exit(11); - } - } + write_lockfile(global_state)?; Ok(()) } @@ -514,18 +519,10 @@ async fn run_command(flags: Flags, script: String) -> Result<(), ErrBox> { create_main_worker(global_state.clone(), main_module.clone())?; debug!("main_module {}", main_module); worker.execute_module(&main_module).await?; + write_lockfile(global_state)?; worker.execute("window.dispatchEvent(new Event('load'))")?; (&mut *worker).await?; worker.execute("window.dispatchEvent(new Event('unload'))")?; - if global_state.flags.lock_write { - if let Some(ref lockfile) = global_state.lockfile { - let g = lockfile.lock().unwrap(); - g.write()?; - } else { - eprintln!("--lock flag must be specified when using --lock-write"); - std::process::exit(11); - } - } Ok(()) } diff --git a/cli/tests/file_exists.ts b/cli/tests/file_exists.ts new file mode 100644 index 0000000000..5fc5414b38 --- /dev/null +++ b/cli/tests/file_exists.ts @@ -0,0 +1,6 @@ +try { + await Deno.open(Deno.args[0]); + Deno.exit(0); +} catch (e) { + Deno.exit(1); +} diff --git a/cli/tests/lock_write_fetch.ts b/cli/tests/lock_write_fetch.ts index 9e71905965..5a4dea0ce9 100644 --- a/cli/tests/lock_write_fetch.ts +++ b/cli/tests/lock_write_fetch.ts @@ -32,6 +32,8 @@ const fetchCheckProc = Deno.run({ const fetchCheckProcCode = (await fetchCheckProc.status()).code; console.log(`fetch check code: ${fetchCheckProcCode}`); +Deno.removeSync("./lock_write_fetch.json"); + const runProc = Deno.run({ stdout: "null", stderr: "null", @@ -39,7 +41,10 @@ const runProc = Deno.run({ Deno.execPath(), "run", "--lock=lock_write_fetch.json", - "https_import.ts", + "--lock-write", + "--allow-read", + "file_exists.ts", + "lock_write_fetch.json", ], });