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

resolveConfigFile bug? #16344

Closed
Jack-Works opened this issue Jun 3, 2024 · 10 comments
Closed

resolveConfigFile bug? #16344

Jack-Works opened this issue Jun 3, 2024 · 10 comments
Labels
status:awaiting response Issues that require answers to questions from maintainers before action can be taken

Comments

@Jack-Works
Copy link

Environments:

  • Prettier Version: 3.3.0
  • Running Prettier via: API
  • Runtime: 22.2.0
  • Operating System: macOS
  • Prettier plugins (if any):

Steps to reproduce:

run in a folder with a .prettierrc file

$ node
> await require('prettier').resolveConfigFile(new URL(url.pathToFileURL(process.cwd())))
null

> await require('prettier').resolveConfigFile(process.cwd())
null

Expected behavior:

Resolve the file

Actual behavior:

null

@kachkaev
Copy link
Member

kachkaev commented Jun 3, 2024

👋 @Jack-Works! Try

await require('prettier').resolveConfigFile(require('path').resolve(process.cwd(), 'foobar.json'))

Config file is resolved relative to a file path, not dir path.

@kachkaev kachkaev closed this as not planned Won't fix, can't repro, duplicate, stale Jun 3, 2024
@kachkaev kachkaev added the type:question Questions and support requests. Please use Stack Overflow for them, not the issue tracker. label Jun 3, 2024
Copy link

github-actions bot commented Jun 3, 2024

👋 @Jack-Works, we use the issue tracker exclusively for development purposes. For questions, please use Stack Overflow.

@marcellino-ornelas
Copy link

@kachkaev any reason why this shouldn't work with directories? Seems kind of wrong to have to put some random file name just to get this to work

await require('prettier').resolveConfigFile(require('path').resolve(process.cwd(), 'foobar.json'))
//                                                                                      ^^

@fisker fisker reopened this Aug 14, 2024
@github-actions github-actions bot removed the type:question Questions and support requests. Please use Stack Overflow for them, not the issue tracker. label Aug 14, 2024
@fisker
Copy link
Member

fisker commented Aug 14, 2024

This was changed in #15363, but I intended to change behavior for resolveConfig(). It kind of make sense that resolveConfigFile() can accept a directory.

I'm curious what the usage of resolveConfigFile in your case, we don't have any API accept the config file path.

@fisker fisker added the status:awaiting response Issues that require answers to questions from maintainers before action can be taken label Aug 14, 2024
@marcellino-ornelas
Copy link

marcellino-ornelas commented Aug 15, 2024

@fisker

This was changed in #15363, but I intended to change behavior for resolveConfig().

Oh this would be awesome and actually what I need!

I'm curious what the usage of resolveConfigFile in your case, we don't have any API accept the config file path

I wasn't sure why passing a directory to resolveConfig didn't work so I though I needed to use the value from resolveConfigFile and pass that to resolveConfig.

const configPath = await prettier.resolveConfigFile(dest);
if (!configPath) return;
const config = await prettier.resolveConfig(configPath);

This is how I came across this issue because passing a directory to resolveConfigFile didn't work either so end up having to do something like this

const configPath = await prettier.resolveConfigFile(`${dest}/hey.json`);
if (!configPath) return;
const config = await prettier.resolveConfig(configPath);

All in all, if resolveConfig could accept a directory than that would 100% solve my issue

const config = await prettier.resolveConfig(dest);

@fisker
Copy link
Member

fisker commented Aug 15, 2024

We can't accept directory in .resolveConfig(), We support overrides in .prettierrc, can't tell the config without filename.

@marcellino-ornelas
Copy link

marcellino-ornelas commented Aug 15, 2024

@fisker

We support overrides in .prettierrc, can't tell the config without filename.

Can you clarify? Not sure how overrides effect this 🤔

We can't accept directory in .resolveConfig()

❓ Doesn't this function recursively just look up the file path searching for a prettier config and returns it if it finds one? 🤔

@fisker
Copy link
Member

fisker commented Aug 15, 2024

❓ Doesn't this function recursively just look up the file path searching for a prettier config and returns it if it finds one? 🤔

...mergeOverrides(result, filePath),

function mergeOverrides(configResult, filePath) {
const { config, configFile } = configResult || {};
const { overrides, ...options } = config || {};
if (filePath && overrides) {
const relativeFilePath = path.relative(path.dirname(configFile), filePath);
for (const override of overrides) {
if (
pathMatchesGlobs(
relativeFilePath,
override.files,
override.excludeFiles,
)
) {
Object.assign(options, override.options);
}
}
}
return options;
}

@marcellino-ornelas
Copy link

Ahhhh got ya! Thanks for the explanation 🙇🏽‍♂️

Copy link

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:awaiting response Issues that require answers to questions from maintainers before action can be taken
Projects
None yet
Development

No branches or pull requests

4 participants