Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2d042a2
ref(node): use readline for context lines
JonasBa May 25, 2024
1fc2544
test(node): contextline tests
JonasBa May 26, 2024
b0df967
test(node): contextline tests
JonasBa May 26, 2024
3a785bc
ref(node): contextlines cache attempt
JonasBa May 28, 2024
28f576f
ref(node) exclude other contextlines extensions
JonasBa Jun 9, 2024
2cfda9b
test(node) fix contextlines test
JonasBa Jun 10, 2024
db2df8d
fix scoped test
JonasBa Jun 10, 2024
2be28a5
lint
JonasBa Jun 10, 2024
93e8173
fix regexp danger warning
JonasBa Jun 10, 2024
a949d73
Merge branch 'develop' into jb/node/contextlines-memory
JonasBa Jun 10, 2024
584dad2
strict undefined check
JonasBa Jun 10, 2024
00fd3fc
reshuffle order
JonasBa Jun 10, 2024
6588673
test adding error handler on input stream
JonasBa Jun 10, 2024
96a617e
lint
JonasBa Jun 10, 2024
b4a51f6
fix duplicate reads on stream error
JonasBa Jun 11, 2024
421178e
fix duplicate reads on stream error
JonasBa Jun 11, 2024
306a11b
add emplace
JonasBa Jun 11, 2024
0dca6c5
fix(node) last line is not picked up as it does not include \n
JonasBa Jun 11, 2024
dea113b
ref(contexlines) use ordered startsWith instead of regexp and store c…
JonasBa Jun 12, 2024
e684eb6
Merge branch 'develop' into jb/node/contextlines-memory
JonasBa Jun 12, 2024
9af7dfc
ref(node) remove all but cache set snipline occurences
JonasBa Jun 12, 2024
83a8d05
Merge branch 'develop' into jb/node/contextlines-memory
JonasBa Jun 13, 2024
17dea5b
Merge branch 'develop' into jb/node/contextlines-memory
JonasBa Jun 13, 2024
2e3fe8f
Merge branch 'develop' into jb/node/contextlines-memory
JonasBa Jun 14, 2024
eecbf5f
Merge branch 'develop' into jb/node/contextlines-memory
JonasBa Jun 17, 2024
e79dac2
fix(contextlines) update type assertions
JonasBa Jun 17, 2024
ef1fa79
fix(contextlines) use runtime checks instead
JonasBa Jun 17, 2024
cc4305a
ref(node) fix types in tests
JonasBa Jun 17, 2024
bd35821
test(node) fix wrong type assertion
JonasBa Jun 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(contextlines) update type assertions
  • Loading branch information
JonasBa committed Jun 17, 2024
commit e79dac25be3ac4a8deeb03a81846403caf1b9c70
45 changes: 27 additions & 18 deletions packages/node/src/integrations/contextlines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function makeLineReaderRanges(lines: number[], linecontext: number): ReadlineRan
}

let i = 0;
let current = makeContextRange(lines[i], linecontext);
let current = makeContextRange(lines[i]!, linecontext);
const out: ReadlineRange[] = [];
// eslint-disable-next-line no-constant-condition
while (true) {
Expand All @@ -94,7 +94,7 @@ function makeLineReaderRanges(lines: number[], linecontext: number): ReadlineRan
}

// If the next line falls into the current range, extend the current range to lineno + linecontext.
const next = lines[i + 1];
const next = lines[i + 1]!;
if (next <= current[1]) {
current[1] = next + linecontext;
} else {
Expand Down Expand Up @@ -124,8 +124,8 @@ function getContextLinesFromFile(path: string, ranges: ReadlineRange[], output:
// Init at zero and increment at the start of the loop because lines are 1 indexed.
let lineNumber = 0;
let currentRangeIndex = 0;
let rangeStart = ranges[currentRangeIndex][0];
let rangeEnd = ranges[currentRangeIndex][1];
let rangeStart = ranges[currentRangeIndex]?.[0]!;
let rangeEnd = ranges[currentRangeIndex]?.[1]!;

// We use this inside Promise.all, so we need to resolve the promise even if there is an error
// to prevent Promise.all from short circuiting the rest.
Expand Down Expand Up @@ -159,8 +159,8 @@ function getContextLinesFromFile(path: string, ranges: ReadlineRange[], output:
return;
}
currentRangeIndex++;
rangeStart = ranges[currentRangeIndex][0];
rangeEnd = ranges[currentRangeIndex][1];
rangeStart = ranges[currentRangeIndex]?.[0]!;
rangeEnd = ranges[currentRangeIndex]?.[1]!;
}
});
});
Expand All @@ -180,18 +180,20 @@ async function addSourceContext(event: Event, contextLines: number): Promise<Eve
// Maps preserve insertion order, so we iterate in reverse, starting at the
// outermost frame and closer to where the exception has occurred (poor mans priority)
for (let i = exception.stacktrace.frames.length - 1; i >= 0; i--) {
const frame = exception.stacktrace.frames[i];
const frame: StackFrame | undefined = exception.stacktrace.frames[i];
const filename = frame?.filename;

if (
typeof frame.filename !== 'string' ||
!frame ||
typeof filename !== 'string' ||
typeof frame.lineno !== 'number' ||
Comment thread
JonasBa marked this conversation as resolved.
Resolved
shouldSkipContextLinesForFile(frame.filename)
shouldSkipContextLinesForFile(filename)
) {
continue;
}

if (!filesToLines[frame.filename]) filesToLines[frame.filename] = [];
filesToLines[frame.filename].push(frame.lineno);
if (!filesToLines[filename]) filesToLines[filename] = [];
filesToLines[filename]!.push(frame.lineno);
}
}
}
Expand All @@ -203,15 +205,20 @@ async function addSourceContext(event: Event, contextLines: number): Promise<Eve

const readlinePromises: Promise<void>[] = [];
for (const file of files) {
// If we failed to read this before, dont try again.
// If we failed to read this before, dont try reading it again.
if (LRU_FILE_CONTENTS_FS_READ_FAILED.get(file)) {
continue;
}

const filesToLineRanges = filesToLines[file];
if (!filesToLineRanges) {
continue;
}

// Sort ranges so that they are sorted by line increasing order and match how the file is read.
filesToLines[file].sort((a, b) => a - b);
filesToLineRanges.sort((a, b) => a - b);
// Check if the contents are already in the cache and if we can avoid reading the file again.
const ranges = makeLineReaderRanges(filesToLines[file], contextLines);
const ranges = makeLineReaderRanges(filesToLineRanges, contextLines);
if (ranges.every(r => rangeExistsInContentCache(file, r))) {
continue;
}
Expand Down Expand Up @@ -287,13 +294,14 @@ export function addContextToFrame(
for (let i = makeRangeStart(lineno, contextLines); i < lineno; i++) {
// We always expect the start context as line numbers cannot be negative. If we dont find a line, then
// something went wrong somewhere. Clear the context and return without adding any linecontext.
if (contents[i] === undefined) {
const line = contents[i];
if (line === undefined) {
clearLineContext(frame);
DEBUG_BUILD && logger.error(`Could not find line ${i} in file ${frame.filename}`);
return;
}

frame.pre_context.push(contents[i]);
frame.pre_context.push(line);
}

// We should always have the context line. If we dont, something went wrong, so we clear the context and return
Expand All @@ -311,10 +319,11 @@ export function addContextToFrame(
for (let i = lineno + 1; i <= end; i++) {
// Since we dont track when the file ends, we cant clear the context if we dont find a line as it could
// just be that we reached the end of the file.
if (contents[i] === undefined) {
let line = contents[i];
if (line === undefined) {
break;
}
frame.post_context.push(contents[i]);
frame.post_context.push(line);
}
}

Expand Down