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

Add --cache-location option #13019

Merged
merged 19 commits into from Jul 31, 2022
Merged

Conversation

sosukesuzuki
Copy link
Member

@sosukesuzuki sosukesuzuki commented Jun 18, 2022

Description

Fixes #13010

Checklist

  • 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/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

TODO

Try the playground for this PR

@fisker
Copy link
Member

fisker commented Jun 19, 2022

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.
Copy link
Contributor

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.

docs/cli.md Outdated Show resolved Hide resolved
@sosukesuzuki
Copy link
Member Author

If multiple projects use the same cache dir, we'll hit the wrong cache, I guess #12800 (comment) make sense now.

@fisker Sorry I don't know what do you mean. Could you share some examples?

@sosukesuzuki
Copy link
Member Author

@fisker Friendly ping

@fisker
Copy link
Member

fisker commented Jun 27, 2022

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?

@alexander-akait
Copy link
Member

@fisker I think it should be exptected, otherwise cache on CI will not work

@fisker
Copy link
Member

fisker commented Jun 28, 2022

IDK, I won't use it anyway.

@fisker
Copy link
Member

fisker commented Jun 28, 2022

I have one more concern, is it a good idea to accept both directory and file path?

@sosukesuzuki
Copy link
Member Author

I have one more concern, is it a good idea to accept both directory and file path?

No, I'll fix it.

@sosukesuzuki
Copy link
Member Author

done

@sosukesuzuki
Copy link
Member Author

If multiple projects use the same cache dir, we'll hit the wrong cache, I guess #12800 (comment) make sense now.

@fisker Are you saying that we should only accept absolute paths as --cache-location?

@fisker
Copy link
Member

fisker commented Jul 9, 2022

No, I meant we should use absolute path of formatting file when save cache.

@sosukesuzuki
Copy link
Member Author

sosukesuzuki commented Jul 10, 2022

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.

@sosukesuzuki sosukesuzuki requested a review from fisker July 10, 2022 04:41
@fisker
Copy link
Member

fisker commented Jul 10, 2022

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);
}
Copy link
Member

@fisker fisker Jul 10, 2022

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.

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 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);
Copy link
Member

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)

Copy link
Member Author

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?

Copy link
Member

@fisker fisker Jul 10, 2022

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.

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

afcbd3c

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.

Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member Author

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay?

@@ -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",
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
description: "Path to the cache file or directory",
description: "Path to the cache file",

Copy link
Member Author

Choose a reason for hiding this comment

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

async function findCacheFileFromOption(cacheLocation) {
const cwd = process.cwd();
const normalized = path.normalize(cacheLocation);
const cacheFile = path.join(cwd, normalized);
Copy link
Member

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))

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

@sosukesuzuki sosukesuzuki requested a review from fisker July 31, 2022 13:49
@sosukesuzuki sosukesuzuki merged commit 955553b into prettier:main Jul 31, 2022
@sosukesuzuki sosukesuzuki deleted the cache-location branch July 31, 2022 13:58
@kachkaev kachkaev added this to the 2.8 milestone Nov 16, 2022
thorn0 added a commit to sosukesuzuki/prettier that referenced this pull request Nov 22, 2022
sosukesuzuki added a commit that referenced this pull request Nov 23, 2022
* 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 <thorn.mailbox@gmail.com>
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Jan 4, 2024
* 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>
medikoo pushed a commit to medikoo/prettier-elastic that referenced this pull request Jan 4, 2024
* 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better cache handling of multiple (but different) prettier calls
5 participants