Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base64 and cursorOffset can break prettier #369

Open
hyrious opened this issue Jun 6, 2023 · 0 comments
Open

Base64 and cursorOffset can break prettier #369

hyrious opened this issue Jun 6, 2023 · 0 comments

Comments

@hyrious
Copy link
Contributor

hyrious commented Jun 6, 2023

Related: #301 #60

TLDR: The plugin converts script contents into base64 and restore it later. This approach can break prettier when enabled cursorOffset because prettier will then try to restore the cursor position with the diff package, which breaks in such large input (each base64 character is considered a single word).

Here's the minimal reproduction:

# initialize
mkdir test && cd test
npm init -y && npm i prettier prettier-plugin-svelte

# get a reasonably sized (11KB) svlete file
curl -O https://raw.githubusercontent.com/sveltejs/vite-plugin-svelte/main/packages/vite-plugin-svelte-inspector/src/runtime/Inspector.svelte

touch a.mjs # see below
// a.mjs
import fs from "fs";
import prettier from "prettier";

const contents = fs.readFileSync("Inspector.svelte", "utf8");

console.time("format");
const result = prettier.formatWithCursor(contents, {
  ...config,
  parser: 'svelte',
  // cursorOffset: 0,
});

console.timeEnd("format");
console.log(result.formatted.length);

Run node a.mjs, it finishes in 300ms in my machine.

Now, try uncomment // cursorOffset: 0,, this process will hang forever.

Instructions about profile a node process and get flamegraph

Before continuing, update the script above to wait some seconds before it goes to hang, so that we could inspect into this process later.

Run node --inspect a.mjs, then open chrome://inspect to run profile on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant