-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Eslint/Prettier Support #5612
Comments
@jamesdbruner have you seen https://zed.dev/docs/javascript? It showcases an example in which you customize |
Yes, I've seen that yes I can get prettier to format my code by doing that. I don't think you should have to install prettier globally when I have it installed at the root of my repository and also I'd prefer to let eslint dictate how and when prettier runs I guess is my point. Is it possible to do something like this? "language_overrides": {
"TypeScript": {
"format_on_save": {
"external": {
"command": "eslint",
"arguments": [
"--stdin-filepath",
"{buffer_path}",
"--fix"
]
}
}
}
} |
Thanks for the feedback, @jamesdbruner! And thanks @kevinsjoberg for providing support on this! ✨
This should already be possible, as we run the command from the root of the worktree that you opened. So you should be able to run things like "language_overrides": {
"JavaScript": {
"format_on_save": {
"external": {
"command": "npx", // you could also specify "node_modules/.bin/prettier" here
"arguments": [
"prettier",
"--stdin-filepath",
"{buffer_path}"
]
}
}
}
}
As for eslint, they support reading files from stdin but I am afraid they don't support reporting the fixed output on stdout. I saw they support reporting fixes as JSON to stdout, so you can probably write a simple script that invokes eslint. I was able to make it work using https://github.com/mantoni/eslint_d.js and the following configuration: "language_overrides": {
"JavaScript": {
"format_on_save": {
"external": {
"command": "npx",
"arguments": [
"eslint_d",
"--stdin",
"--stdin-filename",
"{buffer_path}",
"--fix-to-stdout"
]
}
}
}
} I understand this is suboptimal and we should probably support a more out-of-the-box experience, but would the above work for you? |
@as-cii Thanks so much for getting back to me! I'll try this tonight and let you know how it goes. Honestly so long as it works, I don't really mind adding configuration and considering how npx works, I believe it'll use my local eslint before reaching out to the web to download it. I think that should be a good solution until something out of the box is released. |
I tried this but the experience is not so great. Formatting takes some time since it has to boot up Node each time, and the cursor is moved the the start of the line each time after formatting. I'm sure this will be solved with user plugins :) |
Should we make another issue about inline ESlint error/warning support? Personally, I'm interested in a language-server-like experience for ESLint similar to the extension for VS Code. On another note, @fnky is right about prettier being slow when used like this. |
@morajabi Yeah +1, especially when considering how a lot of enterprise apps are built using Prettier and ESLint together. If these technologies work great out of the box, it will help with Zed's adoption. Edit: have been using Zed for a few hours now running the formatter using Prettier, since that's what we use for all frontend projects at my company. On every save, there's a noticeable ~1s delay from the moment |
I have created feature request for that #4901 |
Maybe in here is a great person to told me how I can run this prettier on save with tsx files?
When I use in terminal I test it changing file extension to JSX and change language in override from TypeScript to JavaScript and then everything works.... but why not work with TS? |
Hi everyone, Thanks to all tips here how I've succeed to format properly my TSX files with both Here my
Here my Zed settings:
I'm using https://github.com/mantoni/eslint_d.js as recommended by @as-cii to be able to handle properly |
I'm trying out zed and a bit disappointed that my .eslintrc config isn't supported for diagnostics. My specific use case is that I am developing a I can even configure the warnings to show up as errors in .eslintrc config and they still don't show up. It appears that .eslintrc is being ignored by zed. |
ESLint is very on our radar, and should be tackled soon! |
I believe eslint is already integrated into typescript-language-server, so in theory my use case should be covered by zed.dev as long as typescript-language-server supports this. I have submitted an issue there as well that may resolve this. I'll update here if anything comes from it typescript-language-server/typescript-language-server#708 |
Wait — I was confused eslint Isnt integration into typescript language server at all |
But is integrated here: https://github.com/microsoft/vscode-eslint/tree/main/server so your idea still makes sense. I mean using eslint as a LSP. |
@iamnbutler Great to hear that it's on your radar 🎉 Is there a way to track progress on planned features? |
@iamnbutler Would it also be possible to group |
Sounds like a good idea if we can make it work. If there isn't already an issue it would be great if you could create one! |
I used this setup but "format_on_save": {
"external": {
"command": "node_modules/.bin/eslint_d",
"arguments": [
"--stdin",
"--fix",
"--fix-to-stdout",
"--stdin-filename",
"{buffer_path}"
]
}
} When I use this setup it works properly, but
And I don't see a way to have both configured, since I can't use an array on |
Looking forward for this integration. ATM this is my most anticipated feature and would be awesome when we can have the same interaction as in vscode 👍 |
this is really needed for any dev work involving web dev, eslint language server support is definitely needed |
Is this really necessary?The ability to explicitly fine-tune formatting is nice; however, a specific property name to target formatting, linting, etc. seems too granular here. Perhaps a generic approach can grant greater flexibility in this case? As an aside
So far, I really have enjoyed using Zed and will continue to do so, which is why I'm here today! Improve the shortcomings in other toolsI see potentially greater gains with an option to enable project-level inheritance, particularly over automatable tasks such as formatting, as a more desirable enhancement. Much of these mundane tasks are being automated with tools like Git Actions or libraries such as lint-staged or husky. This helps abstract non-prod items but it feels short-sighted and hacky, certainly does not resolve the bloat or decouple/abstract tools not required at runtime. I know I have certainly spent entirely too much of my life trying to improve Bridging the gapsImplementing something that makes those tools obsolete feels like the right goal of an IDE. Taking that a step further, if an IDE can replace any need for Although everything below contradicts what I just mentioned, I have a few ideas to contribute that may help move toward what I mentioned above in a more succinct and iterative way, maybe... Root directory assignmentAn option to designate
{
"root_dir": "~/dev/web/%PROJECT_NAME%", // can reach `node_modules` and `package.json`
"extends": "~/dev/lib/%PROJECT_NAME%" // can reach `.zed/settings.json` to use as default
} A more generic way to augment
|
Been following this thread for a while, and am using eslint_d with prettier integration on save. Wanted to share a little trick that someone might find useful: Use a separate eslint config file for your format on save action, so you still get the benefit of useful errors and warnings while editing, while maintaining integrated prettier on save! Why / how / if you need details: prettier advises to keep prettier and eslint separate, primarily because every prettier correction is interpreted as a warning / error while writing code, which can slow things down, and takes away a huge benefit of eslint: to see actual errors and warnings while you are writing. If every line is an error, they are meaningless. To get around this, while still running eslint fix and prettier on save, use eslint_d, and create a separate .eslintrc.json files(for example, .eslintrc.json, and .eslintrc.withprettier.json). .eslintrc is detected by zed, so should just have your lint rules you want to see in the editor. .eslintrc.withprettier.json has all those rules, but also includes prettier:
Then, your formatOnSave settings will call eslint_d using the withprettier config.
Just remember, when changing eslint rules, you need to make your changes to both configs. An improvement would probably be to use .eslintrc js files, then the .prettier version would import the standard, add the prettier stuff, and return a new config with prettier included. But for those of us not constantly updating our eslint rules, this works fine! In practice, only real errors warnings are visible while editing, running a fix on one of them works well without editing formatting, and on each save, everything is fixed and formatted almost instantly!! |
@stabildev @shinebayar-g |
Also wondering this. I tried some different things, but could not get it work in zed. It only removes error. |
Any news about that ? I wan't to use my eslint config to format, not prettier or other default formatting. It works fine in VSCode but it seems really complicated to do that in Zed. I think I have to wait before use this IDE. I made this config : {
"formatter": "language_server",
"language_overrides": {
"TypeScript": {
"format_on_save": {
"external": {
"command": "node_modules/.bin/eslint",
"arguments": [
"--stdin",
"--fix",
"--fix-to-stdout",
"--stdin-filename",
"{buffer_path}"
]
}
}
},
"JavaScript": {
"format_on_save": {
"external": {
"command": "node_modules/.bin/eslint",
"arguments": [
"--stdin",
"--fix",
"--fix-to-stdout",
"--stdin-filename",
"{buffer_path}"
]
}
}
}
}
} But when I save non correct formatted file, nothing change now |
@throrin19 I moved eslint to the new issue #4325 cause this one is closed only with prettier support despite the title 🫠 |
Just to follow-up here: I closed #4325 because I merged changes that allow eslint fix-all to work on save. Also see this comment on how to "turn off" prettier: #4325 (comment) |
I have the same issue with eslint where it doesn't fix imports |
Because it's the internet, everyone is overly dramatic and you shouldn't believe everything they write 😉 There are currently 2 channels for Zed: Stable and Preview. Preview gets most of the updates at the moment, but it is a bit unstable as things can change and things are being ironed out. A bit of patience and potential contributions can help speed up the process of Zed "training to be The Killer of VSCode" lol |
I'd like to reiterate my solution above for anyone coming here still. The solution solved {
"lsp": {
"typescript-language-server": {
"initialization_options": {
"checkOnSave": {
"command": "eslint_d",
"arguments": [
"--stdin",
"--stdin-filename",
"{buffer_path}",
"--fix-to-stdout"
]
}
}
}
}
} |
Thank you for sharing! How did you manage to make zed "respect" rules in |
Update, this worked for me: {
"languages": {
"TypeScript": {
"formatter": {
"external": {
"command": "eslint_d",
"arguments": [
"--stdin",
"--stdin-filename",
"{buffer_path}",
"--fix-to-stdout"
]
}
}
},
"TSX": {
"formatter": {
"external": {
"command": "eslint_d",
"arguments": [
"--stdin",
"--stdin-filename",
"{buffer_path}",
"--fix-to-stdout"
]
}
},
"preferred_line_length": 100
}
} |
@demeralde can you expand a bit on this here?
What have you tried? What's your setup? Do you have any steps to reproduce? I wish it were as easy as merging one PR and it's fixed for everyone, but sadly it's not. There are a lot of different configurations that people use and VS Code (and its extensions!) have many fallbacks and different combinations that are hard to reproduce from the start. So we have to iterate and improve this bit by bit. For others and myself ESLint works: it shows diagnostics, it fixes everything when saved. The video here isn't fake: #8496 But that doesn't mean it works with every project yet! So I need all of your help by giving me some information on how to make it work for you :) |
For anyone looking for a pure "languages": {
"JavaScript": {
"code_actions_on_format": {
"source.fixAll.eslint": true
}
},
"TypeScript": {
"code_actions_on_format": {
"source.fixAll.eslint": true
}
}
},
"formatter": {
"external": {
"command": "eslint --fix",
"arguments": ["fmt", "--stdin", "{buffer_path}"]
}
} Another option is to use this solution #8992 (comment): Credits to @klaframboise "languages": {
"JavaScript": {
"formatter": {
"external": {
"command": "cat << EOF",
"arguments": []
}
},
"code_actions_on_format": {
"source.fixAll.eslint": true
}
}
} |
@diegofontenelle that would do the same thing twice, right? What happens when you only use |
@mrnugget I tried using both individually, but only combined I got With only the languages configuration I got it to format, but it was overwritten by the LSP formatter. After adding the formatter configuration it stopped being overwritten and worked as expected. Here is the output of the
|
@diegofontenelle haven't tried but this solution looks like should work too and do not format twice the same code: #8992 (comment) |
Indeed it does work as well, thanks! I will add it to my original answer |
Got it thanks! Yes, we need a way to only format with code actions. |
Is there a way to support |
@dev-abota you can always shell out to a formatter: https://zed.dev/docs/javascript#code-formatting Would that help? Sorry, not super familiar with |
I have already problem in latest main zed version. Eslint format correctly file but directly after that, prettier launch it as you can see in this capture : In log I can't seend eslint do something but I saw pretier do formatting :
This is my config : // Zed settings
//
// For information on how to configure Zed, see the Zed
// documentation: https://zed.dev/docs/configuring-zed
//
// To see all of Zed's default settings without changing your
// custom settings, run the `open default settings` command
// from the command palette or from `Zed` application menu.
{
"features": {
"copilot": false
},
"tab_size": 4,
"ui_font_size": 16,
"buffer_font_size": 16,
"terminal": {
"font_size": 11
},
"languages": {
"JavaScript": {
"code_actions_on_format": {
"source.fixAll.eslint": true
}
},
"TypeScript": {
"code_actions_on_format": {
"source.fixAll.eslint": true
}
}
},
"show_whitespaces": "all",
"show_wrap_guides": true,
"show_completion_documentation": true
} But, If I save file twice, the second time only eslint formatting is made 😨 : There's really no way to just say, I don't want to prettier at all, disable it? Edit : I change my config and set Edit 2 : I found how to fix that. I have to set "formatter": {
"external": {
"command": "cat << EOF",
"arguments": []
}
} But after that I have an error in zed footer : |
I have the same issue on the below version: Zed: v0.128.3 (Zed) It seems prettier is clashing with ESLint, there's a flicker when saving. In the prettier LSP logs I have: stderr: Resolved config: {"tabWidth":4,"singleQuote":true,"arrowParens":"always","bracketSpacing":true,"printWidth":120,"jsxBracketSameLine":true,"semi":true,"trailingComma":"none","quoteProps":"consistent"} and in my settings.json I have: "languages": { When saving, it seems my ESLint config is applied, but then the prettier config is applied afterwards, leaving me with ESLint errors. |
Yeah, there's no way to only format via code actions. I'll try to add something this week. |
I want to use Zed and VS Code interchangeably because some features in Zed don't exist. How can I configure Zed to get similar results to VS Code? |
@hungify2022 I think you will need to wait quite a bit longer. There are a lot of features present in VSCode that will take some time to develop. And there is a massive number of plugins to VSCode that it is unrealistic to expect the same level of customisability within the near future. Hang tight and if you want try to contribute? |
Just to record this here: I merged #10121 which adds ability to only format via code actions. |
In case someone (like me) runs into this problem: as of today, the configuration map key should be "languages", not "languages_override". You can find this in the Zed docs for JavaScript.
|
I tried every way I could think of to try to get
format_on_save
to work with my existing eslint/prettier setup but I can't figure out how to make it work. I have a narwhal monorepo with aneslintrc.json
file at the root and one in every project and have it setup to use prettier as the formatter.In vscode and webstorm they pickup the nearest eslintrc and use those rules to format my file on save since I added these lines to my settings.json
Here's an example of what that root
eslintrc.json
looks like:Can I use the current
format_on_save
setting to replicate how vscode or webstorm work in this regard? I'm probably just being dumb but would really appreciate some clarification or direction. Awesome work so far, and if I can get this figured out Zed will likely be my editor of choice for the foreseeable future.The text was updated successfully, but these errors were encountered: