Skip to content

getFileInfo does not resolve ignores correctly when filePath is absolute #5513

@BPScott

Description

@BPScott

I received the following issue on eslint-plugin-prettier: prettier/eslint-plugin-prettier#126

When passing an absolute path into getFileInfo() (or in my case getFileInfo.sync()) the line src/not_pretty.js within a .prettierignore is not honored. This results in a file being parsed by prettier when it should be ignored.

This seems to occur for any ignore line that contains a path instead of a single segment.

Environments:

  • Prettier Version: 1.15.2
  • Running Prettier via: vis eslint-plugin-prettier
  • Runtime:Node 10
  • Operating System: macOS

Steps to reproduce:

git clone https://github.com/edmorley/testcase-eslint-plugin-prettier-ignore and cd into the folder

Add the following file to the repository as check-getfileinfo.js and run it using node check-getfileinfo.js:

const path = require("path");
const prettier = require("prettier");

filePath = "src/not_pretty.js";
options = { ignorePath: ".prettierignore" };

const relativeInfo = prettier.getFileInfo.sync(filePath, options);
const relativeWithDotInfo = prettier.getFileInfo.sync(`./${filePath}`, options);
const absoluteInfo = prettier.getFileInfo.sync(path.resolve(filePath), options);

console.log("relativePath is Ignored:", relativeInfo.ignored);
console.log("relativeWithDot is Ignored:", relativeWithDotInfo.ignored);
console.log("absolutePath is Ignored:", absoluteInfo.ignored);

Expected behavior:

All console logs should return true

Actual behavior:

  • When using a relative path the ignored value is true
  • When using a relative path with a leading . the ignored value is false
  • When using an absolute path the ignored value is false

This behaviour does not occur when using the prettier CLI as all paths get resolved to be relative due to #2969. I think the solution is ensure the paths are relative when using getFileInfo() too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:apiIssues with Prettier's Application Programming Interfacelocked-due-to-inactivityPlease open a new issue and fill out the template instead of commenting.status:has prIssues with an accompanying pull request. These issues will probably be fixed soon!

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions