Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added error handling for very large source files. This situation is n… #1973

Merged
merged 2 commits into from
Jun 10, 2021
Merged
Changes from 1 commit
Commits
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
13 changes: 13 additions & 0 deletions packages/pyright-internal/src/analyzer/sourceFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,12 @@ import { SymbolTable } from './symbol';
import { TestWalker } from './testWalker';
import { TypeEvaluator } from './typeEvaluator';

// Limit the number of import cycles tracked per source file.
const _maxImportCyclesPerFile = 4;

// Allow files up to 16MB in length.
const _maxSourceFileSize = 16 * 1024 * 1024;

interface ResolveImportResult {
imports: ImportResult[];
builtinsImportResult?: ImportResult;
Expand Down Expand Up @@ -520,6 +524,15 @@ export class SourceFile {
let fileContents = this.getFileContents();
if (fileContents === undefined) {
try {
const fileStat = this.fileSystem.statSync(this._filePath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this section down into the timeOperation below? That way, we'll count the time it takes to stat in addition to the read itself and not have unaccounted time.

if (fileStat.size > _maxSourceFileSize) {
this._console.error(
`File length of "${this._filePath}" is ${fileStat.size} ` +
`which exceeds the maximum supported file size of ${_maxSourceFileSize}`
);
throw new Error('File larger than max');
}

const startTime = timingStats.readFileTime.totalTime;
timingStats.readFileTime.timeOperation(() => {
// Read the file's contents.
Expand Down