( projectRoot: string, storedPath: string, record: FileRecord )
| 236 | * bounded by {@link MAX_SOURCE_BYTES} like every other read here. |
| 237 | */ |
| 238 | export function readFileShape( |
| 239 | projectRoot: string, |
| 240 | storedPath: string, |
| 241 | record: FileRecord |
| 242 | ): { drift: boolean; totalLines: number | null; reason?: string } { |
| 243 | let absolute: string; |
| 244 | try { |
| 245 | absolute = resolveProjectFile(projectRoot, storedPath); |
| 246 | } catch { |
| 247 | // A refusal on a path the INDEX handed us is not a request to refuse — the |
| 248 | // caller already passed the chokepoint. Treat it as unreadable. |
| 249 | return { drift: false, totalLines: null }; |
| 250 | } |
| 251 | try { |
| 252 | const stats = fs.statSync(absolute); |
| 253 | if (stats.size > MAX_SOURCE_BYTES) { |
| 254 | return { drift: false, totalLines: null, reason: 'The file is too large to read here.' }; |
| 255 | } |
| 256 | const content = fs.readFileSync(absolute, 'utf-8'); |
| 257 | const hashed = indexedHashInput(stats.size, () => content); |
| 258 | const drift = createHash('sha256').update(hashed).digest('hex') !== record.contentHash; |
| 259 | return { |
| 260 | drift, |
| 261 | totalLines: splitLines(content).length, |
| 262 | ...(drift |
| 263 | ? { |
| 264 | reason: |
| 265 | 'This file changed on disk after the last index sync, so the line ' + |
| 266 | 'numbers the graph holds no longer match it.', |
| 267 | } |
| 268 | : {}), |
| 269 | }; |
| 270 | } catch { |
| 271 | return { |
| 272 | drift: true, |
| 273 | totalLines: null, |
| 274 | reason: 'The file is in the index but could not be read from disk.', |
| 275 | }; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | export interface SourceResult { |
| 280 | file: string; |
no test coverage detected