Add --cache-location option#13019
Conversation
|
If multiple projects use the same cache dir, we'll hit the wrong cache, I guess use absolute path make sense now. |
| /** | ||
| * If a file path is passed, that file is used as the cache file. | ||
| * If a directory path is passed, | ||
| * a file with the name hashed process.cwd() is created in that directory and used as a cache file. |
There was a problem hiding this comment.
Thanks for your work!
I think that information should be included in the changelog and CLI doc.
@fisker Sorry I don't know what do you mean. Could you share some examples? |
|
@fisker Friendly ping |
|
As far as I understand, we store path related to PWD in cache, if multiple projects use a global dir to cache , and they have files with same name, won't it cause problem or at least make another project loose cache? |
|
@fisker I think it should be exptected, otherwise cache on CI will not work |
|
IDK, I won't use it anyway. |
|
I have one more concern, is it a good idea to accept both directory and file path? |
No, I'll fix it. |
24f06af to
45f441b
Compare
|
done |
@fisker Are you saying that we should only accept absolute paths as |
|
No, I meant we should use absolute path of formatting file when save cache. |
@fisker I got it. So can we merge this PR for now? I don't want to increase the size of one PR. I'll create new PR to use absolute path later. |
|
Did you mean to support path to cache file? The changelog and docs still says "directory or file path". |
| const stat = await statSafe(cacheFilePath); | ||
| if (stat) { | ||
| await fs.unlink(cacheFilePath); | ||
| } |
There was a problem hiding this comment.
If I didn't get this wrong, this line is going to remove default cache file when running Prettier without --cache, but I don't think we should. People may use Prettier without --cache temporarily.
There was a problem hiding this comment.
This is a direct implementation of ESLint's behavior and has nothing to do with --cache-location. It will be fixed in another PR.
| } | ||
| const cwd = process.cwd(); | ||
| const normalized = path.normalize(cacheLocation); | ||
| return path.join(cwd, normalized); |
There was a problem hiding this comment.
I don't think it's a good idea to use the input path directly, people may pass wrong path by accident, if path exists, we should verify
- It's a file not a directory
- Verify it's a valid Prettier cache file (maybe add a mark to the cache when save cache)
There was a problem hiding this comment.
It's a file not a directory
done! 9789e04
Verify it's a valid Prettier cache file (maybe add a mark to the cache when save cache)
How?
There was a problem hiding this comment.
How?
I'm not sure how the cache file is created, but is it a JSON file? There should be a way to detect if the file is created by Prettier.
There was a problem hiding this comment.
That file is a JSON file, but it is almost always huge. There is an overhead in making sure it is in the correct format. I don't think its validation is worth the overhead.
There was a problem hiding this comment.
Personally, I hope it's safer, I'll be pissed if prettier --cache --cache-location index.js src(forgot cache file path) saves cache to my index.js
There was a problem hiding this comment.
I've added JSON validation. But it consumes a lot of memory. We should use Stream in the future to implement JSON checks with low memory usage.
There was a problem hiding this comment.
Is there a specially key inside that we can confirm it's a Prettier cache file?
There was a problem hiding this comment.
I don't know, need to read the file-entry-cache implementation
There was a problem hiding this comment.
I want to break up the PR because one big PR is exhausting. This is listed in the TODO of this PR.
8a6c246 to
75a8bdb
Compare
| type: "boolean", | ||
| }, | ||
| "cache-location": { | ||
| description: "Path to the cache file or directory", |
There was a problem hiding this comment.
| description: "Path to the cache file or directory", | |
| description: "Path to the cache file", |
| async function findCacheFileFromOption(cacheLocation) { | ||
| const cwd = process.cwd(); | ||
| const normalized = path.normalize(cacheLocation); | ||
| const cacheFile = path.join(cwd, normalized); |
There was a problem hiding this comment.
Should we run path.isAbsolute() check first? Not sure how it works for /foo and D:\foo(Windows, different partition))
There was a problem hiding this comment.
process.cwd() returns absolute path. So I don't think we should run path.isAbsolute check.
There was a problem hiding this comment.
I didn't mean check cwd, cacheLocation.
There was a problem hiding this comment.
Maybe path.resolve(cacheLocation) is enough.
Co-authored-by: Holger Jeromin <[email protected]>
24c9294 to
afcbd3c
Compare
* Draft * Update * Generate * Add `truncate` * Address review * Regen * Update * Reword changelog for #13016 * Edit intro * Edit changelog for #13019 * Regenerate blog post * Address review Co-authored-by: Georgii Dolzhykov <[email protected]>
* Implement searching cache file from `--cache-location` option * Add tests * Add docs * Fix * Fix doc * Add changelog * Update snapshots * Update docs * Update docs/cli.md Co-authored-by: Holger Jeromin <[email protected]> * Fix changelog * Remove directory support * Update docs * Update tests * Update docs * Add validation for dir * Fix comment * Update option description * Validate JSON * Use `path.resolve` Co-authored-by: Holger Jeromin <[email protected]>
* Draft * Update * Generate * Add `truncate` * Address review * Regen * Update * Reword changelog for prettier#13016 * Edit intro * Edit changelog for prettier#13019 * Regenerate blog post * Address review Co-authored-by: Georgii Dolzhykov <[email protected]>
Description
Fixes #13010
Checklist
docs/directory).changelog_unreleased/*/XXXX.mdfile followingchangelog_unreleased/TEMPLATE.md.TODO
--cache-locationoption #13019 (comment)--cache-locationoption #13019 (comment)--cache-locationoption #13019 (comment)✨Try the playground for this PR✨