Skip to content

Report every instance of TS1208 (global scripts not allowed under isolatedModules) #49995

Closed
@wnayes

Description

@wnayes

Bug Report

When more than one global script file is present within a project using isolatedModules, only one error is raised for one of the problematic files.

File(1,1): error TS1208: 'File.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file.
Add an import, export, or an empty 'export {}' statement to make it a module.

// No additional errors for File2.ts etc.

This behavior seems different from other errors that are logged when isolatedModules is enabled. It hides the extent to which a project is incompatible with isolatedModules.

🕗 Version & Regression Information

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

💻 Code

The behavior appears intentional in program.ts. It finds the first global script file and emits one diagnostic.

const firstNonExternalModuleSourceFile = find(files, f => !isExternalModule(f) && !isSourceFileJS(f) && !f.isDeclarationFile && f.scriptKind !== ScriptKind.JSON);
if (firstNonExternalModuleSourceFile) {
const span = getErrorSpanForNode(firstNonExternalModuleSourceFile, firstNonExternalModuleSourceFile);
programDiagnostics.add(createFileDiagnostic(firstNonExternalModuleSourceFile, span.start, span.length,
Diagnostics._0_cannot_be_compiled_under_isolatedModules_because_it_is_considered_a_global_script_file_Add_an_import_export_or_an_empty_export_statement_to_make_it_a_module, getBaseFileName(firstNonExternalModuleSourceFile.fileName)));
}

🙁 Actual behavior

One error of this kind is logged per compile.

🙂 Expected behavior

Log all of the violations, so that the extent of the problem can be assessed easily.

I changed the logic in program.ts locally to be a for loop that logged each problematic file, and I found this helpful for assessing the effort required to implement isolatedModules compatibility in an existing codebase.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions