Skip to content

Commit

Permalink
Fixed bug that results in the command-line version of pyright not han…
Browse files Browse the repository at this point in the history
…dling long file lists provided via stdin. This addresses #9583.
  • Loading branch information
erictraut committed Dec 15, 2024
1 parent 9041f88 commit cc7a58f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
30 changes: 30 additions & 0 deletions packages/pyright-internal/src/common/streamUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* streamUtils.ts
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*
* Utility functions for dealing with standard IO streams in node.
*/

import { stdin } from 'process';

export async function getStdinBuffer() {
if (stdin.isTTY) {
return Buffer.alloc(0);
}

const result = [];
let length = 0;

for await (const chunk of stdin) {
result.push(chunk);
length += chunk.length;
}

return Buffer.concat(result, length);
}

export async function getStdin() {
const buffer = await getStdinBuffer();
return buffer.toString();
}
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/pyright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { timingStats } from './common/timing';

import chalk from 'chalk';
import commandLineArgs, { CommandLineOptions, OptionDefinition } from 'command-line-args';
import * as fs from 'fs';
import * as os from 'os';

import { ChildProcess, fork } from 'child_process';
Expand All @@ -39,6 +38,7 @@ import { PythonVersion } from './common/pythonVersion';
import { RealTempFile, createFromRealFileSystem } from './common/realFileSystem';
import { ServiceProvider } from './common/serviceProvider';
import { createServiceProvider } from './common/serviceProviderExtensions';
import { getStdin } from './common/streamUtils';
import { Range, isEmptyRange } from './common/textRange';
import { Uri } from './common/uri/uri';
import { getFileSpec, tryStat } from './common/uri/uriUtils';
Expand Down Expand Up @@ -255,15 +255,15 @@ async function processArgs(): Promise<ExitStatus> {
// Has the caller indicated that the file list will be supplied by stdin?
if (args.files.length === 1 && args.files[0] === '-') {
try {
const stdText = fs.readFileSync(process.stdin.fd, 'utf-8');
const stdText = await getStdin();
fileSpecList = stdText
.replace(/[\r\n]/g, ' ')
.trim()
.split(' ')
.map((s) => s.trim())
.filter((s) => !!s);
} catch (e) {
console.error('Invalid file list specified by stdin input.');
console.error('Invalid file list specified by stdin input');
return ExitStatus.ParameterError;
}
}
Expand All @@ -281,7 +281,7 @@ async function processArgs(): Promise<ExitStatus> {
try {
const stat = tryStat(tempFileSystem, includeSpec.wildcardRoot);
if (!stat) {
console.error(`File or directory "${includeSpec.wildcardRoot}" does not exist.`);
console.error(`File or directory "${includeSpec.wildcardRoot}" does not exist`);
return ExitStatus.ParameterError;
}
} catch {
Expand Down

0 comments on commit cc7a58f

Please sign in to comment.