Skip to content

Commit 6038c5c

Browse files
mdonnalleymshanemc
andauthored
fix: ignore cache when reading user pjson (#1124)
* fix: ignore cache when reading user pjson * refactor: useCache = true default (#1125) --------- Co-authored-by: Shane McLaughlin <shane.mclaughlin@salesforce.com>
1 parent 5213551 commit 6038c5c

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

src/config/plugin-loader.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ export default class PluginLoader {
208208
try {
209209
const userPJSONPath = join(opts.dataDir, 'package.json')
210210
debug('reading user plugins pjson %s', userPJSONPath)
211-
const pjson = await readJson<PJSON>(userPJSONPath)
211+
// ignore cache because the file might have changed within the same process (e.g. during a JIT plugin install)
212+
const pjson = await readJson<PJSON>(userPJSONPath, false)
212213
if (!pjson.oclif) pjson.oclif = {schema: 1}
213214
if (!pjson.oclif.plugins) pjson.oclif.plugins = []
214215
await this.loadPlugins(

src/util/fs.ts

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Stats, existsSync as fsExistsSync, readFileSync} from 'node:fs'
1+
import {Stats, existsSync as fsExistsSync} from 'node:fs'
22
import {readFile, stat} from 'node:fs/promises'
33

44
import {isProd} from './util'
@@ -57,8 +57,17 @@ class ProdOnlyCache extends Map<string, string> {
5757

5858
const cache = new ProdOnlyCache()
5959

60-
export async function readJson<T = unknown>(path: string): Promise<T> {
61-
if (cache.has(path)) {
60+
/**
61+
* Read a file from disk and cache its contents if in production environment.
62+
*
63+
* Will throw an error if the file does not exist.
64+
*
65+
* @param path file path of JSON file
66+
* @param useCache if false, ignore cache and read file from disk
67+
* @returns <T>
68+
*/
69+
export async function readJson<T = unknown>(path: string, useCache = true): Promise<T> {
70+
if (useCache && cache.has(path)) {
6271
return JSON.parse(cache.get(path)!) as T
6372
}
6473

@@ -67,25 +76,18 @@ export async function readJson<T = unknown>(path: string): Promise<T> {
6776
return JSON.parse(contents) as T
6877
}
6978

70-
export function readJsonSync(path: string, parse: false): string
71-
export function readJsonSync<T = unknown>(path: string, parse?: true): T
72-
export function readJsonSync<T = unknown>(path: string, parse = true): T | string {
73-
if (cache.has(path)) {
74-
return JSON.parse(cache.get(path)!) as T
75-
}
76-
77-
const contents = readFileSync(path, 'utf8')
78-
cache.set(path, contents)
79-
return parse ? (JSON.parse(contents) as T) : contents
80-
}
81-
82-
export async function safeReadJson<T>(path: string): Promise<T | undefined> {
83-
if (cache.has(path)) {
84-
return JSON.parse(cache.get(path)!) as T
85-
}
86-
79+
/**
80+
* Safely read a file from disk and cache its contents if in production environment.
81+
*
82+
* Will return undefined if the file does not exist.
83+
*
84+
* @param path file path of JSON file
85+
* @param useCache if false, ignore cache and read file from disk
86+
* @returns <T> or undefined
87+
*/
88+
export async function safeReadJson<T>(path: string, useCache = true): Promise<T | undefined> {
8789
try {
88-
return await readJson<T>(path)
90+
return await readJson<T>(path, useCache)
8991
} catch {}
9092
}
9193

0 commit comments

Comments
 (0)