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

file-entry-cache - fixing bug on save meta.data and changed state #904

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
16 changes: 12 additions & 4 deletions packages/file-entry-cache/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default class FileEntryDefault {
}

export class FileEntryCache {
private _cache: FlatCache = new FlatCache();
private _cache: FlatCache = new FlatCache({useClone: false});
private _useCheckSum = false;
private _currentWorkingDirectory: string | undefined;
private _hashAlgorithm = 'md5';
Expand Down Expand Up @@ -248,6 +248,8 @@ export class FileEntryCache {
meta: {},
};

result.meta = this._cache.getKey<FileDescriptorMeta>(result.key) ?? {};

// Set the file path
filePath = this.getAbsolutePath(filePath, {currentWorkingDirectory: options?.currentWorkingDirectory});

Expand Down Expand Up @@ -288,20 +290,26 @@ export class FileEntryCache {
return result;
}

if (result.meta.data !== metaCache.data) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result.meta.data is always undefined. It is caused by line 262.

fstat = fs.statSync(filePath);
// Get the file size
result.meta = {
	size: fstat.size,
};

result.changed = true;
}

// Set the data from the cache
result.meta.data = metaCache.data;
if (result.meta.data === undefined) {
result.meta.data = metaCache.data;
}

// If the file is in the cache, check if the file has changed
if (metaCache?.mtime !== result.meta?.mtime || metaCache?.size !== result.meta?.size) {
result.changed = true;
this._cache.setKey(result.key, result.meta);
}

if (useCheckSumValue && metaCache?.hash !== result.meta?.hash) {
result.changed = true;
this._cache.setKey(result.key, result.meta);
}

this._cache.setKey(result.key, result.meta);

return result;
}

Expand Down
33 changes: 32 additions & 1 deletion packages/file-entry-cache/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import {
describe, test, expect, beforeAll, afterAll, beforeEach, afterEach,
} from 'vitest';
import defaultFileEntryCache, {FileEntryCache, type FileEntryCacheOptions} from '../src/index.js';
import defaultFileEntryCache, {createFromFile, FileEntryCache, type FileEntryCacheOptions} from '../src/index.js';

describe('file-entry-cache with options', () => {
test('should initialize', () => {
Expand Down Expand Up @@ -140,6 +140,7 @@ describe('getFileDescriptor()', () => {
const fileCacheName = '.cacheGFD';
beforeEach(() => {
// Generate files for testing
fs.rmSync(path.resolve(`./${fileCacheName}`), {recursive: true, force: true});
fs.mkdirSync(path.resolve(`./${fileCacheName}`));
fs.writeFileSync(path.resolve(`./${fileCacheName}/test1.txt`), 'test');
fs.writeFileSync(path.resolve(`./${fileCacheName}/test2.txt`), 'test sdfljsdlfjsdflsj');
Expand All @@ -160,6 +161,36 @@ describe('getFileDescriptor()', () => {
expect(fileDescriptor.meta.data).to.not.toBeDefined();
});

test('should save the meta data after the first call and loading data', () => {
const shared = {shared: 'shared'};
const data = {testingFooVariable: '11', name: 'test1.txt', shared};
const fileEntryCache = new FileEntryCache({useCheckSum: true});
const testFile1 = path.resolve('./.cacheGFD/test1.txt');
const fileDescriptor = fileEntryCache.getFileDescriptor(testFile1);
fileDescriptor.meta.data = data;
expect(fileDescriptor).toBeDefined();
fileEntryCache.reconcile();

// Add the meta data to the cache
const fileEntryCache2 = createFromFile(fileEntryCache.cache.cacheFilePath, true);
const fileDescriptor2 = fileEntryCache2.getFileDescriptor(testFile1);
const data2 = {testingFooVariable: '22', name: 'test1.txt', shared};
fileDescriptor2.meta.data = data2;
fileEntryCache2.reconcile();

// Load the meta data from the cache
const fileEntryCache3 = createFromFile(fileEntryCache.cache.cacheFilePath, true);
const fileDescriptor3 = fileEntryCache3.getFileDescriptor(testFile1);
expect(fileDescriptor3).toBeDefined();
expect(fileDescriptor3.meta.data).toEqual(data2);

// Verify that the data shows changed
const fileDescriptor4 = fileEntryCache3.getFileDescriptor(testFile1);
expect(fileDescriptor4).toBeDefined();
expect(fileDescriptor4.meta.data).toEqual(data2);
expect(fileDescriptor4.changed).toEqual(true);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you expect changed to be true?

The file didn't change. Yes, meta.data had gotten added.

In my case, I only care if the file changed, not the meta.data. I'm ok with being responsible for detecting changes to the meta.data if necessary.

});

test('should return a file descriptor', () => {
const fileEntryCache = new FileEntryCache();
const testFile1 = path.resolve('./.cacheGFD/test1.txt');
Expand Down
Loading