Skip to content

Commit 2ebf2ac

Browse files
authored
file-entry-cache - feat: adding in key as absolute path (#1394)
* file-entry-cache - feat: adding in keyAsAbsolutePath * updating tests * moving to useAbsolutePathAsKey * updating tests
1 parent ca20f3f commit 2ebf2ac

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

packages/file-entry-cache/src/index.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export type FileEntryCacheOptions = {
3636
cwd?: string;
3737
/** Restrict file access to within cwd boundaries (default: true) */
3838
strictPaths?: boolean;
39+
/** Whether to use absolute path as cache key (default: false) */
40+
useAbsolutePathAsKey?: boolean;
3941
/** Logger instance for logging (default: undefined) */
4042
logger?: ILogger;
4143
/** Options for the underlying flat cache */
@@ -147,6 +149,7 @@ export class FileEntryCache {
147149
private _cwd: string = process.cwd();
148150
private _strictPaths = false;
149151
private _logger?: ILogger;
152+
private _useAbsolutePathAsKey = false;
150153

151154
/**
152155
* Create a new FileEntryCache instance
@@ -173,6 +176,10 @@ export class FileEntryCache {
173176
this._strictPaths = options.strictPaths;
174177
}
175178

179+
if (options?.useAbsolutePathAsKey !== undefined) {
180+
this._useAbsolutePathAsKey = options.useAbsolutePathAsKey;
181+
}
182+
176183
if (options?.logger) {
177184
this._logger = options.logger;
178185
}
@@ -274,6 +281,22 @@ export class FileEntryCache {
274281
this._strictPaths = value;
275282
}
276283

284+
/**
285+
* Get whether to use absolute path as cache key
286+
* @returns {boolean} Whether cache keys use absolute paths (default: false)
287+
*/
288+
public get useAbsolutePathAsKey(): boolean {
289+
return this._useAbsolutePathAsKey;
290+
}
291+
292+
/**
293+
* Set whether to use absolute path as cache key
294+
* @param {boolean} value - The value to set
295+
*/
296+
public set useAbsolutePathAsKey(value: boolean) {
297+
this._useAbsolutePathAsKey = value;
298+
}
299+
277300
/**
278301
* Given a buffer, calculate md5 hash of its content.
279302
* @method getHash
@@ -291,7 +314,12 @@ export class FileEntryCache {
291314
* @return {String}
292315
*/
293316
public createFileKey(filePath: string): string {
294-
const result = filePath;
317+
let result = filePath;
318+
319+
if (this._useAbsolutePathAsKey && this.isRelativePath(filePath)) {
320+
result = this.getAbsolutePathWithCwd(filePath, this._cwd);
321+
}
322+
295323
return result;
296324
}
297325

packages/file-entry-cache/test/cwd.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ describe("cwd functionality", () => {
6060
testBaseDir,
6161
);
6262

63+
cache.useAbsolutePathAsKey = false;
64+
6365
// Get file descriptor using relative path
6466
const descriptor = cache.getFileDescriptor("src/index.js");
6567

packages/file-entry-cache/test/file-rename.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ describe("file-rename with cwd", () => {
4848
originalPath, // cwd set to the unique folder
4949
);
5050

51+
cache1.useAbsolutePathAsKey = false;
52+
5153
// Step 3: Get file descriptor using relative path
5254
const descriptor1 = cache1.getFileDescriptor(testFileName);
5355

@@ -140,6 +142,8 @@ describe("file-rename with cwd", () => {
140142
originalPath,
141143
);
142144

145+
cache1.useAbsolutePathAsKey = false;
146+
143147
// Process all files
144148
files.forEach((file) => {
145149
const desc = cache1.getFileDescriptor(file.name);
@@ -163,6 +167,8 @@ describe("file-rename with cwd", () => {
163167
renamedPath,
164168
);
165169

170+
cache2.useAbsolutePathAsKey = false;
171+
166172
// Verify all files are still cached and unchanged
167173
files.forEach((file) => {
168174
const desc = cache2.getFileDescriptor(file.name);

packages/file-entry-cache/test/index.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ describe("file-entry-cache with options", () => {
127127
fs.writeFileSync(testFile, "initial content");
128128

129129
const fileEntryCache = new FileEntryCache({
130+
// @ts-expect-error as test
130131
logger,
131132
useCheckSum: true,
132133
});
@@ -223,6 +224,7 @@ describe("file-entry-cache with options", () => {
223224
fs.writeFileSync(testFile, "initial content");
224225

225226
const fileEntryCache = new FileEntryCache({
227+
// @ts-expect-error
226228
logger,
227229
useCheckSum: false, // Disable checksum to use mtime
228230
});
@@ -720,6 +722,7 @@ describe("normalizeEntries()", () => {
720722
test("should return all entries", () => {
721723
const fileEntryCache = new FileEntryCache({
722724
useCheckSum: true,
725+
useAbsolutePathAsKey: false,
723726
});
724727
fileEntryCache.getFileDescriptor(`./${fileCacheName}/test1.txt`);
725728
fileEntryCache.getFileDescriptor(`./${fileCacheName}/test2.txt`);
@@ -906,7 +909,9 @@ describe("getUpdatedFiles()", () => {
906909
});
907910

908911
test("should return empty array on get updated files", () => {
909-
const fileEntryCache = new FileEntryCache();
912+
const fileEntryCache = new FileEntryCache({
913+
useAbsolutePathAsKey: false,
914+
});
910915
const files = [
911916
`./${fileCacheName}/test1.txt`,
912917
`./${fileCacheName}/test2.txt`,
@@ -920,7 +925,9 @@ describe("getUpdatedFiles()", () => {
920925
});
921926

922927
test("should return updated files if one is updated", () => {
923-
const fileEntryCache = new FileEntryCache();
928+
const fileEntryCache = new FileEntryCache({
929+
useAbsolutePathAsKey: false,
930+
});
924931
const files = [
925932
`./${fileCacheName}/test1.txt`,
926933
`./${fileCacheName}/test2.txt`,
@@ -1041,7 +1048,9 @@ describe("getFileDescriptorsByPath()", () => {
10411048
});
10421049

10431050
test("should return an array of file descriptors", () => {
1044-
const fileEntryCache = new FileEntryCache();
1051+
const fileEntryCache = new FileEntryCache({
1052+
useAbsolutePathAsKey: false,
1053+
});
10451054
fileEntryCache.getFileDescriptor(`./${fileCacheName}/test1.txt`);
10461055
fileEntryCache.getFileDescriptor(`./${fileCacheName}/test2.txt`);
10471056
const fileDescriptors = fileEntryCache.getFileDescriptorsByPath(

packages/file-entry-cache/test/relative-eslint.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import fs from "node:fs";
22
import path from "node:path";
33
import { describe, expect, test } from "vitest";
4-
import fileEntryCache from "../src/index.js";
4+
import fileEntryCache, {
5+
FileEntryCache,
6+
type FileEntryCacheOptions,
7+
} from "../src/index.js";
58

69
describe("eslint tests scenarios", () => {
710
test("relative pathing works on files and cache", () => {
@@ -18,6 +21,10 @@ describe("eslint tests scenarios", () => {
1821
path.resolve("./src"),
1922
);
2023

24+
if (cache.useAbsolutePathAsKey) {
25+
cache.useAbsolutePathAsKey = false;
26+
}
27+
2128
const indexDescriptor1 = cache.getFileDescriptor(file);
2229

2330
expect(indexDescriptor1.changed).toBe(true);
@@ -72,4 +79,36 @@ describe("eslint tests scenarios", () => {
7279
force: true,
7380
});
7481
});
82+
test("Need to use the cache directory as the cwd and use absolute path key", () => {
83+
const cacheDirectory = "./.cache";
84+
const cacheId = ".eslintcache-2020111";
85+
const file = "../src/index.ts";
86+
const useCheckSum = true;
87+
const useAbsolutePathAsKey = true;
88+
const cwd = cacheDirectory;
89+
90+
const options: FileEntryCacheOptions = {
91+
useCheckSum,
92+
useAbsolutePathAsKey,
93+
cwd,
94+
cache: {
95+
cacheId,
96+
cacheDir: cacheDirectory,
97+
},
98+
};
99+
100+
const cache = new FileEntryCache(options);
101+
102+
const fileDescriptor = cache.getFileDescriptor(file);
103+
104+
expect(fileDescriptor.changed).toBe(true);
105+
expect(fileDescriptor.meta.hash).toBeDefined();
106+
107+
cache.reconcile();
108+
109+
const fileDescriptor2 = cache.getFileDescriptor(file);
110+
111+
expect(fileDescriptor2.changed).toBe(false);
112+
expect(fileDescriptor2.meta.hash).toBeDefined();
113+
});
75114
});

0 commit comments

Comments
 (0)