Skip to content

Commit df79d9a

Browse files
fs: stat() throws if file does not exist
Signed-off-by: nkomonen <[email protected]>
1 parent 352dfdf commit df79d9a

File tree

6 files changed

+13
-29
lines changed

6 files changed

+13
-29
lines changed

src/amazonqFeatureDev/util/files.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,5 @@ export async function prepareRepoData(
133133

134134
export async function getSourceCodePath(workspaceRoot: string, projectRoot: string) {
135135
const srcRoot = path.join(workspaceRoot, projectRoot)
136-
const srcFound = await fsCommon.stat(srcRoot)
137-
return srcFound !== undefined ? srcRoot : workspaceRoot
136+
return (await fsCommon.directoryExists(srcRoot)) ? srcRoot : workspaceRoot
138137
}

src/auth/providers/sharedCredentialsProviderFactory.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,6 @@ export class SharedCredentialsProviderFactory extends BaseCredentialsProviderFac
8181
private async getLastModifiedMillis(filepath: string): Promise<number | undefined> {
8282
try {
8383
const stat = await fsCommon.stat(filepath)
84-
85-
if (stat === undefined) {
86-
throw new Error(`Cannot get stat() of ${filepath}`)
87-
}
88-
8984
return stat.mtime
9085
} catch (err) {
9186
return undefined

src/shared/filesystemUtilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export async function getDirSize(
4242
return getDirSize(filePath, startTime, duration, fileExt)
4343
}
4444
if (type === vscode.FileType.File && fileName.endsWith(fileExt)) {
45-
const stat = (await fsCommon.stat(filePath))!
45+
const stat = await fsCommon.stat(filePath)
4646
return stat.size
4747
}
4848

src/shared/logger/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function cleanLogFiles(logDir: string, params = defaultCleanupParam
3131
const logFullPath = path.join(logDir, log)
3232
let logSize: number = 0
3333
try {
34-
logSize = (await fsCommon.stat(logFullPath))!.size
34+
logSize = (await fsCommon.stat(logFullPath)).size
3535
} catch (e) {
3636
getLogger().error('cleanLogFiles: fs.stat() failed on file "%s": %s', logFullPath, (e as Error).message)
3737
}

src/srcShared/fs.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,13 @@ export class FileSystemCommon {
8484

8585
async exists(path: Uri | string, fileType?: vscode.FileType): Promise<boolean> {
8686
path = FileSystemCommon.getUri(path)
87-
const stat = await this.stat(path)
88-
89-
// No specific filetype, so only check if anything exists
90-
if (fileType === undefined) {
91-
return stat !== undefined
87+
try {
88+
const stat = await this.stat(path)
89+
// check filetype if it was given
90+
return fileType === undefined ? true : stat.type === fileType
91+
} catch (e) {
92+
return false
9293
}
93-
94-
// Check if file exists and is expected filetype
95-
return stat === undefined ? false : stat.type === fileType
9694
}
9795

9896
async fileExists(path: Uri | string): Promise<boolean> {
@@ -111,16 +109,9 @@ export class FileSystemCommon {
111109
/**
112110
* The stat of the file, undefined if the file does not exist, otherwise an error is thrown.
113111
*/
114-
async stat(uri: vscode.Uri | string): Promise<vscode.FileStat | undefined> {
112+
async stat(uri: vscode.Uri | string): Promise<vscode.FileStat> {
115113
const path = FileSystemCommon.getUri(uri)
116-
try {
117-
return await fs.stat(path)
118-
} catch (err) {
119-
if (err instanceof vscode.FileSystemError && err.code === 'FileNotFound') {
120-
return undefined
121-
}
122-
throw err
123-
}
114+
return await fs.stat(path)
124115
}
125116

126117
async delete(uri: vscode.Uri | string): Promise<void> {

src/test/srcShared/fs.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,9 @@ describe('FileSystem', function () {
219219
assert.strictEqual(stat.type, vscode.FileType.File)
220220
})
221221

222-
it('return undefined if no file exists', async function () {
222+
it('throws if no file exists', async function () {
223223
const filePath = createTestPath('thisDoesNotExist.txt')
224-
const stat = await fsCommon.stat(filePath)
225-
assert.strictEqual(stat, undefined)
224+
await assert.rejects(() => fsCommon.stat(filePath))
226225
})
227226
})
228227

0 commit comments

Comments
 (0)