Skip to content

Trying to add multiple alphabetically adjacent imports from the same module results in assertion fail #42527

@nmggithub

Description

@nmggithub

Bug Report

🔎 Search Terms

  • add all missing imports
  • debug failure
  • false expression
  • changes overlap

🕗 Version & Regression Information

This bug exists in v4.1.2 and v4.1.3 according to my testing.

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about imports.

⏯ Playground Link

This is a bug with the TypeScript Server itself. It cannot be shown in the Playground.

💻 Code

This bug can be replicated in VSCode by making two files:

exports.ts

export interface Test1 {}
export interface Test2 {}
export interface Test3 {}
export interface Test4 {}

importer.ts

import { Test1, Test4 } from './exports';
interface Testing {
    test1: Test1;
    test2: Test2;
    test3: Test3;
    test4: Test4;
}

🙁 Actual behavior

Expectedly, the server correctly reports that it cannot find Test2 or Test3. Attempting to use the Add all missing imports
feature results in no change to the file. If the Developer Tools are used, an error is printed, similar to the one mentioned in #40065. In fact, this is likely the root of the previous issue.

The server fails at this assert statement:

Debug.assert(normalized[i].range.end <= normalized[i + 1].range.pos, "Changes overlap", () =>
`${JSON.stringify(normalized[i].range)} and ${JSON.stringify(normalized[i + 1].range)}`);

(which appears to have been introduced by this commit: f4af74a)

The assert statement appears to be trying to catch edits that target the same area of text. Commenting out the assert and running this modified version of the TypeScript server fixes the issue and the imports are added correctly, showing that its use, at least in this context, is unnecessary. I am not sure though of the further ramifications of removing this assert, though.

I believe this bug happens in this case because the two imports, Test2 and Test3, are adjacent to each other alphabetically, and thus the server would be placing them both in the same spot, after Test1. It then gets confused and thinks it's trying to add conflicting edits and stops before it can do the deed. This behavior does not exist when trying to add imports for Test3 and Test4 (or for Test1 and Test2), which makes me believe that the two adjacent imports have to also be sandwiched between existing imports for this bug to happen.

TL;DR:
The server fails at adding multiple imports from the same module if, when added to the existing imports list,

  • Two or more of the imports to be added will be alphabetically adjacent to each other
  • One or more already-existing imports will be alphabetically before all of the imports to be added
  • One or more already-existing imports will be alphabetically after all of the imports to be added

🙂 Expected behavior

It is expected that the imports be added correctly, as they do when the assert statement is commented out.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions