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

Prettier not found if it's a dev-dependency #12731

Closed
1 task done
ExEr7um opened this issue Jun 6, 2024 · 10 comments
Closed
1 task done

Prettier not found if it's a dev-dependency #12731

ExEr7um opened this issue Jun 6, 2024 · 10 comments
Labels
bug [core label] prettier Prettier tooling support tooling An umbrella label for language tools, linters, formatters, etc

Comments

@ExEr7um
Copy link

ExEr7um commented Jun 6, 2024

Check for existing issues

  • 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 ExEr7um added admin read Pending admin review bug [core label] triage Maintainer needs to classify the issue labels Jun 6, 2024
@Moshyfawn Moshyfawn added prettier Prettier tooling support and removed triage Maintainer needs to classify the issue labels Jun 7, 2024
@JosephTLyons JosephTLyons added tooling An umbrella label for language tools, linters, formatters, etc and removed admin read Pending admin review labels Jun 10, 2024
@joefitzgerald
Copy link

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"

@mattlucock
Copy link

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.

@SomeoneToIgnore
Copy link
Contributor

SomeoneToIgnore commented Jul 10, 2024

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

#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: #10107 , PR to fix that: #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.

@ExEr7um
Copy link
Author

ExEr7um commented Jul 13, 2024

@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.

@SomeoneToIgnore
Copy link
Contributor

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:

if has_prettier_in_package_json(&package_json_contents) {
if has_prettier_in_node_modules(fs, &path_to_check).await? {
log::debug!("Found prettier path {path_to_check:?} in both package.json and node_modules");
return Ok(ControlFlow::Continue(Some(path_to_check)));

(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
Copy link
Contributor

SomeoneToIgnore commented Jul 13, 2024

@ExEr7um can you test #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
Copy link
Contributor

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 added a commit that referenced this issue Jul 13, 2024
Do not require the `prettier` dependency name to be in package.json's
[dev]Dependencies, instead just checking the `node_modules` contents.

Release Notes:

- Improved `prettier` detection to pick up its installation from
transitive dependencies
([12731](#12731)
SomeoneToIgnore added a commit that referenced this issue Jul 15, 2024
Do not require the `prettier` dependency name to be in package.json's
[dev]Dependencies, instead just checking the `node_modules` contents.

Release Notes:

- Improved `prettier` detection to pick up its installation from
transitive dependencies
([12731](#12731)
SomeoneToIgnore added a commit that referenced this issue Jul 15, 2024
Do not require the `prettier` dependency name to be in package.json's
[dev]Dependencies, instead just checking the `node_modules` contents.

Release Notes:

- Improved `prettier` detection to pick up its installation from
transitive dependencies
([12731](#12731)
@mattlucock
Copy link

@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.

@SomeoneToIgnore
Copy link
Contributor

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.

@ExEr7um
Copy link
Author

ExEr7um commented Jul 17, 2024

Thank you! Everything works as expected now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug [core label] prettier Prettier tooling support tooling An umbrella label for language tools, linters, formatters, etc
Projects
None yet
Development

No branches or pull requests

7 participants