jscheid / prettier.el

Prettier code formatting for Emacs.
GNU General Public License v3.0
168 stars 12 forks source link

Find Prettier in nested package.json project #118

Closed bolivier closed 1 year ago

bolivier commented 1 year ago

Is your feature request related to a problem? Please describe.

I primarily work in a codebase with a root level package.json and nested package.json files. In this instance the root package.json has the prettier dependency listed.

prettier.el only searches for the most immediate package.json file and then moves on to other locations.

Describe the solution you'd like I would like the search process to check nested package.json files so that if it's installed in the project, but not the most immediate package.json, prettier.el will work as normal.

Describe alternatives you've considered What has worked for most of the time is using a global install pinned to the version I use, but I recently noticed that prettier.el is not using the globally installed version of prettier.

Additional context Add any other context or screenshots about the feature request here.

I'm happy to work on a solution to this myself, but I'm not particularly well versed in building things with elisp.

jscheid commented 1 year ago

Hi, thanks for the detailed write-up. It definitely sounds like something that should work better.

Could you share a few more details about your setup? I'm pretty sure I've used prettier.el many times with packages nested via yarn workspace and things always seemed to work as they should. I assume you're not using Yarn?

bolivier commented 1 year ago

That's right, I'm using npm and not yarn.

The project structure is something like this, and as noted prettier is only installed in the root package.json

my-app/
├─ client/
|  ├─ package.json (for client)
├─ const/
│  ├─ package.json (for const)
├─ package.json (for root, where prettier is installed)
├─ const/
│  ├─ index.js

As best I can tell, it's defaulting to an older globally installed version in the client code. I've managed to sync the version number from M-x prettier-info and the root package.json.

bolivier commented 1 year ago

@jscheid I think I have more context on this, which is that my nested package.json has a dependency install of prettier and that's what's being used.

Ideally I could manually set a version/executable of Prettier to use, but given the js server, I'm not sure the best way to go about configuring that. Do you have any thoughts on it?

jscheid commented 1 year ago

my nested package.json has a dependency install of prettier and that's what's being used.

I assume you mean a transient dependency. Can you install a non-transient (direct) dependency on prettier in the package? If so, that one should be picked up instead of the transient dep. (I'm not sure I've ever tested this but if it doesn't work, I'd consider that a bug.)

Alternatively, can you remove the transient dep? A couple ways I can think of:

Ideally I could manually set a version/executable of Prettier to use

There's currently no provision for doing this: the way to select the Prettier version is to install it at that location (via package.json.) We could conceivably add an override but that would be some work and it would complicate matters, I'd like to avoid it if possible.

We could consider ignoring transient deps on Prettier, that seems right (since the dep is "unintentional".) It also sounds complicated though, I'm not aware of a robust and reasonably simple way to tell apart transient and direct dependencies, one that works with all package managers.

seh commented 1 year ago

s/transient/transitive/g, right?

bolivier commented 1 year ago

the way to select the Prettier version is to install it at that location

It works as expected when I install it as a dev dependency.

I still think it'd be nice to select an install manually, but it's probably best to leave it as is. My issue is annoying but should be resolved outside this package, I think. Closing since there's nothing to address here.