Skip to content

Commit

Permalink
fix: initialize v8 on main thread to prevent random linux errors (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 7, 2024
1 parent 7b3e31c commit a44f776
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 38 deletions.
55 changes: 41 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ members = [
]

[workspace.dependencies]
deno_core = "0.238.0"
deno_core = "0.243.0"
dprint-core = "0.65.0"
serde = { version = "1.0.149", features = ["derive"] }
serde_json = { version = "1.0", features = ["preserve_order"] }
5 changes: 5 additions & 0 deletions base/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ impl JsRuntime {
}
}

/// Call this once on the main thread.
pub fn initialize_main_thread() {
deno_core::JsRuntime::init_platform(None)
}

pub async fn execute_format_script(&mut self, code: String) -> Result<Option<String>, Error> {
let global = self.inner.execute_script("format.js", code.into())?;
let resolve = self.inner.resolve(global);
Expand Down
1 change: 0 additions & 1 deletion js/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"dependencies": {
"buffer": "^6.0.3",
"prettier": "^3.1.1",
"prettier-plugin-astro": "^0.12.3",
"prettier-plugin-jsdoc": "^1.3.0",
"prettier-plugin-svelte": "^3.1.2",
"process": "^0.11.10",
Expand Down
12 changes: 6 additions & 6 deletions plugin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ version = "0.32.0"
edition = "2021"

[dependencies]
deno_console = "0.129.0"
deno_console = "0.130.0"
deno_core = { workspace = true }
deno_url = "0.129.0"
deno_webidl = "0.129.0"
deno_url = "0.130.0"
deno_webidl = "0.130.0"
dprint-core = { workspace = true, features = ["process"] }
dprint-plugin-deno-base = { version = "0.1.0", path = "../base" }
once_cell = "1.18"
serde = { workspace = true }
serde_json = { workspace = true }

[build-dependencies]
deno_console = "0.129.0"
deno_console = "0.130.0"
deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] }
dprint-plugin-deno-base = { version = "0.1.0", features = ["build"], path = "../base" }
deno_url = "0.129.0"
deno_webidl = "0.129.0"
deno_url = "0.130.0"
deno_webidl = "0.130.0"
sha256 = "1.4.0"
zstd = "0.13.0"

Expand Down
2 changes: 2 additions & 0 deletions plugin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dprint_core::plugins::process::get_parent_process_id_from_cli_args;
use dprint_core::plugins::process::handle_process_stdio_messages;
use dprint_core::plugins::process::start_parent_process_checker_task;
use dprint_plugin_deno_base::runtime::JsRuntime;
use dprint_plugin_deno_base::util::create_tokio_runtime;
use handler::PrettierPluginHandler;

Expand All @@ -9,6 +10,7 @@ mod formatter;
mod handler;

fn main() {
JsRuntime::initialize_main_thread();
let runtime = create_tokio_runtime();
let result = runtime.block_on(async {
if let Some(parent_process_id) = get_parent_process_id_from_cli_args() {
Expand Down
15 changes: 0 additions & 15 deletions scripts/createForTesting.ps1

This file was deleted.

21 changes: 21 additions & 0 deletions scripts/create_for_testing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env -S deno run -A

// Script for quickly creating the plugin for testing purposes on Windows
// To run:
// 1. Run `./scripts/create_plugin_file.ts`
// 2. Update dprint.json to point at ./target/release/plugin.json then update checksum
// as shown when initially run.

import $ from "dax";

await $`deno task build`;
await $`cargo build --release`;
if (Deno.build.os === "windows") {
await $`powershell -Command ${"Compress-Archive -Force -Path target/release/dprint-plugin-prettier.exe -DestinationPath target/release/dprint-plugin-prettier-x86_64-pc-windows-msvc.zip"}`;
} else if (Deno.build.os === "linux") {
await $`zip -j target/release/dprint-plugin-prettier-x86_64-unknown-linux-gnu.zip target/release/dprint-plugin-prettier`;
} else {
throw "TODO";
}

await $`cd target/release && deno run -A ../../scripts/create_plugin_file.ts --test`;
11 changes: 11 additions & 0 deletions scripts/local_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import $ from "dax";
import { getChecksum } from "https://raw.githubusercontent.com/dprint/automation/0.8.1/hash.ts";

await $`./scripts/create_for_testing.ts`;
const checksum = await getChecksum($.path("./target/release/plugin.json").readBytesSync());
const dprintConfig = $.path("dprint.json");
const data = dprintConfig.readJsonSync();
const index = data.plugins.findIndex(d => d.startsWith("./target") || d.includes("prettier"));
data.plugins[index] = `./target/release/plugin.json@${checksum}`;
dprintConfig.writeJsonPrettySync(data);
await $`dprint fmt`;
2 changes: 1 addition & 1 deletion scripts/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ $.logStep("Upgrading prettier...");
const jsNodePath = rootDirPath.join("./js/node");
await $`npm install`.cwd(jsNodePath);
await $`npm install --save prettier`.cwd(jsNodePath);
await $`npm install --save prettier-plugin-astro`.cwd(jsNodePath);
// await $`npm install --save prettier-plugin-astro`.cwd(jsNodePath);
await $`npm install --save prettier-plugin-jsdoc`.cwd(jsNodePath);
await $`npm install --save prettier-plugin-svelte`.cwd(jsNodePath);

Expand Down

0 comments on commit a44f776

Please sign in to comment.