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

Fix file rename check running on files that aren't part of the workspace #843

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 3 additions & 4 deletions packages/pyright-internal/src/analyzer/renameUsageFinder.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion packages/pyright-internal/src/languageServerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/languageServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
Loading