prettier / plugin-ruby

Prettier Ruby Plugin
MIT License
1.47k stars 98 forks source link

Formatting is failing for certain files after #1407 #1410

Closed bschlenk closed 11 months ago

bschlenk commented 1 year ago

It seems like after https://github.com/prettier/plugin-ruby/pull/1407, formatting certain files in my repo isn't working. I have a monorepo and there are multiple Gemfiles in different subdirectories. I found that trying to format files in subdirectories that don't have syntax_tree listed in the Gemfile are the ones that fail. I think this makes sense because the require "bundler/setup" is resolving based on the nearest Gemfile. It'd be nice though if I could configure the directory that the plugin spawns in so I only have to install the plugin's dependencies in the top level directory.

It may also be an issue if the plugin only gets spawned once in whatever directory happens to be the first file formatted (not sure if this is the case though).

kddnewton commented 1 year ago

Thanks @bschlenk I think that's an unintended consequence of #1407 - @hrabe could you take a look at this? We certainly shouldn't be breaking behavior in point releases, hopefully we can get this fixed without reverting.

bschlenk commented 1 year ago

I don't know if this would work for everyone, but in my case using the directory where the prettier config file lives as the cwd of the spawned ruby process works for me, both from the cli and vscode. It looks like this:

// plugin.js
...
import { resolveConfigFile } from "prettier";
...

// in spawnServer

const options = { 
  env: Object.assign({}, process.env, { LANG: getLang() }),
  stdio: ["ignore", "ignore", "inherit"],
  detached: true
};

if (opts.filepath) { 
  const prettierConfig = await resolveConfigFile(opts.filepath)
  options.cwd = path.dirname(prettierConfig)
}