diff --git a/packages/pyright-internal/src/analyzer/renameUsageFinder.ts b/packages/pyright-internal/src/analyzer/renameUsageFinder.ts index c78244c5ff..3aaa16eafa 100644 --- a/packages/pyright-internal/src/analyzer/renameUsageFinder.ts +++ b/packages/pyright-internal/src/analyzer/renameUsageFinder.ts @@ -1,7 +1,3 @@ -/** - * visitor that looks for imports of an old file path and creates {@link TextEdit}s to update them for the new file path - */ - import { TextEdit } from 'vscode-languageserver-types'; import { ModuleNameNode, NameNode, ParseNodeType } from '../parser/parseNodes'; import { ParseTreeWalker } from './parseTreeWalker'; @@ -14,6 +10,9 @@ import { TextRange } from '../common/textRange'; import { ModuleType, TypeCategory } from './types'; import { Program } from './program'; +/** + * visitor that looks for imports of an old file path and creates {@link TextEdit}s to update them for the new file path + */ export class RenameUsageFinder extends ParseTreeWalker { edits: TextEdit[] = []; private _oldModuleName: string; diff --git a/packages/pyright-internal/src/languageServerBase.ts b/packages/pyright-internal/src/languageServerBase.ts index 0fec7a0ac4..36e3530549 100644 --- a/packages/pyright-internal/src/languageServerBase.ts +++ b/packages/pyright-internal/src/languageServerBase.ts @@ -1156,9 +1156,16 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis } const workspace = await this.getWorkspaceForFile(newUri); const program = workspace.service.backgroundAnalysisProgram.program; + + // if the uri being renamed is not part of the workspace, don't bother + if (!program.containsSourceFileIn(oldUri)) { + continue; + } + + const oldFileContents = program.getParseResults(oldUri); workspace.service.getUserFiles().forEach((file) => { const currentFileParseResults = program.getParseResults(file); - const oldFile = program.getParseResults(oldUri) ?? oldUri; + const oldFile = oldFileContents ?? oldUri; if (currentFileParseResults && workspace.rootUri && program.evaluator) { const importFinder = new RenameUsageFinder(program, currentFileParseResults, oldFile, newUri); importFinder.walk(currentFileParseResults.parserOutput.parseTree); diff --git a/packages/pyright-internal/src/tests/languageServer.test.ts b/packages/pyright-internal/src/tests/languageServer.test.ts index b65b577209..99f0117b57 100644 --- a/packages/pyright-internal/src/tests/languageServer.test.ts +++ b/packages/pyright-internal/src/tests/languageServer.test.ts @@ -777,7 +777,7 @@ describe(`Basic language server tests`, () => { const result = await serverInfo.connection.sendRequest( WillRenameFilesRequest.type, { - files: [{ oldUri: 'file:///src/foo/bar', newUri: 'file:///src/foo/bar2' }], + files: [{ oldUri: marker.fileUri.toString(), newUri: 'file:///src/foo/bar2.py' }], }, CancellationToken.None );