From 5568b4dc95f9610da3cd4263108f04de94841d6e Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Mon, 19 Jun 2023 18:00:31 -0700 Subject: [PATCH] Enhanced command-line version of pyright to allow file or directory names to be passed via stdin if `-` option is used in the command line. This addresses https://github.com/microsoft/pyright/issues/5342. --- docs/command-line.md | 1 + packages/pyright-internal/src/pyright.ts | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/docs/command-line.md b/docs/command-line.md index 086844156012..0777f3ef9dfb 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -25,6 +25,7 @@ Pyright can be run as either a VS Code extension or as a node-based command-line | --version | Print pyright version and exit | | --warnings | Use exit code of 1 if warnings are reported | | -w, --watch | Continue to run and watch for changes (5) | +| - | Read file or directory list from stdin | (1) If specific files are specified on the command line, it overrides the files or directories specified in the pyrightconfig.json or pyproject.toml file. diff --git a/packages/pyright-internal/src/pyright.ts b/packages/pyright-internal/src/pyright.ts index 09fc452a45bf..b524e1a705ed 100644 --- a/packages/pyright-internal/src/pyright.ts +++ b/packages/pyright-internal/src/pyright.ts @@ -15,6 +15,7 @@ import { timingStats } from './common/timing'; import chalk from 'chalk'; import commandLineArgs, { CommandLineOptions, OptionDefinition } from 'command-line-args'; +import * as fs from 'fs'; import { PackageTypeReport, TypeKnownStatus } from './analyzer/packageTypeReport'; import { PackageTypeVerifier } from './analyzer/packageTypeVerifier'; @@ -218,7 +219,24 @@ async function processArgs(): Promise { // Assume any relative paths are relative to the working directory. if (args.files && Array.isArray(args.files)) { - options.fileSpecs = args.files; + let fileSpecList = args.files; + + // 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'); + fileSpecList = stdText + .trim() + .split(' ') + .map((s) => s.trim()) + .filter((s) => !s); + } catch (e) { + console.error('Invalid file list specified by stdin input.'); + return ExitStatus.ParameterError; + } + } + + options.fileSpecs = fileSpecList; options.fileSpecs = options.fileSpecs.map((f) => combinePaths(process.cwd(), f)); } else { options.fileSpecs = []; @@ -729,7 +747,8 @@ function printUsage() { ' --verifytypes Verify type completeness of a py.typed package\n' + ' --version Print Pyright version and exit\n' + ' --warnings Use exit code of 1 if warnings are reported\n' + - ' -w,--watch Continue to run and watch for changes\n' + ' -w,--watch Continue to run and watch for changes\n' + + ' - Read files from stdin\n' ); }