Skip to content

Commit 544409f

Browse files
committed
Handle token reader failures per file
1 parent 2eb4681 commit 544409f

3 files changed

Lines changed: 32 additions & 18 deletions

File tree

packages/code-map/__tests__/parse.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,5 +619,22 @@ console.log('Total:', formatCurrency(total));
619619
expect(typeof result.tokenScores).toBe('object')
620620
expect(typeof result.tokenCallers).toBe('object')
621621
})
622+
623+
it('should continue scoring when a provided reader rejects for one file', async () => {
624+
const result = await getFileTokenScores(
625+
'/tmp/test-project',
626+
['src/unreadable.ts', 'src/readable.ts'],
627+
async (filePath: string) => {
628+
if (filePath === 'src/unreadable.ts') {
629+
throw new Error('permission denied')
630+
}
631+
632+
return 'export function readable() { return helper() }\nfunction helper() { return 1 }\n'
633+
},
634+
)
635+
636+
expect(result.tokenScores).toBeDefined()
637+
expect(result.tokenCallers).toBeDefined()
638+
})
622639
})
623640
})

packages/code-map/src/parse.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,19 @@ async function parseTokensForScoring(params: {
164164
})
165165
}
166166

167-
const source = await readFile(filePath)
168-
return parseTokensWithLimits(filePath, languageConfig, () => source, {
169-
maxBytes: MAX_PARSE_FILE_BYTES,
170-
remainingBytes,
171-
})
167+
try {
168+
const source = await readFile(filePath)
169+
return parseTokensWithLimits(filePath, languageConfig, () => source, {
170+
maxBytes: MAX_PARSE_FILE_BYTES,
171+
remainingBytes,
172+
})
173+
} catch (e) {
174+
if (DEBUG_PARSING) {
175+
console.error(`Error reading source: ${e}`)
176+
console.log(filePath)
177+
}
178+
return emptyParsedTokens(false)
179+
}
172180
}
173181

174182
function parseTokensWithLimits(

sdk/src/run-state.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ type ProjectIndexInput = {
141141
readFile?: (filePath: string) => string | null | Promise<string | null>
142142
}
143143

144-
const DEFAULT_SYMBOL_PARSE_FILE_BYTES = 1_000_000
144+
const MAX_DISCOVERED_PROJECT_READ_BYTES = 1_000_000
145145

146146
async function computeProjectIndex(params: ProjectIndexInput): Promise<{
147147
fileTree: FileTreeNode[]
@@ -205,13 +205,12 @@ function createDiscoveredProjectReader(params: {
205205
logger: Logger
206206
}): (filePath: string) => Promise<string | null> {
207207
const { cwd, fs, logger } = params
208-
const maxBytes = getSymbolParseFileByteLimit()
209208

210209
return async (filePath: string) => {
211210
const fullPath = path.join(cwd, filePath)
212211
try {
213212
const stats = await fs.stat(fullPath)
214-
if (getFileSize(stats) > maxBytes) {
213+
if (getFileSize(stats) > MAX_DISCOVERED_PROJECT_READ_BYTES) {
215214
return null
216215
}
217216
return await fs.readFile(fullPath, 'utf8')
@@ -229,16 +228,6 @@ function getFileSize(stats: Awaited<ReturnType<CodebuffFileSystem['stat']>>) {
229228
return typeof stats.size === 'number' ? stats.size : 0
230229
}
231230

232-
function getSymbolParseFileByteLimit() {
233-
const raw = process.env.CODEBUFF_MAX_PARSE_FILE_BYTES
234-
if (!raw) return DEFAULT_SYMBOL_PARSE_FILE_BYTES
235-
236-
const parsed = Number.parseInt(raw, 10)
237-
return Number.isFinite(parsed) && parsed > 0
238-
? parsed
239-
: DEFAULT_SYMBOL_PARSE_FILE_BYTES
240-
}
241-
242231
/**
243232
* Helper to convert ChildProcess to Promise with stdout/stderr
244233
*/

0 commit comments

Comments
 (0)