Title: Auto-import in TypeScript suggests one outlandish choice, doesn't list reasonable ones · Issue #43352 · microsoft/TypeScript · GitHub
Open Graph Title: Auto-import in TypeScript suggests one outlandish choice, doesn't list reasonable ones · Issue #43352 · microsoft/TypeScript
X Title: Auto-import in TypeScript suggests one outlandish choice, doesn't list reasonable ones · Issue #43352 · microsoft/TypeScript
Description: VS Code Version: 1.54.3 OS Version: macOS 10.14.6 Does this issue occur when all extensions are disabled?: Yes I have a monorepo with a directory structure like so: @myscope/ foo/ package.json node_modules/ ... MyClass.ts index.ts <- exp...
Open Graph Description: VS Code Version: 1.54.3 OS Version: macOS 10.14.6 Does this issue occur when all extensions are disabled?: Yes I have a monorepo with a directory structure like so: @myscope/ foo/ package.json node...
X Description: VS Code Version: 1.54.3 OS Version: macOS 10.14.6 Does this issue occur when all extensions are disabled?: Yes I have a monorepo with a directory structure like so: @myscope/ foo/ package.json node...
Opengraph URL: https://github.com/microsoft/TypeScript/issues/43352
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Auto-import in TypeScript suggests one outlandish choice, doesn't list reasonable ones","articleBody":"\u003c!-- ⚠️⚠️ Do Not Delete This! bug_report_template ⚠️⚠️ -->\r\n\u003c!-- Please read our Rules of Conduct: https://opensource.microsoft.com/codeofconduct/ -->\r\n\u003c!-- 🕮 Read our guide about submitting issues: https://github.com/microsoft/vscode/wiki/Submitting-Bugs-and-Suggestions -->\r\n\u003c!-- 🔎 Search existing issues to avoid creating duplicates. -->\r\n\u003c!-- 🧪 Test using the latest Insiders build to see if your issue has already been fixed: https://code.visualstudio.com/insiders/ -->\r\n\u003c!-- 💡 Instead of creating your report here, use 'Report Issue' from the 'Help' menu in VS Code to pre-fill useful information. -->\r\n- VS Code Version: 1.54.3\r\n- OS Version: macOS 10.14.6\r\n\r\n\r\n\u003c!-- 🔧 Launch with `code --disable-extensions` to check. -->\r\nDoes this issue occur when all extensions are disabled?: Yes\r\n\r\nI have a monorepo with a directory structure like so:\r\n\r\n```\r\n@myscope/\r\n foo/\r\n package.json\r\n node_modules/\r\n ...\r\n MyClass.ts\r\n index.ts \u003c- exports MyClass\r\n bar/\r\n package.json \u003c- depends on @myscope/foo\r\n node_modules/\r\n ...\r\n index.ts \u003c- re-exports MyClass\r\n bar-demo/\r\n package.json \u003c- depends on @myscope/bar\r\n node_modules/\r\n ...\r\n baz/\r\n package.json \u003c- depends on @myscope/foo\r\n node_modules/\r\n ...\r\n currentFile.ts\r\n```\r\n\r\nNow, in a sane world, the sort of tough problem faced by VS Code would be how to prioritize the different reasonable ways to import `MyClass`. Suppose a package (not shown) depended on *both* `@myscope/foo` and `@myscope/bar`, then there would be *three* possible ways to import it:\r\n\r\n```\r\nimport { MyClass } from \"@myscope/foo\";\r\nimport { MyClass } from \"@myscope/foo/MyClass\";\r\nimport { MyClass } from \"@myscope/bar\";\r\n```\r\n\r\nI would be coming here asking, for example, how to de-prioritize the second one, because I'm using `index.ts` files with barrel exports.\r\n\r\nHowever, that is not the world we live in, because when I am in `currentFile.ts` (above), and I type `MyClass`, I might be offered *one* choice like:\r\n\r\n```\r\nMyClass (@myscope/foo/MyClass) > Auto import from '../foo-demo/node_modules/@myscope/bar'\r\n```\r\n\r\n...with no other choices, when the correct and only reasonable place to auto-import from is (simply) `'@myscope/foo'`.\r\n\r\nThis is just the most bizarre thing.\r\n\r\n* Why would a convoluted path involving `..` and descending into `node_modules` ever be given priority over a simple one?\r\n* Why isn't the \"correct\" module path (or any others) at least listed alongside the weird one as an option?\r\n* Why does the gray text next to MyClass (rendered in parentheses in the snippet above) disagree with the part that says \"Auto import from...\", with one referring to `@myscope/foo` and the other referring to `@myscope/bar` inside the `foo-demo` package?\r\n\r\nBefore VS Code, I used Emacs, for over a decade, and the auto-import features I encountered there were similarly terrible. I've never had a good auto-import experience, at least in JS/TS. But, VS Code's feature does save me some typing, even if I have to constantly correct it, and I have hope it could one day serve me well.\r\n\r\nCan extensions improve this experience? I have looked through the extension API a bit, but I can't find anything about influencing auto-import suggestions, even though there are apparently extensions that do this, including a popular one called Auto Import (which doesn't explain how it differs from or interacts with VS Code's built-in feature that does the same thing).\r\n\r\nI would like to know what auto-import is expected to be able to do, or take into account, and what it simply doesn't. I feel like there is some design limitation here, like maybe the different ways of importing MyClass are considered \"the same\" because they ultimately come from the same original declaration? That doesn't explain why the single path chosen among many is such a bad one, though.\r\n\r\nIf I were sitting down to write an auto-import feature from scratch that needed to come up with good suggestions for how to import a symbol, I would take factors into account like:\r\n\r\n* Is the symbol exported by a module that is already being imported from in this file?\r\n* How is this symbol usually imported in this workspace?\r\n* What is available in the nearest node_modules?\r\n\r\nThe first one seems like a no-brainer. The second one might require architectural changes or something, but an auto-import feature that *only* looked at existing import statements as source material might actually be more useful and accurate a lot of the time than what the current system is doing.\r\n\r\nWhen this problem isn't occurring, I actually do encounter the \"in a sane world\" problem mentioned above, where VS Code will not see and choose `@myscope/foo` over `@myscope/foo/MyClass` even though the former is shorter, even with 'Import Module Specifier' set to 'shortest.'","author":{"url":"https://github.com/dgreensp","@type":"Person","name":"dgreensp"},"datePublished":"2021-03-23T15:38:08Z","url":"https://github.com/microsoft/TypeScript/issues/43352"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0b393b4c-49bd-d252-ce48-15eb7e4d2ba4 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8E18:296654:991A0B:CEDD04:6B00E99E |
| html-safe-nonce | 5c6888980791efdc0067b0792790b8fa573fac266926e4b6c989753cf7dd853c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTE4OjI5NjY1NDo5OTFBMEI6Q0VERDA0OjZCMDBFOTlFIiwidmlzaXRvcl9pZCI6Ijg0MzEzOTMzNzUwNzYyODA3MzQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8b8b586f48acbf6d269e3c47e606ac8be5320c60a5457f05d295cbce8864144c |
| hovercard-subject-tag | issue:839120571 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/microsoft/TypeScript/43352/issue_layout |
| twitter:image | https://opengraph.githubassets.com/606830d9a1ba009da96b3e838a28c1e236d0cf56dcc23b21d82ed08512ebafbe/microsoft/TypeScript/issues/43352 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/606830d9a1ba009da96b3e838a28c1e236d0cf56dcc23b21d82ed08512ebafbe/microsoft/TypeScript/issues/43352 |
| og:image:alt | VS Code Version: 1.54.3 OS Version: macOS 10.14.6 Does this issue occur when all extensions are disabled?: Yes I have a monorepo with a directory structure like so: @myscope/ foo/ package.json node... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dgreensp |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| go-import | github.com/microsoft/TypeScript git https://github.com/microsoft/TypeScript.git |
| octolytics-dimension-user_id | 6154722 |
| octolytics-dimension-user_login | microsoft |
| octolytics-dimension-repository_id | 20929025 |
| octolytics-dimension-repository_nwo | microsoft/TypeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 20929025 |
| octolytics-dimension-repository_network_root_nwo | microsoft/TypeScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width