Skip to content

Commit e4b9165

Browse files
authored
Ensure that file exists before trying to read mtime (#3299)
1 parent e437757 commit e4b9165

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

extension/src/fileSystem/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ describe('getModifiedTime', () => {
172172
expect(typeof epoch).toBe('number')
173173
expect(epoch).toBeGreaterThan(1640995200000)
174174
})
175+
176+
it('should return -1 for a file that does not exist on the system', () => {
177+
const epoch = getModifiedTime('not a path')
178+
179+
expect(epoch).toStrictEqual(-1)
180+
})
175181
})
176182

177183
describe('findOrCreateDvcYamlFile', () => {

extension/src/fileSystem/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ export const isDirectory = (path: string): boolean =>
3636

3737
export const isFile = (path: string): boolean => checkStats(path, 'isFile')
3838

39-
export const getModifiedTime = (path: string): number =>
40-
lstatSync(path).mtime.getTime()
39+
export const getModifiedTime = (path: string): number => {
40+
if (exists(path)) {
41+
return lstatSync(path).mtime.getTime()
42+
}
43+
return -1
44+
}
4145

4246
export const findSubRootPaths = async (
4347
cwd: string,

0 commit comments

Comments
 (0)