Skip to content

Commit 63abf49

Browse files
committed
modify to use current interpreter path for resolving sys.path
1 parent 8179c18 commit 63abf49

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/client/application/importPath/copyImportPathCommand.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { IClipboard, ICommandManager, IWorkspaceService } from '../../common/app
66
import { IExtensionSingleActivationService } from '../../activation/types';
77
import { Commands } from '../../common/constants';
88
import { getSysPath } from '../../common/utils/pythonUtils';
9+
import { IInterpreterPathService } from '../../common/types';
910

1011
@injectable()
1112
export class CopyImportPathCommand implements IExtensionSingleActivationService {
@@ -15,6 +16,7 @@ export class CopyImportPathCommand implements IExtensionSingleActivationService
1516
@inject(ICommandManager) private readonly commands: ICommandManager,
1617
@inject(IWorkspaceService) private readonly workspace: IWorkspaceService,
1718
@inject(IClipboard) private readonly clipboard: IClipboard,
19+
@inject(IInterpreterPathService) private readonly interpreterPathService: IInterpreterPathService,
1820
) {}
1921

2022
async activate(): Promise<void> {
@@ -28,7 +30,9 @@ export class CopyImportPathCommand implements IExtensionSingleActivationService
2830
return;
2931
}
3032

31-
const importPath = this.resolveImportPath(uri.fsPath);
33+
const resource: vscode.Uri | undefined = uri ?? this.workspace.workspaceFolders?.[0]?.uri;
34+
const pythonPath = this.interpreterPathService.get(resource);
35+
const importPath = this.resolveImportPath(uri.fsPath, pythonPath);
3236
await this.clipboard.writeText(importPath);
3337
void vscode.window.showInformationMessage(`Copied: ${importPath}`);
3438
}
@@ -45,9 +49,9 @@ export class CopyImportPathCommand implements IExtensionSingleActivationService
4549
* @param absPath - The absolute path to a `.py` file.
4650
* @returns The resolved import path in dotted notation (e.g., 'pkg.module').
4751
*/
48-
private resolveImportPath(absPath: string): string {
52+
private resolveImportPath(absPath: string, pythonPath?: string): string {
4953
// ---------- ① sys.path ----------
50-
for (const sysRoot of getSysPath()) {
54+
for (const sysRoot of getSysPath(pythonPath)) {
5155
if (sysRoot && absPath.startsWith(sysRoot)) {
5256
return CopyImportPathCommand.toDotted(path.relative(sysRoot, absPath));
5357
}

src/test/application/importPath/copyImportPathCommand.unit.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ import * as pythonUtils from '../../../client/common/utils/pythonUtils';
1313
import { ClipboardService } from '../../../client/common/application/clipboard';
1414
import { CommandManager } from '../../../client/common/application/commandManager';
1515
import { WorkspaceService } from '../../../client/common/application/workspace';
16+
import { IInterpreterPathService } from '../../../client/common/types';
17+
import { InterpreterPathService } from '../../../client/common/interpreterPathService';
1618

1719
suite('Copy Import Path Command', () => {
1820
let command: CopyImportPathCommand;
1921
let commandManager: ICommandManager;
2022
let workspaceService: IWorkspaceService;
2123
let clipboard: IClipboard;
24+
let interpreterPathService: IInterpreterPathService;
2225
let originalGetSysPath: () => string[];
2326

2427
let clipboardText = '';
@@ -27,7 +30,13 @@ suite('Copy Import Path Command', () => {
2730
commandManager = mock(CommandManager);
2831
workspaceService = mock(WorkspaceService);
2932
clipboard = mock(ClipboardService);
30-
command = new CopyImportPathCommand(instance(commandManager), instance(workspaceService), instance(clipboard));
33+
interpreterPathService = mock(InterpreterPathService);
34+
command = new CopyImportPathCommand(
35+
instance(commandManager),
36+
instance(workspaceService),
37+
instance(clipboard),
38+
instance(interpreterPathService),
39+
);
3140
originalGetSysPath = pythonUtils.getSysPath;
3241

3342
clipboardText = '';

0 commit comments

Comments
 (0)