zed icon indicating copy to clipboard operation
zed copied to clipboard

Prettier not found if it's a dev-dependency

Open ExEr7um opened this issue 1 year ago • 7 comments

Check for existing issues

  • [X] Completed

Describe the bug / provide steps to reproduce it

I have my personal Prettier config in an npm package, and prettier is a dev-dependency of that config. When I try to use Prettier in Zed, it falls back to default Prettier because package.json doesn't have Prettier dependency in it, it only has my config dependency.

In my config I have some prettier plugins, which are not installed in default Prettier, so formatting fails.

In VSCode it works fine.

Environment

Zed: v0.138.4 (Zed) OS: macOS 14.5.0 Memory: 32 GiB Architecture: aarch64

If applicable, add mockups / screenshots to help explain present your vision of the feature

No response

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue.

2024-06-06T15:17:00+03:00 [WARN] Skipping path "/Users/exer7um/Работа/Филиал Ботанов/olymp-lk-patient-frontend" that has no prettier dependency and no workspaces section in its package.json
2024-06-06T15:17:00+03:00 [INFO] Waiting for default prettier to install
2024-06-06T15:17:00+03:00 [INFO] Starting prettier at path "/Users/exer7um/Library/Application Support/Zed/prettier"
2024-06-06T15:17:00+03:00 [INFO] Node runtime install_if_needed
2024-06-06T15:17:00+03:00 [INFO] starting language server. binary path: "/Users/exer7um/Library/Application Support/Zed/node/node-v18.15.0-darwin-arm64/bin/node", working directory: "/Users/exer7um/Library/Application Support/Zed/prettier", args: ["/Users/exer7um/Library/Application Support/Zed/prettier/prettier_server.js", "/Users/exer7um/Library/Application Support/Zed/prettier"]
2024-06-06T15:17:00+03:00 [INFO] Started default prettier in "/Users/exer7um/Library/Application Support/Zed/prettier"
2024-06-06T15:17:00+03:00 [ERROR] crates/editor/src/editor.rs:9199: default prettier instance failed to format buffer

Caused by:
    0: prettier format request
    1: error during message '{"jsonrpc":"2.0","id":2,"method":"prettier/format","params":{"text":"..snip..","options":{"plugins":[],"parser":"vue","filepath":"/Users/exer7um/Работа/Филиал Ботанов/olymp-lk-patient-frontend/components/TheLogo.vue","prettierOptions":{"tabWidth":2,"printWidth":80,"useTabs":false}}}}' handling: Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'prettier-plugin-css-order' imported from /Users/exer7um/Library/Application Support/Zed/prettier/noop.js

ExEr7um avatar Jun 06 '24 12:06 ExEr7um

I think the main issue here is that the working directory for prettier is not being set to the project root that Zed has open. You can see this in the provided logs:

working directory: "/Users/exer7um/Library/Application Support/Zed/prettier"

joefitzgerald avatar Jun 11 '24 04:06 joefitzgerald

This issue is currently a blocker for me adopting Zed. Indeed, the problem appears to be that, given a formatting command to run, Zed is executing that command in its app config directory (~/.config/zed for me) rather than in the project directory. Two years ago in #5612, @as-cii said that the command is executed in the project directory, so I don't know if this is an unintentional regression or an intentional change. What I do know is that this behavior is undocumented.

I think a bigger issue here is that Zed currently makes no attempt to detect whether a local version of Prettier exists in the project. The current assumption seems to be that using the local version is a niche desire, and for all I know it is, but this what I want to do in literally every project ever—not least because I was hoping to do this as a workaround for the frustrating issue of #8534.

mattlucock avatar Jul 10 '24 09:07 mattlucock

@mattlucock (or anybody else who experiences this issue), I would love to get a project example or at least to know about it more.

https://github.com/zed-industries/zed/pull/8837 should have fixed issues with the working directory (and there was a set of prettier-related fixes around that time)

The main remaining culprit could be yarn support: it's nonexistent (issue: https://github.com/zed-industries/zed/issues/10107 , PR to fix that: https://github.com/zed-industries/zed/pull/13644) So maybe it does not work due to you using yarn? It should work with npm and pnpm though.

Another potential issue could be due to npm i or similar not run so that there's no trace of prettier in node_modules/ where Zed's prettier intergration tries to look it up.

I've tried to replicate the original issue (as the only few code-related details in this issue) with

vue create my-vue-project

Selecting
? Please pick a preset: Default ([Vue 3] babel, eslint)
? Pick the package manager to use when installing dependencies: NPM

cd my-vue-project
npm install --save-dev prettier prettier-plugin-css-order

and the following extra .prettierrc config on top:

{
    "singleQuote": true,
    "semi": false,
    "trailingComma": "es5",
    "printWidth": 80,
    "plugins": ["prettier-plugin-css-order"]
}

and that seems to work: the file gets formatted, LSP logs show that all plugins were picked correctly: image

So, if the project could not be shared, not based on yarn and does not work still, more log output (~/Library/Logs/Zed/Zed.log one and the LSP one from the screenshot) is needed.

SomeoneToIgnore avatar Jul 10 '24 10:07 SomeoneToIgnore

@SomeoneToIgnore Yes, I can provide a project. For me, it happens in https://github.com/exer7um/exer7um.com.

I have @exer7um/prettier-config in my dependencies, and Prettier is a dependency of my config. So, I don’t have Prettier specifically listed in package.json, and that’s why the issue happens.

If I add Prettier to the dependencies, then everything works fine. In your example, you install Prettier as a dependency, so it works correctly.

ExEr7um avatar Jul 13 '24 09:07 ExEr7um

Thank you so much! You're the first one since June who answered my pledges and bothered to share a project.

This case is relatively easy to fix as currently, Zed checks every package.json with prettier word in its [dev]Dependencies: https://github.com/zed-industries/zed/blob/89fbd6528f688cc98eaa4b3002f4b41da6c7b520/crates/prettier/src/prettier.rs#L95-L98

(or walks up the stack searching for workspace entries to support monorepos).

The main reason for doing so was to avoid cases when some "stale" prettier is in node_modules but not the dependency declaration — but after my tests seems that both npm i and pnpm i would remove such stray thing, so I think we can ignore that entirely and hope that the used did things right, same way as we hope for that already.

I hope to fix this before end of Monday, but feel free to do the same if you want to .


@mattlucock , I'm very interested in your case still, you've got such a fiery intro with the words blocker which made me hope I could get some answer, not just silence.

SomeoneToIgnore avatar Jul 13 '24 10:07 SomeoneToIgnore

@ExEr7um can you test https://github.com/zed-industries/zed/pull/14403 ? I'm especially interested in monorepo project set-ups as I do not have good examples of these.

But on my test projects and https://github.com/exer7um/exer7um.com , it works now.

SomeoneToIgnore avatar Jul 13 '24 14:07 SomeoneToIgnore

Found a monorepo project example, seems to work for me too. So I'll merge this and let it close the issue — so far it seems that everything actionable was fixed.

SomeoneToIgnore avatar Jul 13 '24 18:07 SomeoneToIgnore

@SomeoneToIgnore I did not at all intend saying that this issue was a blocker to be 'fiery'; it was actually supposed to be calm and objective. The bar to me adopting Zed is high since I already have something that works well for me. Therefore, incompatibilities with existing projects that appear to simply be bugs or similar issues with Zed are reasons why I can't adopt Zed. I also was definitely not intentionally ignoring you, which you seemed to infer from me taking longer than three days to reply to you.

Upon reflection I think I misread this issue. The problem I was having was very related and appeared to manifest in a very similar way, but I don't think it was actually the same issue. I apologise for the confusion.

I hadn't touched Zed since last week, but launching it today and performing an update to the latest version, the specific issue I was having appears to be magically resolved now; the exact same configuration that was failing a week ago in the manner I described is suddenly working now. I'm not sure what happened and I'm fine to not find out. If OP's issue has been resolved, you can close this issue.

mattlucock avatar Jul 17 '24 01:07 mattlucock

Thank you so much for the update.

Please, ping me in any of the other issues you find especially irritating and I'll try to work around them.

SomeoneToIgnore avatar Jul 17 '24 10:07 SomeoneToIgnore

Thank you! Everything works as expected now.

ExEr7um avatar Jul 17 '24 19:07 ExEr7um