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: ignore cache when reading user pjson #1124

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/config/plugin-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,8 @@ export default class PluginLoader {
try {
const userPJSONPath = join(opts.dataDir, 'package.json')
debug('reading user plugins pjson %s', userPJSONPath)
const pjson = await readJson<PJSON>(userPJSONPath)
// ignore cache because the file might have changed within the same process (e.g. during a JIT plugin install)
const pjson = await readJson<PJSON>(userPJSONPath, false)
if (!pjson.oclif) pjson.oclif = {schema: 1}
if (!pjson.oclif.plugins) pjson.oclif.plugins = []
await this.loadPlugins(
Expand Down
44 changes: 23 additions & 21 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Stats, existsSync as fsExistsSync, readFileSync} from 'node:fs'
import {Stats, existsSync as fsExistsSync} from 'node:fs'
import {readFile, stat} from 'node:fs/promises'

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

const cache = new ProdOnlyCache()

export async function readJson<T = unknown>(path: string): Promise<T> {
if (cache.has(path)) {
/**
* Read a file from disk and cache its contents if in production environment.
*
* Will throw an error if the file does not exist.
*
* @param path file path of JSON file
* @param useCache if false, ignore cache and read file from disk
* @returns <T>
*/
export async function readJson<T = unknown>(path: string, useCache = true): Promise<T> {
if (useCache && cache.has(path)) {
return JSON.parse(cache.get(path)!) as T
}

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

export function readJsonSync(path: string, parse: false): string
export function readJsonSync<T = unknown>(path: string, parse?: true): T
export function readJsonSync<T = unknown>(path: string, parse = true): T | string {
if (cache.has(path)) {
return JSON.parse(cache.get(path)!) as T
}

const contents = readFileSync(path, 'utf8')
cache.set(path, contents)
return parse ? (JSON.parse(contents) as T) : contents
}

export async function safeReadJson<T>(path: string): Promise<T | undefined> {
if (cache.has(path)) {
return JSON.parse(cache.get(path)!) as T
}

/**
* Safely read a file from disk and cache its contents if in production environment.
*
* Will return undefined if the file does not exist.
*
* @param path file path of JSON file
* @param useCache if false, ignore cache and read file from disk
* @returns <T> or undefined
*/
export async function safeReadJson<T>(path: string, useCache = true): Promise<T | undefined> {
try {
return await readJson<T>(path)
return await readJson<T>(path, useCache)
} catch {}
}

Expand Down
Loading