Skip to content

source.organizeImports affects export object #61588

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

Open
Zitrooone opened this issue Apr 16, 2025 · 4 comments
Open

source.organizeImports affects export object #61588

Zitrooone opened this issue Apr 16, 2025 · 4 comments
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@Zitrooone
Copy link

Zitrooone commented Apr 16, 2025

Does this issue occur when all extensions are disabled?: Yes/No
Yes

  • VS Code Version: 1.99.2 (user setup)
  • OS Version: Windows_NT x64 10.0.26100

Steps to Reproduce:

With "source.OrganizeImports" = "explicit" exports of a TypeScript file are also affected.
I set up an example that looks like this:

import sample1 from './sampleImports/sample1'
import sample10 from './sampleImports/sample10'
import sample11 from './sampleImports/sample11'
import sample12 from './sampleImports/sample12'
import sample13 from './sampleImports/sample13'
import sample2 from './sampleImports/sample2'
import sample3 from './sampleImports/sample3'
import sample4 from './sampleImports/sample4'
import sample5 from './sampleImports/sample5'
import sample6 from './sampleImports/sample6'
import sample7 from './sampleImports/sample7'
import sample8 from './sampleImports/sample8'
import sample9 from './sampleImports/sample9'

export {
    sample1,
    sample2,
    sample3,
    sample4,
    sample5,
    sample6,
    sample7,
    sample8,
    sample9,
    sample10,
    sample11,
    sample12,
    sample13,
}

When I add a newline and save the file it results in this:

import sample1 from './sampleImports/sample1'
import sample10 from './sampleImports/sample10'
import sample11 from './sampleImports/sample11'
import sample12 from './sampleImports/sample12'
import sample13 from './sampleImports/sample13'
import sample2 from './sampleImports/sample2'
import sample3 from './sampleImports/sample3'
import sample4 from './sampleImports/sample4'
import sample5 from './sampleImports/sample5'
import sample6 from './sampleImports/sample6'
import sample7 from './sampleImports/sample7'
import sample8 from './sampleImports/sample8'
import sample9 from './sampleImports/sample9'


export {
    sample1, sample10,
    sample11,
    sample12,
    sample13, sample2,
    sample3,
    sample4,
    sample5,
    sample6,
    sample7,
    sample8,
    sample9
}

I expect the order of the exports to be untouched.
We are using an EsLint Plugin to sort the exports alphabetically (which is not happy with the result of the import organization), but the issue stems from the setting. Launching VS Code without any extension produces the same result.
The example can be found here: https://github.com/Zitrooone/vscode-organizeImports-test

@mjbvz mjbvz transferred this issue from microsoft/vscode Apr 16, 2025
@mjbvz mjbvz removed their assignment Apr 16, 2025
@RyanCavanaugh
Copy link
Member

Confirmed. No setting change required here, just need to invoke the "Organize Imports" command from the palette.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this labels Apr 17, 2025
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Apr 17, 2025
@idango10
Copy link
Contributor

idango10 commented May 7, 2025

@RyanCavanaugh I noticed the behavior seems tied to the default settings for organizeImportsCollation ("ordinal") and organizeImportsNumericCollation (false). Given those defaults, it looks like the current sorting is working as designed (reference: #52115).

That said, the issue has a Help Wanted label, were you thinking of potentially changing those defaults?

@jakebailey
Copy link
Member

Initially when I looked at this, I thought that the problem is that the detection code isn't working (which applies when the user hasn't set anything explicitly), however rereading this, the two different blocks have two different sortings. The imports are not sorted naturally, but the exports are sorted naturally. So no matter what we're doing, we're going to resort one of the blocks.

I can't say I like the edit that was chosen here, but, is there anything actually wrong here outside the suboptimal columns?

@Zitrooone
Copy link
Author

I am uncertain whether @jakebailey 's question was directed at me, but I would like to offer my two cents on this regardless.

Choosing sample 1 through 13 for my example was not ideal, I agree. However, I am not taking issue in the order of the keys, but with the way they are put on the same line in some instances. I would like to add, that this only happens once a certain number of keys has been reached.

Through the issue mentioned by @idango10 I learned that the organizeImports setting is in fact supposed to affect the export as well. I see how the two are related, though the naming of the setting specifically mentions "imports" and thus I was surprised by the actual behaviour. Paired with the fact that I was not able to find any official documentation for this setting and its various sub-settings, I do think this is misleading and could likely confuse other users as well.

To me it would be sufficient if keys remained in a one-per-line configuration, as this is important for readability in this scenario.

Would renaming the settings or splitting import and export sorting be plausible to avoid confusion and/or conflicts with other import/export sorting?

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

No branches or pull requests

5 participants