Skip to content

Commit 703ffcc

Browse files
committed
Fix caching issues for VSCode hosts
1 parent 7a9c3e0 commit 703ffcc

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/git.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ describe("getDiffFileList", () => {
106106
it("should get the list of staged files", () => {
107107
jest.mock("child_process").resetAllMocks();
108108
mockedChildProcess.execSync.mockReturnValueOnce(Buffer.from(diffFileList));
109-
const staged = false;
110109
expect(mockedChildProcess.execSync).toHaveBeenCalledTimes(0);
111-
const fileListA = getDiffFileList(staged);
112-
const fileListB = getDiffFileList(staged);
110+
const fileListA = getDiffFileList();
111+
const fileListB = getDiffFileList();
113112

114113
expect(mockedChildProcess.execSync).toHaveBeenCalledTimes(1);
115114
expect(fileListA).toEqual(

src/git.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import * as child_process from "child_process";
22
import * as path from "path";
33
import { Range } from "./Range";
44

5+
const RUNNING_INSIDE_VSCODE = process.env.VSCODE_CLI !== undefined;
6+
57
const sanitizeFilePath = (filePath: string) =>
68
JSON.stringify(path.resolve(filePath));
79

@@ -17,10 +19,9 @@ const getCachedDiff = (filePath: string, staged: boolean) =>
1719

1820
const getDiffForFile = (filePath: string, staged = false): string => {
1921
let diff = getCachedDiff(filePath, staged);
20-
if (diff === undefined) {
22+
if (RUNNING_INSIDE_VSCODE || diff === undefined) {
2123
const command = [
2224
"git",
23-
"--no-pager",
2425
"diff",
2526
"--diff-filter=ACM",
2627
"--relative",
@@ -42,16 +43,15 @@ const getDiffForFile = (filePath: string, staged = false): string => {
4243
};
4344

4445
let diffFileListCache: string[] | undefined;
45-
const getDiffFileList = (staged = false): string[] => {
46-
if (diffFileListCache === undefined) {
46+
const getDiffFileList = (): string[] => {
47+
if (RUNNING_INSIDE_VSCODE || diffFileListCache === undefined) {
4748
const command = [
4849
"git",
49-
"--no-pager",
5050
"diff",
5151
"--diff-filter=ACM",
5252
"--name-only",
5353
"--relative",
54-
staged && "--staged",
54+
"--staged",
5555
JSON.stringify(process.env.ESLINT_PLUGIN_DIFF_COMMIT ?? "HEAD"),
5656
]
5757
.filter(Boolean)
@@ -69,7 +69,8 @@ const getDiffFileList = (staged = false): string[] => {
6969

7070
let gitFileListCache: string[] | undefined;
7171
const getGitFileList = (): string[] => {
72-
if (gitFileListCache === undefined) {
72+
console.log("getGitFileList");
73+
if (RUNNING_INSIDE_VSCODE || gitFileListCache === undefined) {
7374
const command = ["git", "ls-files"].filter(Boolean).join(" ");
7475

7576
gitFileListCache = child_process
@@ -86,7 +87,6 @@ const getGitFileList = (): string[] => {
8687
const hasCleanIndex = (filePath: string): boolean => {
8788
const command = [
8889
"git",
89-
"--no-pager",
9090
"diff",
9191
"--quiet",
9292
"--relative",
@@ -109,7 +109,7 @@ let untrackedFileListCache: string[] | undefined;
109109
const getUntrackedFileList = (staged = false): string[] => {
110110
if (staged) {
111111
untrackedFileListCache = [];
112-
} else if (untrackedFileListCache === undefined) {
112+
} else if (RUNNING_INSIDE_VSCODE || untrackedFileListCache === undefined) {
113113
const command = ["git", "ls-files", "--exclude-standard", "--others"]
114114
.filter(Boolean)
115115
.join(" ");

src/processors.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ const getPreProcessor =
2121
(staged = false) =>
2222
(text: string, filename: string) => {
2323
const shouldBeProcessed =
24-
getDiffFileList(staged).includes(filename) ||
25-
getUntrackedFileList(staged).includes(filename);
26-
24+
process.env.VSCODE_CLI !== undefined ||
25+
!staged ||
26+
getDiffFileList().includes(filename);
2727
return shouldBeProcessed ? [text] : [];
2828
};
2929

30-
const isLineWithinRange = (line: number) => (range: Range) =>
31-
range.isWithinRange(line);
30+
const isLineWithinRange = (line: number) => (range: Range) => {
31+
return range.isWithinRange(line);
32+
};
3233

3334
const getPostProcessor =
3435
(staged = false) =>

0 commit comments

Comments
 (0)