Skip to content

fix(vscode): remove json caching in various places #2587

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

Merged
merged 2 commits into from
Jun 25, 2025
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
13 changes: 8 additions & 5 deletions libs/language-server/watcher/src/lib/native-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export class NativeWatcher {
private watcher: Watcher | undefined;
private stopped = false;

constructor(private workspacePath: string, private callback: () => unknown) {
constructor(
private workspacePath: string,
private callback: () => unknown,
) {
this.initWatcher();
}

Expand All @@ -39,7 +42,7 @@ export class NativeWatcher {
const native = await importNxPackagePath<typeof import('nx/src/native')>(
this.workspacePath,
'src/native/index.js',
lspLogger
lspLogger,
);
this.watcher = new native.Watcher(this.workspacePath);

Expand All @@ -57,13 +60,13 @@ export class NativeWatcher {
path.endsWith('workspace.json') ||
path.endsWith('tsconfig.base.json') ||
NX_PLUGIN_PATTERNS_TO_WATCH.some((pattern) =>
minimatch([path], pattern, { dot: true })
minimatch([path], pattern, { dot: true }),
) ||
NativeWatcher.openDocuments.has(path)) &&
!path.startsWith('node_modules') &&
!path.startsWith(normalize('.nx/cache')) &&
!path.startsWith(normalize('.yarn/cache')) &&
!path.startsWith(normalize('.nx/workspace-data'))
!path.startsWith(normalize('.nx/workspace-data')),
)
) {
if (this.stopped) {
Expand All @@ -73,7 +76,7 @@ export class NativeWatcher {
lspLogger.log(
`native watcher detected project changes in files ${events
.map((e) => normalize(e.path))
.join(', ')}`
.join(', ')}`,
);
this.callback();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { readAndCacheJsonFile } from '@nx-console/shared-file-system';
import { readJsonFile } from '@nx-console/shared-file-system';
import { Option } from '@nx-console/shared-schema';
import { normalizeSchema } from '@nx-console/shared-schema';
import { nxWorkspace } from '@nx-console/shared-nx-workspace-info';
Expand All @@ -10,7 +10,7 @@ export async function getGeneratorOptions(
generatorName: string,
generatorPath: string,
): Promise<Option[]> {
const generatorSchema = await readAndCacheJsonFile(
const generatorSchema = await readJsonFile(
generatorPath,
undefined,
lspLogger,
Expand Down
6 changes: 2 additions & 4 deletions libs/shared/file-system/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
export { fileExists } from './lib/file-exists';
export { directoryExists } from './lib/directory-exists';
export { cacheJson } from './lib/cache-json';
export { readAndCacheJsonFile } from './lib/cache-json';
export { clearJsonCache } from './lib/cache-json';
export { readAndParseJson } from './lib/cache-json';
export { readJsonFile } from './lib/read-json-file';
export { readAndParseJson } from './lib/read-json-file';
export { listFiles } from './lib/list-files';
export { readDirectory } from './lib/read-directory';
export { readFile } from './lib/read-file';
Expand Down
98 changes: 0 additions & 98 deletions libs/shared/file-system/src/lib/cache-json.ts

This file was deleted.

6 changes: 5 additions & 1 deletion libs/shared/file-system/src/lib/read-file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { crossFs } from './cache-json';
import { PosixFS } from '@yarnpkg/fslib';
import { ZipOpenFS, getLibzipSync as libzip } from '@yarnpkg/libzip';

const zipOpenFs = new ZipOpenFS({ libzip });
export const crossFs = new PosixFS(zipOpenFs);

export async function readFile(filePath: string): Promise<string> {
try {
Expand Down
53 changes: 53 additions & 0 deletions libs/shared/file-system/src/lib/read-json-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import * as path from 'path';
import { Logger } from '@nx-console/shared-utils';
import { readFileSync, existsSync } from 'fs';
import { parse as parseJson, ParseError } from 'jsonc-parser';

export async function readAndParseJson(filePath: string) {
const content = readFileSync(filePath, 'utf8');
try {
return JSON.parse(content);
} catch {
const errors: ParseError[] = [];
const result = parseJson(content, errors);
return result;
}
}

export async function readJsonFile(
filePath: string | undefined,
basedir = '',
logger?: Logger,
): Promise<{ path: string; json: any }> {
if (!filePath) {
return {
path: '',
json: {},
};
}

let fullFilePath = basedir ? path.join(basedir, filePath) : filePath;
if (fullFilePath.startsWith('file:\\')) {
fullFilePath = fullFilePath.replace('file:\\', '');
}
if (fullFilePath.startsWith('file://')) {
fullFilePath = fullFilePath.replace('file://', '');
}

try {
if (existsSync(fullFilePath)) {
const json = await readAndParseJson(fullFilePath);
return {
path: fullFilePath,
json,
};
}
} catch (e) {
logger?.log(`${fullFilePath} does not exist`);
}

return {
path: fullFilePath,
json: {},
};
}
9 changes: 3 additions & 6 deletions libs/shared/npm/src/lib/check-is-nx-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { join } from 'path';
import {
fileExists,
readAndCacheJsonFile,
} from '@nx-console/shared-file-system';
import { fileExists, readJsonFile } from '@nx-console/shared-file-system';
import { coerce } from 'semver';
import { workspaceDependencyPath } from './workspace-dependencies';

Expand All @@ -18,15 +15,15 @@ export async function checkIsNxWorkspace(
return false;
}

const lernaPackageJson = await readAndCacheJsonFile(
const lernaPackageJson = await readJsonFile(
join(lernaPath, 'package.json'),
);
const lernaVersion = coerce(lernaPackageJson.json.version);

if (lernaVersion?.major ?? 0 >= 6) {
isNxWorkspace = true;
} else {
const lerna = await readAndCacheJsonFile('lerna.json', workspacePath);
const lerna = await readJsonFile('lerna.json', workspacePath);
isNxWorkspace = lerna.json.useNx ?? false;
}
}
Expand Down
36 changes: 16 additions & 20 deletions libs/shared/npm/src/lib/package-details.spec.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { readAndCacheJsonFile } from '@nx-console/shared-file-system';
import { readJsonFile } from '@nx-console/shared-file-system';
import { packageDetails } from './package-details';
import { normalize } from 'node:path';

jest.mock('@nx-console/shared-file-system');

describe('packageDetails', () => {
it('should return package path, package name and JSON contents', async () => {
const readAndCacheJsonFileMock = jest
.mocked(readAndCacheJsonFile)
.mockResolvedValueOnce({
path: 'path',
json: {
name: 'utils',
version: '1.0.0',
main: 'dist/index.js',
types: 'dist/index.d.ts',
},
});
const readJsonFileMock = jest.mocked(readJsonFile).mockResolvedValueOnce({
path: 'path',
json: {
name: 'utils',
version: '1.0.0',
main: 'dist/index.js',
types: 'dist/index.d.ts',
},
});

expect(await packageDetails('libs/utils')).toEqual({
packagePath: 'libs/utils',
Expand All @@ -28,24 +26,22 @@ describe('packageDetails', () => {
types: 'dist/index.d.ts',
},
});
expect(readAndCacheJsonFileMock).toBeCalledWith(
expect(readJsonFileMock).toBeCalledWith(
normalize('libs/utils/package.json'),
);
});

it('should return undefined package name if JSON is empty', async () => {
const readAndCacheJsonFileMock = jest
.mocked(readAndCacheJsonFile)
.mockResolvedValueOnce({
path: '',
json: {},
});
const readJsonFileMock = jest.mocked(readJsonFile).mockResolvedValueOnce({
path: '',
json: {},
});

expect(await packageDetails('')).toEqual({
packagePath: '',
packageName: undefined,
packageJson: {},
});
expect(readAndCacheJsonFileMock).toBeCalledWith('package.json');
expect(readJsonFileMock).toBeCalledWith('package.json');
});
});
6 changes: 2 additions & 4 deletions libs/shared/npm/src/lib/package-details.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { readAndCacheJsonFile } from '@nx-console/shared-file-system';
import { readJsonFile } from '@nx-console/shared-file-system';
import { join } from 'path';

export async function packageDetails(packagePath: string) {
const { json } = await readAndCacheJsonFile(
join(packagePath, 'package.json')
);
const { json } = await readJsonFile(join(packagePath, 'package.json'));
return {
packagePath,
packageName: json.name,
Expand Down
3 changes: 0 additions & 3 deletions libs/shared/nx-workspace-info/src/lib/get-executors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,19 @@ import { Logger } from '@nx-console/shared-utils';

export type GetExecutorsOptions = {
includeHidden: boolean;
clearPackageJsonCache: boolean;
};

export async function getExecutors(
workspacePath: string,
options: GetExecutorsOptions = {
includeHidden: false,
clearPackageJsonCache: false,
},
logger?: Logger,
): Promise<ExecutorCollectionInfo[]> {
return (
await readCollections(
workspacePath,
{
clearPackageJsonCache: options.clearPackageJsonCache,
includeHidden: options.includeHidden,
},
logger,
Expand Down
Loading
Loading