microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.15k stars 12.51k forks source link

Refactor "Move to a new file" does not honor explicit file extensions #52886

Closed birgersp closed 1 year ago

birgersp commented 1 year ago

Type: Bug

When working on a ESM project, file extensions of imported paths need to be "explicit". In other words you need to write e.g. import * from "./somefile.js" (notice the .js at the end).

When the cursor is at a type or class, open the refactor menu (Ctrl+Shift+R) and select "Move to a new file".

image

Notice how the file then is automatically imported:

image

This will only be a valid import if the --moduleResolution is not set to node16 or nodenext. When you are working on a ESM project, you'll get the following compiler error:

Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Did you mean './MyClass.js'?

System info (generated from "Report Issue" button in VSCode):

VS Code version: Code 1.74.3 (97dec172d3256f8ca4bfb2143f3f76b503ca0534, 2023-01-09T16:57:40.428Z) OS version: Linux x64 6.0.12-76060006-generic Modes: Sandboxed: No Remote OS version: Linux x64 5.4.0-131-generic

System Info |Item|Value| |---|---| |CPUs|AMD Ryzen 9 5900HX with Radeon Graphics (16 x 1085)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
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_renderer: enabled_on
video_decode: disabled_software
video_encode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off| |Load (avg)|1, 1, 1| |Memory (System)|15.05GB (3.39GB free)| |Process Argv|dipai.code-workspace --crash-reporter-id 736a46a2-ea8f-4a41-bf49-9aa168b1e111| |Screen Reader|no| |VM|0%| |DESKTOP_SESSION|pop| |XDG_CURRENT_DESKTOP|Unity| |XDG_SESSION_DESKTOP|pop| |XDG_SESSION_TYPE|x11| |Item|Value| |---|---| |Remote|SSH: server.dipai.no| |OS|Linux x64 5.4.0-131-generic| |CPUs|Intel(R) Xeon(R) Silver 4216 CPU @ 2.10GHz (12 x 2099)| |Memory (System)|47.06GB (31.80GB free)| |VM|0%|
Extensions (16) Extension|Author (truncated)|Version ---|---|--- vscode-backtix|adi|2.1.0 tsco|alj|1.0.24 vscode-tailwindcss|bra|0.9.7 vscode-toggle-quotes|Bri|0.3.6 vscode-eslint|dba|2.4.0 prettier-vscode|esb|9.10.4 remote-containers|ms-|0.266.1 remote-ssh|ms-|0.96.0 remote-ssh-edit|ms-|0.84.0 remote-wsl|ms-|0.72.0 vscode-remote-extensionpack|ms-|0.23.0 remote-explorer|ms-|0.2.0 sqltools|mtx|0.27.1 sqltools-driver-pg|mtx|0.5.1 multi-command|ryu|1.6.0 errorlens|use|3.7.0 (1 theme extensions excluded)
A/B Experiments ``` vsliv368cf:30146710 vsreu685:30147344 python383cf:30185419 vspor879:30202332 vspor708:30202333 vspor363:30204092 vswsl492:30256859 vslsvsres303:30308271 pythonvspyl392:30443607 vserr242:30382549 pythontb:30283811 vsjup518:30340749 pythonptprofiler:30281270 vsdfh931cf:30280410 vshan820:30294714 vstes263:30335439 vscorecescf:30445987 pythondataviewer:30285071 vscod805:30301674 binariesv615:30325510 bridge0708:30335490 bridge0723:30353136 cmake_vspar411:30581797 vsaa593cf:30376535 pythonvs932:30410667 cppdebug:30492333 vsclangdf:30486550 c4g48928:30535728 dsvsc012cf:30540253 pynewvextcfv2:30660300 azure-dev_surveyone:30548225 pyindex848:30577860 nodejswelcome1:30587005 3biah626:30602489 pyind779cf:30657577 f6dab269:30613381 pythonsymbol12:30657548 ```
vscodenpa commented 1 year ago

Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.75.0. Please try upgrading to the latest version and checking whether this issue remains.

Happy Coding!

birgersp commented 1 year ago

Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.75.0. Please try upgrading to the latest version and checking whether this issue remains.

Happy Coding!

No thank you, I'm waiting for 1.75.1. The issue also exists in newer versions of code.

mjbvz commented 1 year ago

Have you tried setting "javascript.preferences.importModuleSpecifierEnding": "js"?

birgersp commented 1 year ago

I just tried it (them) now. This is a snippet from my settings.json:

    "typescript.preferences.importModuleSpecifierEnding": "js",
    "javascript.preferences.importModuleSpecifierEnding": "js",
    "typescript.updateImportsOnFileMove.enabled": "always",

(Idk if the last line is relevant but I figured it might be)

The result is the same. Also tried restarting VSCode to be sure that the settings are applied.

I suspect these two settings are not considered when doing the refactor, only when doing "auto-import".

birgersp commented 1 year ago

Idk when this was fixed, but in VSCode 1.77.3 this is no longer an issue

Thank you!