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

Auto-imports from code actions and completions don't match order that "Organize imports" outputs #60492

Open
MariaSolOs opened this issue Nov 13, 2024 · 2 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@MariaSolOs
Copy link
Contributor

πŸ”Ž Search Terms

"auto-import", "order", "import", "organize imports"

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried.

⏯ Playground Link

No response

πŸ’» Code

In a simple npm package with just TypeScript 5.6.3 installed, create a TypeScript file with the following content:

import { foo } from "@foo";
import { bar } from "@bar";
import { a } from "workspace-a";
import { b } from "workspace-b";

console.log(foo + bar + a + b)

Now at the end of the file type is and select the completion isAccessor that brings the import from TypeScript.

πŸ™ Actual behavior

The import from the completion is added below import { b } from "workspace-b";, but notice how the import gets moved to the middle if one runs the "Organize imports" command in VS Code.

πŸ™‚ Expected behavior

For completions and code actions that add an import to use the same ordering logic that the "Organize imports" command uses. After all this seems to be advertised by #52115 ("These rules will also apply to auto-imports and import fixes, which will use the user's collation preferences to determine if a list of import or export specifiers is already sorted before selecting an insertion point").

Additional information about the issue

No response

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Nov 13, 2024
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.8.0 milestone Nov 13, 2024
@iisaduan
Copy link
Member

@MariaSolOs When you run organizeImports, does the order of the first two imports (the one from @foo and @bar) also flip? Seems like this case should have autoimports adding onto the end because the original imports are unsorted.

@MariaSolOs
Copy link
Contributor Author

MariaSolOs commented Nov 13, 2024

@MariaSolOs When you run organizeImports, does the order of the first two imports (the one from @foo and @bar) also flip? Seems like this case should have autoimports adding onto the end because the original imports are unsorted.

@iisaduan It does. And indeed, if I follow the repro steps above using this file:

import { bar } from "@bar";
import { foo } from "@foo";
import { a } from "workspace-a";
import { b } from "workspace-b";

console.log(foo + bar + a + b)

Then the bug doesn't happen.

HOWEVER it does happen with this file:

import { bar } from "@bar";
import { foo } from "@foo";
import { a } from "workspace-a";
import { b } from "workspace-b";
import { x } from "../file1";
import { y } from "./file2";

console.log(foo + bar + a + b + x + y);

Using the above content, organizeImports makes no changes. Now at the bottom of the file start typing is and select the completion for isAccessor. The import goes below the import of y, but then it's moved above a if one runs organizeImports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants