Skip to content

"Add all missing imports" adds them all in one line #38082

Closed

Description

@mjbvz

TypeScript Version: 3.9.0-dev.20200420

Search Terms:

  • fix all
  • code action
  • quick fix

Code
With @types/node installed, open a file:

join();
homedir()
  1. Now trigger quick fixes and select add all missing imports

Bug:
The two imports are added on the same line:

import { join } from "path";import { homedir } from "os";

join();
homedir()

Here's the edit that TS Server returns in this case

[Trace  - 23:52:04.985] <semantic> Response received: getCombinedCodeFix (72). Request took 13 ms. Success: true 
Result: {
    "changes": [
        {
            "fileName": "/Users/matb/projects/san/test.ts",
            "textChanges": [
                {
                    "start": {
                        "line": 1,
                        "offset": 1
                    },
                    "end": {
                        "line": 1,
                        "offset": 1
                    },
                    "newText": "import { join } from \"path\";"
                },
                {
                    "start": {
                        "line": 1,
                        "offset": 1
                    },
                    "end": {
                        "line": 1,
                        "offset": 1
                    },
                    "newText": "import { homedir } from \"os\";\n"
                }
            ]
        }
    ]
}

Activity

  1. DanielRosenwasser commented on Apr 21, 2020

    @DanielRosenwasser
    Member

    Likely a culprit of the new "preserved trivia" strategy - generated nodes don't have trivia, so we're not printing out new lines between statements. See some instances of this in #37814

  2. andrewbranch commented on Apr 21, 2020

    @andrewbranch
    Member

    It would be helpful if I could get a review on #37846

  3. DanielRosenwasser commented on Apr 21, 2020

    @DanielRosenwasser
    Member

    I gave a reivew, but if I understand correctly I think that only solves problems for gathering newlines between nodes more appropriately - it doesn't fix the problem for nodes that have no newlines because they're generated.

  4. andrewbranch commented on Apr 21, 2020

    @andrewbranch
    Member

    I think you’re probably right, but it will be useful nonetheless to avoid making simultaneous PRs to the same area. Thanks!

  5. andrewbranch commented on Apr 21, 2020

    @andrewbranch
    Member

    Matt Bierner (@mjbvz), it doesn’t seem to repro for me—also using that nightly and Insiders. Now that I think about it, we definitely already have test coverage for fix-all imports that should catch this. My screen cap shows semicolons on both calls, but I tried it with mixed and semicolon-less styles too. Any tricks you can think of to reproduce this?

    Kapture 2020-04-21 at 15 42 28

  6. mjbvz commented on Apr 21, 2020

    @mjbvz
    ContributorAuthor

    To repo for me, start with:

    import fs = require('fs')
    
    join();
    homedir()

    Delete the import. Then trigger the add all missing imports quick fix

  7. andrewbranch commented on Apr 22, 2020

    @andrewbranch
    Member

    Thanks! The trick was having a newline at the start of the file. This turned out not to be related to the newline preservation changes after all.

  8. 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

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions