pylance-release icon indicating copy to clipboard operation
pylance-release copied to clipboard

Moving Folders And Multiple Files - Python Imports Not Refactoring

Open RealTachyon opened this issue 9 months ago • 10 comments

Type: Bug

Behaviour

When moving a folder within a python workspace to another location, all the modules that reference relative imports to another location outside the folder are not automatically updated.

The automatic update behavior is only executed when a single file is moved at once.

The same breaking behavior can be observed when multiple (2 or more) files are moved at the same time to a new folder.

Original structure: As a screenshot Image As a zip parent_folder.zip

Correct behavior

Moving a single file

Steps to Reproduce:

  1. Write out the structure that's given in the screenshot. (You can also find it as an attachment to this post.)
  2. Drag and drop the "animal.py" file into the "new_source_module" folder.
  3. Observe that the imports in the "human.py" are correctly updated to reflect the new location. The import line changed from from ..source_modules.animal import Animal to from ..new_source_modules.animal import Animal

Problematic behavior

Moving folders

Steps to Reproduce:

  1. Write out the structure that's given in the screenshot. (You can also find it as an attachment to this post.)
  2. Drag and drop the "source_modules" folder into the "new_source_module" folder.
  3. Observe that the imports in the "human.py" are not updated to reflect the new locations.

Moving multiple files

Steps to Reproduce:

  1. Write out the structure that's given in the screenshot. (You can also find it as an attachment to this post.)
  2. Drag and drop the "animal.py" and "toy.py" files into the "new_source_module" folder.
  3. Observe that the imports in the "human.py" are not updated to reflect the new locations.

See also the issue here

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

2025-03-17 13:41:41.574 [info] Experiment 'pythonRecommendTensorboardExt' is active
2025-03-17 13:41:41.575 [info] Experiment 'pythonTerminalEnvVarActivation' is active
2025-03-17 13:41:41.575 [info] Experiment 'pythonTestAdapter' is active
2025-03-17 13:41:41.575 [info] Native locator: Refresh started
2025-03-17 13:41:41.575 [info] > pyenv which python
2025-03-17 13:41:41.575 [info] cwd: .
2025-03-17 13:41:42.684 [info] Poetry environment manager found at: C:\Users\David\AppData\Local\Programs\Python\Python313\Scripts\poetry.exe
2025-03-17 13:41:42.763 [info] Python interpreter path: ~\AppData\Local\Programs\Python\Python313\python.exe
2025-03-17 13:41:43.690 [info] Native locator: Refresh finished in 9555 ms
2025-03-17 13:41:53.726 [info] Starting Pylance language server.
2025-03-17 13:43:53.296 [info] > conda info --json
2025-03-17 13:59:12.530 [info] Discover tests for workspace name: playground_2 - uri: l:\Programming\Python\Projects\Experiments\playground_2\refactoring_test\source_stuff\toy.py
2025-03-17 13:59:20.428 [info] Discover tests for workspace name: playground_2 - uri: l:\Programming\Python\Projects\Experiments\playground_2\refactoring_test\target_stuff\human.py
2025-03-17 13:59:22.064 [info] Discover tests for workspace name: playground_2 - uri: l:\Programming\Python\Projects\Experiments\playground_2\refactoring_test\target_stuff\human.py
2025-03-17 15:51:17.950 [info] Discover tests for workspace name: undefined - uri: c:\Users\David\AppData\Roaming\Code\User\settings.json
2025-03-17 15:54:37.547 [info] Experiment 'pythonRecommendTensorboardExt' is active
2025-03-17 15:54:37.547 [info] Experiment 'pythonTerminalEnvVarActivation' is active
2025-03-17 15:54:37.547 [info] Experiment 'pythonTestAdapter' is active
2025-03-17 15:54:37.547 [info] Native locator: Refresh started
2025-03-17 15:54:37.564 [info] > pyenv which python
2025-03-17 15:54:37.564 [info] cwd: .
2025-03-17 15:54:38.048 [info] Poetry environment manager found at: C:\Users\David\AppData\Local\Programs\Python\Python313\Scripts\poetry.exe
2025-03-17 15:54:38.051 [info] Python interpreter path: ~\AppData\Local\Programs\Python\Python313\python.exe
2025-03-17 15:54:38.125 [info] Native locator: Refresh finished in 640 ms
2025-03-17 15:54:38.797 [info] Starting Pylance language server.

Extension version: 2025.2.0 VS Code version: Code 1.98.2 (ddc367ed5c8936efe395cffeec279b04ffd7db78, 2025-03-12T13:32:45.399Z) OS version: Windows_NT x64 10.0.19045 Modes:

  • Python version (& distribution if applicable, e.g. Anaconda): 3.13.1
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): System
  • Value of the python.languageServer setting: Default
User Settings


languageServer: "Pylance"

Installed Extensions
Extension Name Extension Id Version
debugpy ms- 2025.4.1
js-debug ms- 1.97.1
js-debug-companion ms- 1.1.3
python ms- 2025.2.0
vscode-js-profile-table ms- 1.0.10
vscode-pylance ms- 2025.3.2
System Info
Item Value
CPUs AMD Ryzen Threadripper 2970WX 24-Core Processor (48 x 2994)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) undefined
Memory (System) 95.88GB (53.22GB free)
Process Argv --crash-reporter-id 0f924fbb-88c9-44bf-bd09-f8e30396fdf9 --crash-reporter-id 0f924fbb-88c9-44bf-bd09-f8e30396fdf9
Screen Reader no
VM 0%
A/B Experiments
vsliv368:30146709
vspor879:30202332
vspor708:30202333
vspor363:30204092
vscod805cf:30301675
binariesv615:30325510
py29gd2263:31024239
vscaat:30438848
c4g48928:30535728
azure-dev_surveyone:30548225
962ge761:30959799
h48ei257:31000450
pythontbext0:30879054
cppperfnew:31000557
dwnewjupytercf:31046870
nativerepl1:31139838
pythonrstrctxt:31112756
nativeloc1:31192215
iacca1:31171482
5fd0e150:31155592
dwcopilot:31170013
6074i472:31201624
dwoutputs:31242946
customenabled:31248079
hdaa2157:31222309
copilot_t_ci:31222730
968h8231:31259996
jda6j935:31233686
pythoneinst12:31257679
fh1c7952:31258891
4gafe986:31261448
31787653:31256342

RealTachyon avatar Mar 17 '25 15:03 RealTachyon

Thanks for the issue. I can reproduce the problem. Strangely, renaming the folder works, but drag and drop doesn't.

rchiodo avatar Mar 18 '25 17:03 rchiodo

Yes, I found that renaming the folder works for me as well. However, not in the capacity that when you rename it to move it into a new parent.

So naming it source_modules_2 works, and refactors the imports. But when naming it new_source_modules\source_modules_2 it moves it to the correct location, but doesn't refactor the imports.

RealTachyon avatar Mar 18 '25 20:03 RealTachyon

Is there any update on how development of this is coming along?

RealTachyon avatar Apr 03 '25 02:04 RealTachyon

Sorry it's on the backlog but not being worked on at the moment. We generally use up votes to determine what to work on.

rchiodo avatar Apr 03 '25 16:04 rchiodo

Are the upvotes the 👍 emoji on the actual post? Or how is this metric being analyzed?

RealTachyon avatar May 10 '25 14:05 RealTachyon

Are the upvotes the 👍 emoji on the actual post? Or how is this metric being analyzed?

Yes, the 👍 on the original comment at the top.

debonte avatar May 10 '25 14:05 debonte

Fixing this feature will be of enormous value to my team in a few weeks, when we finally do the big refactor we've been wanting to do, moving all our Django app folders up out of the top-level directory of the repo and into a subfolder.

I tried doing this via rename, by changing the folder search to multitenant/search, which did in fact move the search folder into the multitenant subfolder, but it did not trigger the refactorer. Renaming it to just search2 also did not work.

Renaming a .py file does successfully trigger the refactorer, but not renaming a folder, so I'm surprised to see several people report that such a thing does work for them.

Here's my VSCode info:

Version: 1.100.2 Commit: 848b80aeb52026648a8ff9f7c45a9b0a80641e2e Date: 2025-05-14T21:47:40.416Z (2 wks ago) Electron: 34.5.1 ElectronBuildId: 11369351 Chromium: 132.0.6834.210 Node.js: 20.19.0 V8: 13.2.152.41-electron.0 OS: Darwin arm64 24.5.0. (Mac OS Sequoia 15.5)

coredumperror avatar May 29 '25 23:05 coredumperror

Hi folks, has it always worked this way?

I just noticed it in my project while refactoring its structure.

Moving files works fine though...

I tried installing a specific Pylance version (6–12 months ago), but it had no effect.
Has it always behaved like this (with folders), or is it just the Mandela Effect? 😅

Glitchy-Sheep avatar Aug 01 '25 08:08 Glitchy-Sheep

Hey, is there any estimate on when this might be worked on? This issue has been sitting here with quite a few 👍 upvotes for several months now.

RealTachyon avatar Sep 20 '25 16:09 RealTachyon

Sorry it's not on our backlog yet.

rchiodo avatar Sep 22 '25 16:09 rchiodo