Skip to content

Commit f4f4011

Browse files
Implicitly opting out of caching when a plugin's version can't be determined
1 parent 5d5a6ca commit f4f4011

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Known from "./known.js";
1212
import Logger from "./logger.js";
1313
import { makePrettier } from "./prettier.js";
1414
import { castArray, getExpandedFoldersPaths, getFoldersChildrenPaths, getPluginsVersions, getProjectPath, getStdin, getTargetsPaths } from "./utils.js";
15-
import { fastRelativePath, isString, isUndefined, negate, pluralize, trimFinalNewline, uniq } from "./utils.js";
15+
import { fastRelativePath, isNull, isString, isUndefined, negate, pluralize, trimFinalNewline, uniq } from "./utils.js";
1616
import type { FormatOptions, Options, PluginsOptions } from "./types.js";
1717

1818
async function run(options: Options, pluginsDefaultOptions: PluginsOptions, pluginsCustomOptions: PluginsOptions): Promise<void> {
@@ -72,6 +72,7 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
7272
const cliVersion = CLI_VERSION;
7373
const pluginsNames = options.formatOptions.plugins || [];
7474
const pluginsVersions = getPluginsVersions(pluginsNames);
75+
const pluginsVersionsMissing = pluginsVersions.filter(isNull);
7576

7677
const editorConfigNames = options.editorConfig ? [".editorconfig"].filter(Known.hasFileName) : [];
7778
const ignoreNames = options.ignore ? [".gitignore", ".prettierignore"].filter(Known.hasFileName) : [];
@@ -102,7 +103,7 @@ async function runGlobs(options: Options, pluginsDefaultOptions: PluginsOptions,
102103
const cliFormatConfig = options.formatOptions;
103104
const cacheVersion = stringify({ prettierVersion, cliVersion, pluginsNames, pluginsVersions, editorConfigs, ignoreContents, prettierConfigs, ignoreManualFilesPaths, ignoreManualFilesContents, prettierManualFilesPaths, prettierManualFilesContents, cliContextConfig, cliFormatConfig, pluginsDefaultOptions, pluginsCustomOptions }); // prettier-ignore
104105

105-
const shouldCache = options.cache && isUndefined(cliContextConfig.cursorOffset);
106+
const shouldCache = options.cache && !pluginsVersionsMissing.length && isUndefined(cliContextConfig.cursorOffset);
106107
const cache = shouldCache ? new Cache(cacheVersion, projectPath, options, stdout) : undefined;
107108
const prettier = await makePrettier(options, cache);
108109

src/utils.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,15 @@ function getPluginPath(name: string): string {
120120
return pluginPath;
121121
}
122122

123-
function getPluginVersion(name: string): string {
123+
function getPluginVersion(name: string): string | null {
124124
const pluginPath = getPluginPath(name);
125125
const parentPath = path.dirname(pluginPath);
126126
const pkg = findUp("package.json", parentPath);
127-
if (!pkg || !pkg.content.version) throw new Error(`Version not found for plugin: "${name}"`);
128-
return pkg.content.version;
127+
if (!pkg || !pkg.content.version) {
128+
return null;
129+
} else {
130+
return pkg.content.version;
131+
}
129132
}
130133

131134
function getPlugins(names: string[]): PromiseMaybe<PrettierPlugin[]> {
@@ -142,7 +145,7 @@ function getPluginsPaths(names: string[]): string[] {
142145
return pluginsPaths;
143146
}
144147

145-
function getPluginsVersions(names: string[]): string[] {
148+
function getPluginsVersions(names: string[]): (string | null)[] {
146149
const pluginsVersions = names.map(getPluginVersion);
147150
return pluginsVersions;
148151
}
@@ -256,6 +259,10 @@ function isIntegerInRange(value: unknown, min: number = -Infinity, max: number =
256259
return isInteger(value) && value >= min && value <= max && value % step === 0;
257260
}
258261

262+
function isNull(value: unknown): value is null {
263+
return value === null;
264+
}
265+
259266
function isNumber(value: unknown): value is number {
260267
return typeof value === "number";
261268
}
@@ -688,6 +695,7 @@ export {
688695
isFunction,
689696
isInteger,
690697
isIntegerInRange,
698+
isNull,
691699
isNumber,
692700
isObject,
693701
isString,

0 commit comments

Comments
 (0)