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
Add --cache-location
option
#13019
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. |
src/cli/find-cache-file.js
Outdated
/** | ||
* 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a direct implementation of ESLint's behavior and has nothing to do with --cache-location. It will be fixed in another PR.
src/cli/find-cache-file.js
Outdated
} | ||
const cwd = process.cwd(); | ||
const normalized = path.normalize(cacheLocation); | ||
return path.join(cwd, normalized); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specially key inside that we can confirm it's a Prettier cache file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, need to read the file-entry-cache implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to break up the PR because one big PR is exhausting. This is listed in the TODO of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay?
8a6c246
to
75a8bdb
Compare
src/cli/constant.js
Outdated
@@ -76,6 +76,10 @@ const options = { | |||
description: "Only format changed files. Cannot use with --stdin-filepath.", | |||
type: "boolean", | |||
}, | |||
"cache-location": { | |||
description: "Path to the cache file or directory", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
description: "Path to the cache file or directory", | |
description: "Path to the cache file", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/cli/find-cache-file.js
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
process.cwd()
returns absolute path. So I don't think we should run path.isAbsolute
check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't mean check cwd, cacheLocation
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe path.resolve(cacheLocation)
is enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-authored-by: Holger Jeromin <mailgithub@katur.de>
24c9294
to
afcbd3c
Compare
* 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 <mailgithub@katur.de> * 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 <mailgithub@katur.de>
* 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 <thorn.mailbox@gmail.com>
Description
Fixes #13010
Checklist
docs/
directory).changelog_unreleased/*/XXXX.md
file followingchangelog_unreleased/TEMPLATE.md
.TODO
--cache-location
option #13019 (comment)--cache-location
option #13019 (comment)--cache-location
option #13019 (comment)✨Try the playground for this PR✨