Skip to content

Commit

Permalink
Fix --cache set only if with --write or no-different (#13016)
Browse files Browse the repository at this point in the history
* Fixed typo

* Return early if file does not exist

* Added tests for cache

* Added `mockWriteFileErrors` option to run helper

* Added tests for write error

* Remove files with differences from cache

* Add `getCliArguments` utility function

Refactor

* Fix lint

* Revert "Fix lint"

This reverts commit 18be64b.

* Revert "Add `getCliArguments` utility function"

This reverts commit d653fe1.

* Refactor with `cliArguments` variable

Co-authored-by: Sosuke Suzuki <sosuke.suzuki@dr-ubie.com>
  • Loading branch information
Milly and sosukesuzuki committed Oct 24, 2022
1 parent f38111f commit ca246af
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 70 deletions.
22 changes: 13 additions & 9 deletions src/cli/format-results-cache.js
Expand Up @@ -40,7 +40,7 @@ function getMetadataFromFileDescriptor(fileDescriptor) {

class FormatResultsCache {
/**
* @param {string} cacheFileLocation The path of cache file location. (default: `node_modules/.cache/prettier/prettier-cache`)
* @param {string} cacheFileLocation The path of cache file location. (default: `node_modules/.cache/prettier/.prettier-cache`)
* @param {string} cacheStrategy
*/
constructor(cacheFileLocation, cacheStrategy) {
Expand All @@ -60,20 +60,17 @@ class FormatResultsCache {
*/
existsAvailableFormatResultsCache(filePath, options) {
const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath);
const hashOfOptions = getHashOfOptions(options);
const meta = getMetadataFromFileDescriptor(fileDescriptor);
const changed =
fileDescriptor.changed || meta.hashOfOptions !== hashOfOptions;

if (fileDescriptor.notFound) {
return false;
}

if (changed) {
return false;
}
const hashOfOptions = getHashOfOptions(options);
const meta = getMetadataFromFileDescriptor(fileDescriptor);
const changed =
fileDescriptor.changed || meta.hashOfOptions !== hashOfOptions;

return true;
return !changed;
}

/**
Expand All @@ -88,6 +85,13 @@ class FormatResultsCache {
}
}

/**
* @param {string} filePath
*/
removeFormatResultsCache(filePath) {
this.fileEntryCache.removeEntry(filePath);
}

reconcile() {
this.fileEntryCache.reconcile();
}
Expand Down
14 changes: 10 additions & 4 deletions src/cli/format.js
Expand Up @@ -407,9 +407,8 @@ async function formatFiles(context) {
continue;
}

formatResultsCache?.setFormatResultsCache(filename, options);

const isDifferent = output !== input;
let shouldSetCache = !isDifferent;

if (printedFilename) {
// Remove previously printed filename to log it with duration.
Expand All @@ -433,14 +432,15 @@ async function formatFiles(context) {

try {
await fs.writeFile(filename, output, "utf8");

// Set cache if format succeeds
shouldSetCache = true;
} catch (error) {
/* istanbul ignore next */
context.logger.error(
`Unable to write file: ${filename}\n${error.message}`
);

// Don't exit the process if one file failed
/* istanbul ignore next */
process.exitCode = 2;
}
} else if (!context.argv.check && !context.argv.listDifferent) {
Expand All @@ -462,6 +462,12 @@ async function formatFiles(context) {
writeOutput(context, result, options);
}

if (shouldSetCache) {
formatResultsCache?.setFormatResultsCache(filename, options);
} else {
formatResultsCache?.removeFormatResultsCache(filename);
}

if (isDifferent) {
if (context.argv.check) {
context.logger.warn(filename);
Expand Down

0 comments on commit ca246af

Please sign in to comment.