Skip to content

Commit

Permalink
Fix pyright output schema and file watching (#6743)
Browse files Browse the repository at this point in the history
* Fix pyright output schema and file watching

* Fix build error
  • Loading branch information
rchiodo authored Dec 14, 2023
1 parent 81b5fa1 commit 8bb790c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
26 changes: 15 additions & 11 deletions packages/pyright-internal/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { FileSystem } from '../common/fileSystem';
import { FileWatcher, FileWatcherEventType, ignoredWatchEventFunction } from '../common/fileWatcher';
import { Host, HostFactory, NoAccessHost } from '../common/host';
import { defaultStubsDirectory } from '../common/pathConsts';
import { isRootedDiskPath } from '../common/pathUtils';
import { getFileName, isRootedDiskPath } from '../common/pathUtils';
import { ServiceProvider } from '../common/serviceProvider';
import { ServiceKeys } from '../common/serviceProviderExtensions';
import { Range } from '../common/textRange';
Expand Down Expand Up @@ -1252,33 +1252,35 @@ export class AnalyzerService {
this._console.info(`SourceFile: Received fs event '${event}' for path '${path}'`);
}

if (isIgnored(path.getFilePath())) {
if (isIgnored(path)) {
return;
}

// Wholesale ignore events that appear to be from tmp file / .git modification.
if (path.pathEndsWith('.tmp') || path.pathEndsWith('.git') || path.pathIncludes(_gitDirectory)) {
if (path.endsWith('.tmp') || path.endsWith('.git') || path.includes(_gitDirectory)) {
return;
}

let uri = Uri.file(path, this.fs.isCaseSensitive, /* checkRelative */ true);

// Make sure path is the true case.
path = this.fs.realCasePath(path);
uri = this.fs.realCasePath(uri);

const eventInfo = getEventInfo(this.fs, this._console, this._program, event, path);
const eventInfo = getEventInfo(this.fs, this._console, this._program, event, uri);
if (!eventInfo) {
// no-op event, return.
return;
}

if (!this._shouldHandleSourceFileWatchChanges(path, eventInfo.isFile)) {
if (!this._shouldHandleSourceFileWatchChanges(uri, eventInfo.isFile)) {
return;
}

// This is for performance optimization. If the change only pertains to the content of one file,
// then it can't affect the 'import resolution' result. All we need to do is reanalyze the related files
// (those that have a transitive dependency on this file).
if (eventInfo.isFile && eventInfo.event === 'change') {
this._backgroundAnalysisProgram.markFilesDirty([path], /* evenIfContentsAreSame */ false);
this._backgroundAnalysisProgram.markFilesDirty([uri], /* evenIfContentsAreSame */ false);
this._scheduleReanalysis(/* requireTrackedFileUpdate */ false);
return;
}
Expand Down Expand Up @@ -1442,16 +1444,18 @@ export class AnalyzerService {
this._console.info(`LibraryFile: Received fs event '${event}' for path '${path}'`);
}

if (isIgnored(path.getFilePath())) {
if (isIgnored(path)) {
return;
}

if (!this._shouldHandleLibraryFileWatchChanges(path, watchList)) {
const uri = Uri.file(path, this.fs.isCaseSensitive, /* checkRelative */ true);

if (!this._shouldHandleLibraryFileWatchChanges(uri, watchList)) {
return;
}

// If file doesn't exist, it is delete.
const isChange = event === 'change' && this.fs.existsSync(path);
const isChange = event === 'change' && this.fs.existsSync(uri);
this._scheduleLibraryAnalysis(isChange);
});
} catch {
Expand Down Expand Up @@ -1569,7 +1573,7 @@ export class AnalyzerService {
}

if (event === 'add' || event === 'change') {
const fileName = path.fileName;
const fileName = getFileName(path);
if (fileName && configFileNames.some((name) => name === fileName)) {
if (this._verboseOutput) {
this._console.info(`Received fs event '${event}' for config file`);
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/common/fileWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Stats } from './fileSystem';
import { Uri } from './uri/uri';

export type FileWatcherEventType = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir';
export type FileWatcherEventHandler = (eventName: FileWatcherEventType, uri: Uri, stats?: Stats) => void;
export type FileWatcherEventHandler = (eventName: FileWatcherEventType, path: string, stats?: Stats) => void;

export interface FileWatcher {
close(): void;
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/common/realFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ export class WorkspaceFileWatcherProvider implements FileWatcherProvider, FileWa
// for a file change. It is event handler's job to filter those out.
this._fileWatchers.forEach((watcher) => {
if (watcher.workspacePaths.some((dirPath) => fileUri.pathStartsWith(dirPath))) {
watcher.eventHandler(eventType, fileUri);
watcher.eventHandler(eventType, fileUri.getFilePath());
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/pyright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ interface PyrightPublicSymbolReport {
}

interface PyrightJsonDiagnostic {
uri: Uri;
file: string;
severity: SeverityLevel;
message: string;
range?: Range | undefined;
Expand Down Expand Up @@ -865,7 +865,7 @@ function convertDiagnosticCategoryToSeverity(category: DiagnosticCategory): Seve

function convertDiagnosticToJson(uri: Uri, diag: Diagnostic): PyrightJsonDiagnostic {
return {
uri,
file: uri.getFilePath(),
severity: convertDiagnosticCategoryToSeverity(diag.category),
message: diag.message,
range: isEmptyRange(diag.range) ? undefined : diag.range,
Expand Down Expand Up @@ -924,8 +924,8 @@ function reportDiagnosticsAsText(

function logDiagnosticToConsole(diag: PyrightJsonDiagnostic, prefix = ' ') {
let message = prefix;
if (!diag.uri.isEmpty()) {
message += `${diag.uri.toUserVisibleString()}:`;
if (diag.file) {
message += `${diag.file}:`;
}
if (diag.range && !isEmptyRange(diag.range)) {
message +=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class TestFileSystemWatcher implements FileWatcher {

fireFileChange(path: Uri, eventType: FileWatcherEventType): boolean {
if (this._paths.some((p) => path.startsWith(p))) {
this._listener(eventType, path);
this._listener(eventType, path.getFilePath());
return true;
}
return false;
Expand Down

0 comments on commit 8bb790c

Please sign in to comment.