Skip to content

Conversation

@thorn0
Copy link
Member

@thorn0 thorn0 commented Feb 23, 2020

Closes #6085
Supersedes #6128
Supersedes #6285

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory)
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/pr-XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

@thorn0 thorn0 added the status:wip Pull requests that are a work in progress and should not be merged label Feb 23, 2020
@thorn0 thorn0 force-pushed the expand-dirs branch 3 times, most recently from 93755e0 to e26bd2f Compare February 23, 2020 23:36
@thorn0
Copy link
Member Author

thorn0 commented Feb 23, 2020

Resolved files aren't globally sorted anymore. Instead, their order matches the order of the given patterns, and for each pattern, the corresponding group of files is sorted. This seems logical and acceptable while ditching the global sorting allows us to use generators to avoid various problems related to big arrays.

Open problems:

  1. files found by globs but all ignored by .prettierignore - warning?
  2. directory expanding + --parser: what is "supported files" then?
  3. directory expanding + glob-based config overrides: same question

@thorn0 thorn0 force-pushed the expand-dirs branch 2 times, most recently from 2a38cf6 to 5637b04 Compare February 24, 2020 00:12
@thorn0 thorn0 removed the status:wip Pull requests that are a work in progress and should not be merged label Feb 24, 2020
@thorn0 thorn0 force-pushed the expand-dirs branch 2 times, most recently from d570c01 to c896ba5 Compare February 24, 2020 12:53
@thorn0 thorn0 mentioned this pull request Feb 24, 2020
3 tasks
@thorn0 thorn0 added this to the 2.0 milestone Feb 24, 2020
.map(ext => "*" + (ext[0] === "." ? ext : "." + ext))
.concat(filenames)}}`;
}
return supportedFilesGlob;
Copy link
Member

@fisker fisker Feb 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I format exts add by config override or plugins? I think we should glob all files
and inferParser then filter the files without parser

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I format exts add by config override or plugins?

Plugins - yes. There is a test for that.

Config overrides - no. Because configs are resolved on a later stage.

I think we should glob all files and inferParser then filter the files without parser

That's a good idea. I'll give it a try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This parser inference thing looks a bit complicated though. How about merging this PR as is and improving this later?

I feel this PR is already good enough for most of the use cases. People just want to run prettier . --write or prettier src --write. For more complicated scenarios, they can use globs.

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! Couple notes

for (const pattern of context.filePatterns) {
const absolutePath = path.resolve(cwd, pattern);

if (containsIgnoredPathSegment(absolutePath, cwd, ignoredDirectories)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using micromatch here, we already have it in our deps (from fast-glob)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is only for silent ignoring files in node_modules and VCS dirs. I think it's good enough.

@thorn0 thorn0 force-pushed the expand-dirs branch 5 times, most recently from e0b374d to ae238af Compare February 26, 2020 20:05
@fisker
Copy link
Member

fisker commented Feb 26, 2020

I guess only problem is how to get supported files in dir?

@thorn0

@fisker
Copy link
Member

fisker commented Feb 26, 2020

Can you also post a log run ./bin/prettier.js . on codebase?

@lydell
Copy link
Member

lydell commented Feb 27, 2020

What are the answers to the open problems?

Open problems:

  1. files found by globs but all ignored by .prettierignore - warning?
  2. directory expanding + --parser: what is "supported files" then?
  3. directory expanding + glob-based config overrides: same question

Copy link
Member

@alexander-akait alexander-akait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job

@thorn0
Copy link
Member Author

thorn0 commented Feb 27, 2020

What are the answers to the open problems?

# 2 and # 3 are the same problem, parser inference vs using extensions.

As for # 1, Prettier 1.19.1 doesn't issue any warning if passed a glob that is fully ignored by .prettierignore. Maybe it's not a big deal if the same happens with directories.

@alexander-akait
Copy link
Member

@thorn0

directory expanding + --parser: what is "supported files" then?

Maybe we should using only parser extensions for better DX and 0cjs

@thorn0
Copy link
Member Author

thorn0 commented Feb 27, 2020

@evilebottnawi

directory expanding + --parser: what is "supported files" then?

Maybe we should using only parser extensions for better DX and 0cjs

I'd rather expect that prettier dir --parser html would match dir/**/* and use html for all files.

@fisker

glob all files from dir and yield {file, ignoreNoParserError: true} , deal with it later in handleError. This can handle unknown files add by config overrides. It seems if we do this, the NoResultsError also need move there, I didn't think about this before.

Yes. It's tricky. Not super difficult, but I'm sure that if I really start digging into it, I'll discover a dozen of new microissues there, so it might take long. The .prettierignore part looks especially suspicious. You tried to update the ignore package, so you should have an idea of what I mean.

@alexander-akait
Copy link
Member

I'd rather expect that prettier dir --parser html would match dir/**/* and use html for all files.

Yes, i think you are right. Do we have a test for this?

@duailibe
Copy link
Collaborator

I'd rather expect that prettier dir --parser html would match dir/**/* and use html for all files.

This should work exactly if we used dir/**/*, so you're right. It should just follow the current precedence order: command line args / config via API, config file (overrides and whatnot), inference based on extension.

IMO these things shouldn't block this PR if these problems already existed when using a blob, but those problems were things I wished to fix for a very long time (that would end up being a 2.0 release), that are way more important than other things people are more concerned with 😅

@thorn0
Copy link
Member Author

thorn0 commented Feb 27, 2020

I'd rather expect that prettier dir --parser html would match dir/**/* and use html for all files.

Yes, i think you are right. Do we have a test for this?

That's not how it works now. It's the same issue, namely: what exactly is "supported files" when we're searching for all "supported files" in a directory?

The current implementation in this PR: "supported files" = files with extensions and names taken from getSupportInfo().

An ideal implementation: "supported files" = glob(dir + '/**').filter(parserCanBeInferred). In the case when --parser is specified as a CLI arg, parserCanBeInferred turns into () => true.

@duailibe
Copy link
Collaborator

Ah, now I understand the problem

The current implementation in this PR: "supported files" = files with extensions and names taken from getSupportInfo().

This sounds good to me

@thorn0
Copy link
Member Author

thorn0 commented Feb 27, 2020

My proposal is to postpone the ideal implementation until 3.0.

@alexander-akait
Copy link
Member

My proposal is to postpone the ideal implementation until 3.0.

Sounds good for me, we will also collect feedback and it will help us make it better

@fisker
Copy link
Member

fisker commented Feb 27, 2020

Looks like @duailibe and @evilebottnawi both Okay with current implementation, I'm ok with it too.

But I have a question, can we somehow hide this NoParserError for both dir and glob? I'm on cellphone, I don't know how ESLint handle eslint foo.js *.jpg bar.js? I guess it throw a ParseError and abort lint progress?

@thorn0
Copy link
Member Author

thorn0 commented Feb 27, 2020

@fisker Currently (in 1.19.1 and in this PR), the "parser can't be inferred" error is not fatal. The error is logged, the file is skipped, Prettier CLI proceeds with next file. Why would we want to hide the error? ESLint doesn't have relevant behavior because it always uses the same parser, but I don't think it ever aborts the whole linting run because of a single file.

@fisker
Copy link
Member

fisker commented Feb 27, 2020

@thorn0 I was thinking if eslint abort linting when error occurs, we can also ask people pass glob with valid extensions like prettier *.{js,css} instead of prettier *. For ESLint I never use --ext, I use **/*.{js,mjs}

Update: If eslint ignore error, we can also ignore it , so we don't need find "supported files"

@thorn0
Copy link
Member Author

thorn0 commented Feb 27, 2020

@fisker Not sure how what you wrote this time is different from what you wrote before about 'I was going to do something like glob all files from dir and yield {file, ignoreNoParserError: true}'.

@fisker
Copy link
Member

fisker commented Feb 27, 2020

I'm okay with current implementation.

This is only a idea of new possible/simple solution.

@thorn0
Copy link
Member Author

thorn0 commented Feb 27, 2020

I'm actually trying to rewrite it. Let's see how it goes. If it works, I'll open a new PR.

@fisker
Copy link
Member

fisker commented Mar 10, 2020

Should we merge this and release v2.0?

@alexander-akait
Copy link
Member

/cc @prettier/core Any review will be helpful.

Copy link

@dougal83 dougal83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Great work. Could open a new PR to refactor at this point.

@alexander-akait
Copy link
Member

/cc @thorn0 Can we merge it and start to release next version?

@thorn0
Copy link
Member Author

thorn0 commented Mar 16, 2020

@evilebottnawi Okay, I'll do this today (if nobody objects).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants