Skip to content

Organize Import creates whitespace that needs to be deleted manually #40736

Description

TS Template added by Matt Bierner (@mjbvz)

TypeScript Version: 4.1.0-dev.20200918

Search Terms

  • organize imports

  • VSCode Version: 1.49.1 (user setup)
  • OS Version: Windows_NT x64 10.0.19041

I am not 100% sure what is the exact scenario how to reproduce this, but it happens 90% of the time. When I press ALT+SHIFT+O (Organize Imports), I get plenty of spacing in my imports.
The rules for organizing these imports seem to be all over the place, which one ends up on their own line? etc.
I always have to clean up manually.

These imports:

import {
    ChangeDetectionStrategy,
    Component,
    ContentChild,
    EventEmitter,
    Input,
    Output,
    TemplateRef,
    AfterViewInit,
    ChangeDetectorRef,
    HostBinding,
    ElementRef,
    ViewChild
} from '@angular/core';
import { Resource, ClickableResource } from '@models/resource';
import { newGuid } from '@utility/string';
import { DragulaService } from 'ng2-dragula';

Become:

import {
    AfterViewInit, ChangeDetectionStrategy,







    ChangeDetectorRef, Component,
    ContentChild,







    ElementRef, EventEmitter,





    HostBinding, Input,
    Output,
    TemplateRef,




    ViewChild
} from '@angular/core';
import { ClickableResource, Resource } from '@models/resource';
import { newGuid } from '@utility/string';
import { DragulaService } from 'ng2-dragula';

Video of what is happeining.
spacing-incorrect.zip

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

Activity

  1. mjbvz commented on Sep 23, 2020

    @mjbvz

    Can you share a small project that causes this. I can't reproduce it

  2. jsgoupil commented on Sep 24, 2020

    @jsgoupil
    Author

    Try this:

    import {
        ChangeDetectionStrategy,
        Component, ContentChild, EventEmitter, Input, Output, TemplateRef,
        AfterViewInit,
        ChangeDetectorRef,
        HostBinding,
        ElementRef,
        ViewChild
    } from '@angular/core';
    import { Resource, ClickableResource } from '@models/resource';
    import { newGuid } from '@utility/string';
    import { DragulaService } from 'ng2-dragula';
    
    interface A extends ContentChild {}
    interface B extends EventEmitter {}
    interface C extends Input {}
    interface D extends Output {}
    interface E extends TemplateRef {}
    interface F extends AfterViewInit {}
    interface G extends ChangeDetectorRef {}
    interface H extends HostBinding {}
    interface I extends ElementRef {}
    interface J extends Resource {}
    interface L extends ClickableResource {}
    
    @Component({
        selector: 'wm-table',
        templateUrl: 'table.component.html',
        styleUrls: ['table.component.scss'],
        providers: [DragulaService],
        changeDetection: ChangeDetectionStrategy.OnPush
    })
    export class TableComponent implements AfterViewInit {
    }
    
  3. removed their assignment
    on Sep 24, 2020
  4. mjbvz commented on Sep 24, 2020

    @mjbvz

    Thanks Jean-Sébastien Goupil (@jsgoupil)!

    Daniel Rosenwasser (@DanielRosenwasser) Can we make sure this is scheduled for TS 4.1? (I have not see other reports so far, so I don't think we want to try fixing it in a 4.0 recovery build)

  5. jsgoupil commented on Sep 24, 2020

    @jsgoupil
    Author

    Maybe there should be something a little more predictable when it comes to either putting them all the imports on one line or putting them all on different lines? It seems quite random. Also, make sure to check how many lines end up under the last import and in order to stay consistent.

    It had been happening forever, but I thought it would just get fixed one day :) Now that I'm doing major refactoring, this is happening a lot.

  6. andrewbranch commented on Oct 2, 2020

    @andrewbranch
    Member

    This bug is almost definitely caused by #36688. I’m not sure off the top of my head what the best fix is, but that’s the first place to look. Eli Barzilay (@elibarzilay), I can take this one if you want. /cc also Oleksandr Tarasiuk (@a-tarasyuk).

  7. elibarzilay commented on Feb 13, 2021

    @elibarzilay
    Contributor

    Andrew Branch (@andrewbranch): I looked at this, and at the closed #40895, and I'm confused. It looks like #36688 is about preserving trivia(?) (comments & newlines, and maybe whitespace?) but I don't see how this would make sense when reordering things:

    • Wouldn't this get very messed up when there's an import line that looks like

      Foo, // blah blah

      since the comment will get dragged along with the next import (so will appear on whatever happens to be before it, "doubly shuffled")?

    • If the source has empty lines to separate parts of the imports, then those would also move in unexpected ways; even worse with comment lines to separate such parts.

    Given this, I'd expect the reasonable option to be dropping all the extras, and maybe at most do the minor thing of putting newlines in the new text if the original had any newlines, but nothing else.

    Clearly there's some brokenness in these cases with the extra empty lines, and I'm guessing that the #36688 doesn't account for lines being shuffled which leads to it, but even if this didn't happen, I don't see how the fixed thing would make sense given the above.

  8. andrewbranch commented on Feb 16, 2021

    @andrewbranch
    Member

    This should be fixed by #42630.

  9. andrewbranch commented on Feb 16, 2021

    @andrewbranch
    Member

    Looks like #41417 should have been marked as an exact duplicate of this, in fact.

  10. elibarzilay commented on Feb 23, 2021

    @elibarzilay
    Contributor

    Closing this, since as Andrew Branch (@andrewbranch) notes, it's the same as #41417, and I also verified that #42630 resolves this one.

  11. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugA bug in TypeScriptDomain: FormatterThe issue relates to the built-in formatterDomain: LS: Organize ImportsIssues with the organize imports featureEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Fix AvailableA PR has been opened for this issueHelp WantedYou can do thisRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions