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

Fix --cache set only if with --write or no-different #13016

Merged
merged 11 commits into from Oct 24, 2022
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) {
Milly marked this conversation as resolved.
Show resolved Hide resolved
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);
Milly marked this conversation as resolved.
Show resolved Hide resolved
}

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