Repository: prettier/prettier
File: src/cli/format-results-cache.js (JavaScript)

Code before:
 * @param {FileDescriptor} fileDescriptor
 * @returns {FileDescriptorMeta & {data?: {hashOfOptions?: string }}}}
 */
function getMetadataFromFileDescriptor(fileDescriptor) {
  return fileDescriptor.meta;
}

class FormatResultsCache {
  #fileEntryCache;

  /**
   * @param {string} cacheFileLocation The path of cache file location. (default: `node_modules/.cache/prettier/.prettier-cache`)
   * @param {string} cacheStrategy
   */
  constructor(cacheFileLocation, cacheStrategy) {
    const useChecksum = cacheStrategy === "content";

    try {
      this.#fileEntryCache = fileEntryCache.createFromFile(
        /* filePath */ cacheFileLocation,
        useChecksum,
      );
    } catch {
      // https://github.com/prettier/prettier/issues/17092
      // Prettier 3.5 uses a different cache format than previous versions.
      // If the cache file is not in the expected format, delete it and retry.
      if (fs.existsSync(cacheFileLocation)) {
        fs.unlinkSync(cacheFileLocation);
        // retry
        this.#fileEntryCache = fileEntryCache.createFromFile(
          /* filePath */ cacheFileLocation,
          useChecksum,
        );
      }
    }
  }

  /**
   * @param {string} filePath
   * @param {any} options
   */
  existsAvailableFormatResultsCache(filePath, options) {
    const fileDescriptor = this.#fileEntryCache.getFileDescriptor(filePath);
    if (fileDescriptor.notFound || fileDescriptor.changed) {
      return false;
    }

    const hashOfOptions =
      getMetadataFromFileDescriptor(fileDescriptor).data?.hashOfOptions;
    return hashOfOptions && hashOfOptions === getHashOfOptions(options);
  }

Review comment (bug):
```suggestion
      // If `createFromFile()` fails, it's probably because the format of cache file changed, it happened when we release v3.5.0
```

Code after:
 * @param {FileDescriptor} fileDescriptor
 * @returns {FileDescriptorMeta & {data?: {hashOfOptions?: string }}}}
 */
function getMetadataFromFileDescriptor(fileDescriptor) {
  return fileDescriptor.meta;
}

class FormatResultsCache {
  #fileEntryCache;

  /**
   * @param {string} cacheFileLocation The path of cache file location. (default: `node_modules/.cache/prettier/.prettier-cache`)
   * @param {string} cacheStrategy
   */
  constructor(cacheFileLocation, cacheStrategy) {
    const useChecksum = cacheStrategy === "content";

    try {
      this.#fileEntryCache = fileEntryCache.createFromFile(
        /* filePath */ cacheFileLocation,
        useChecksum,
      );
    } catch {
      // https://github.com/prettier/prettier/issues/17092
      // If `createFromFile()` fails, it's probably because the format
      // of cache file changed,it happened when we release v3.5.0
      if (fs.existsSync(cacheFileLocation)) {
        fs.unlinkSync(cacheFileLocation);
        // retry
        this.#fileEntryCache = fileEntryCache.createFromFile(
          /* filePath */ cacheFileLocation,
          useChecksum,
        );
      }
    }
  }

  /**
   * @param {string} filePath
   * @param {any} options
   */
  existsAvailableFormatResultsCache(filePath, options) {
    const fileDescriptor = this.#fileEntryCache.getFileDescriptor(filePath);
    if (fileDescriptor.notFound || fileDescriptor.changed) {
      return false;
    }

    const hashOfOptions =
      getMetadataFromFileDescriptor(fileDescriptor).data?.hashOfOptions;
    return hashOfOptions && hashOfOptions === getHashOfOptions(options);
  }