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: Upgrade file-entry-cache to 10.x #6579

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ typings/
.tsbuildinfo

# cspell cache
.cspellcache
.cspellcache*

# Node Performance

Expand Down
2 changes: 1 addition & 1 deletion packages/cspell/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
"cspell-io": "workspace:*",
"cspell-lib": "workspace:*",
"fast-json-stable-stringify": "^2.1.0",
"file-entry-cache": "^9.1.0",
"file-entry-cache": "^10.0.3",
"get-stdin": "^9.0.0",
"semver": "^7.6.3",
"tinyglobby": "^0.2.10"
Expand Down
9 changes: 7 additions & 2 deletions packages/cspell/src/app/util/cache/file-entry-cache.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import fileEntryCache from 'file-entry-cache';

export type { FileDescriptor, FileEntryCache } from 'file-entry-cache';

export function createFromFile(pathToCache: string, useChecksum?: boolean): FileEntryCache {
return fileEntryCache.createFromFile(pathToCache, useChecksum);
export interface FileEntryCacheOptions {
useCheckSum?: boolean;
currentWorkingDirectory?: string;
}

export function createFromFile(pathToCache: string, options: FileEntryCacheOptions): FileEntryCache {
return fileEntryCache.createFromFile(pathToCache, options.useCheckSum, options.currentWorkingDirectory);
}
66 changes: 13 additions & 53 deletions packages/cspell/src/app/util/cache/fileEntryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,32 @@
export type { FileDescriptor } from './file-entry-cache.mjs';
import { mkdirSync } from 'node:fs';
import * as path from 'node:path';
import { isMainThread } from 'node:worker_threads';

import type { FileEntryCache as FecFileEntryCache } from './file-entry-cache.mjs';
import * as fec from './file-entry-cache.mjs';
import { createFromFile as fecCreateFromFile } from './file-entry-cache.mjs';

export type FileEntryCache = FecFileEntryCache;
export type FileEntryCache = Pick<FecFileEntryCache, 'reconcile' | 'destroy' | 'getFileDescriptor'>;

export function createFromFile(pathToCache: string, useCheckSum: boolean, useRelative: boolean): FileEntryCache {
const absPathToCache = path.resolve(pathToCache);
const relDir = path.dirname(absPathToCache);
mkdirSync(relDir, { recursive: true });
const create = wrap(() => fec.createFromFile(absPathToCache, useCheckSum));
const feCache = create();
const feCache = fecCreateFromFile(absPathToCache, { useCheckSum, currentWorkingDirectory: relDir });
const cacheWrapper: FileEntryCache = {
get cache() {
return feCache.cache;
},
getHash(buffer: Buffer): string {
return feCache.getHash(buffer);
},
hasFileChanged: wrap((cwd, file: string) => {
// console.log(file);
return feCache.hasFileChanged(resolveFile(cwd, file));
}),
analyzeFiles: wrap((cwd, files?: string[]) => {
return feCache.analyzeFiles(resolveFiles(cwd, files));
}),

getFileDescriptor: wrap((cwd, file: string) => {
return feCache.getFileDescriptor(resolveFile(cwd, file));
}),
getUpdatedFiles: wrap((cwd, files?: string[]) => {
return feCache.getUpdatedFiles(resolveFiles(cwd, files));
}),
normalizeEntries: wrap((cwd, files?: string[]) => {
return feCache.normalizeEntries(resolveFiles(cwd, files));
}),
removeEntry: wrap((cwd, file: string) => {
// console.log(file);
return feCache.removeEntry(resolveFile(cwd, file));
}),
deleteCacheFile(): void {
feCache.deleteCacheFile();
getFileDescriptor: (file: string) => {
const r = resolveFile(process.cwd(), file);
const d = feCache.getFileDescriptor(r, { useCheckSum, currentWorkingDirectory: relDir });
// if (d.changed) {
// console.log(`File changed: ${r} %o`, d);
// }
return d;
},
destroy(): void {
feCache.destroy();
},
reconcile: wrap((_cwd, noPrune?: boolean) => {
feCache.reconcile(noPrune);
}),
reconcile: () => {
feCache.reconcile();
},
};

return cacheWrapper;
Expand All @@ -64,22 +40,6 @@ export function createFromFile(pathToCache: string, useCheckSum: boolean, useRel
const r = path.relative(relDir, path.resolve(cwd, file));
return normalizePath(r);
}

function resolveFiles(cwd: string, files: string[] | undefined): string[] | undefined {
return files?.map((file) => resolveFile(cwd, file));
}

function wrap<P extends unknown[], T>(fn: (cwd: string, ...params: P) => T): (...params: P) => T {
return (...params: P) => {
const cwd = process.cwd();
try {
isMainThread && process.chdir(relDir);
return fn(cwd, ...params);
} finally {
isMainThread && process.chdir(cwd);
}
};
}
}

export function normalizePath(filePath: string): string {
Expand Down
Loading
Loading