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

Unwanted auto-imports in script files #55256

Open
HolgerJeromin opened this issue Aug 3, 2023 · 14 comments · Fixed by #55556
Open

Unwanted auto-imports in script files #55256

HolgerJeromin opened this issue Aug 3, 2023 · 14 comments · Fixed by #55556
Assignees
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Milestone

Comments

@HolgerJeromin
Copy link
Contributor

HolgerJeromin commented Aug 3, 2023

Bug Report

I have a mixture of module and non-module code.
The problem occurs when I edit non-module files (tsconfig: "module": "none").

Sometimes when using auto-completion (in VisualStudio 2022) typescript finds a "matching" name from the module world and adds a line like

import { HmiHelper } from './helper/HmiHelper.js';

into the first line of my file.
But as the current file was configured as "module": "none" this import pushes typescript into modules mode and the current code is invalid.

🔎 Search Terms

intellisense
auto-import
modules

🕗 Version & Regression Information

This bug is many years old and persists in Typescript 5.1.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Aug 3, 2023
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Aug 3, 2023

The title seems backwards; do you mean "with module: none" ?

@DanielRosenwasser DanielRosenwasser changed the title typescript should never auto-import into files without "module": "none" typescript should never auto-import into files with "module": "none" Aug 3, 2023
@hardikkoul
Copy link
Contributor

If this is still not resolved or assigned to anyone, may i have a go ?

@HolgerJeromin
Copy link
Contributor Author

HolgerJeromin commented Aug 8, 2023

@hardikkoul Just go for it:
https://github.com/Microsoft/TypeScript/blob/main/CONTRIBUTING.md#issue-claiming

Hmm, after rereading the link: This issue do not have a "help wanted" or "Backlog" tag...

@andrewbranch andrewbranch added this to the TypeScript 5.3.0 milestone Aug 8, 2023
@andrewbranch
Copy link
Member

@hardikkoul fine by me 👍 There’s a function shouldOfferImportCompletions or something like that in completions.ts.

@hardikkoul
Copy link
Contributor

@HolgerJeromin , @andrewbranch thanks , will start analyzing.

@hardikkoul
Copy link
Contributor

I can see there is a method programContainsModules , so its already checking for module config , but the catch is it only executes when none of the other conditions of shouldOfferImportCompletions return true. Correct me if my understanding is wrong.

@andrewbranch
Copy link
Member

You can just add a check that module isn’t none higher up, or change compilerOptionsIndicateEsModules (already a nonsense function, as my old TODO comment notes)

@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Aug 10, 2023
@hardikkoul
Copy link
Contributor

Let me know if it needs any revisions or is totally wrong (which is a possibility :P) , i opted not to touch the existing method just in case something breaks.

@hardikkoul
Copy link
Contributor

hardikkoul commented Aug 29, 2023

Sorry for the mess up while resolving conflicts @andrewbranch , i have closed and deleted the previous PR and branch , have created a new branch and raised a PR without conflicts, please review
#55556

@HolgerJeromin
Copy link
Contributor Author

cc @andrewbranch

Moving the discussion from the merged PR to this issue. Please reopen because the PR does not change behaviour.

ref #55556 (comment)

I do not understand. With typescript ts5.2.2 I still get bogus, file breaking autosuggestions in my modules:none code

If you accept the import, do you get an error?

Yes. My code is something like this non-module file with namespaces and a 3.7 MByte outfile:

namespace TcHmi.Config {
    export function get() {
        return TcHmi.System.config;  // <-- Defined in some other file 
    }
}

If I accept the auto-import
image

I will get this code:

import { parse } from "acorn";

namespace TcHmi.Config {
    export function get() {
        parse
        return TcHmi.System.config;  // <-- Defined in some other file 
    }
}

Typescript handles this code as a module and correctly has not idea what TcHmi.System should be.

I have "include": ["Lib/acorn.d.ts", ... in my tsconfig.json which this reduced content:

declare namespace acorn {  // I access this namespace
    /*...*/
    function parse(input: string, options?: Options): ESTree.Program;
}
declare module "acorn" {
    export = acorn
}

@andrewbranch
Copy link
Member

Ok, I see. The import is allowed, but turning your file into a module causes other errors. But that may be the case in any module mode, so it’s not really related to --module none. The truth is that --module none just doesn’t mean anything when --target is es2015 or higher, and I’m a little uncomfortable with forcing more meaning onto it than it has.

We could possibly consider preventing auto-imports when there is a top-level namespace declaration in a script that already merges with one in another file.

@HolgerJeromin
Copy link
Contributor Author

@andrewbranch
Could you please reopen this issue because the PR did not changed typescript code at all.
I still get this bogus suggestion with typescript 5.3.
Opening another issue feels not nice.

@andrewbranch andrewbranch reopened this Dec 7, 2023
@andrewbranch andrewbranch changed the title typescript should never auto-import into files with "module": "none" Unwanted auto-imports in script files Dec 7, 2023
@andrewbranch andrewbranch added Suggestion An idea for TypeScript Help Wanted You can do this Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experience Enhancement Noncontroversial enhancements and removed Bug A bug in TypeScript Fix Available A PR has been opened for this issue labels Dec 7, 2023
@andrewbranch andrewbranch modified the milestones: TypeScript 5.3.0, Backlog Dec 7, 2023
@andrewbranch
Copy link
Member

Accepting PRs for this:

We could possibly consider preventing auto-imports when there is a top-level namespace declaration in a script that already merges with one in another file.

@HolgerJeromin
Copy link
Contributor Author

HolgerJeromin commented Aug 24, 2024

Found #35395

Moving
typescript.preferences.autoImportFileExcludePatterns
from vscode settings to tsconfig setting would help visual studio 2022 users (like us) too with this very issue.
Edit: or create a vs setting for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Experience Enhancement Noncontroversial enhancements Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
5 participants