dsherret / dsherret/ts-morph

moduleDetection force compiler setting does not work

Open
#1,611 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Describe the bug**

Version: 25.0.0

**To Reproduce**

Create a simple project setting `moduleDetection` to `force` which should treat each source file as a module:

```ts
import { Project, ts } from 'ts-morph';

const project = new Project({
compilerOptions: {
module: ts.ModuleKind.ESNext,
moduleDetection: ts.ModuleDetectionKind.Force,
},
useInMemoryFileSystem: true,
});

project.createSourceFile('a.ts', `const a = 'a'`);
project.createSourceFile('b.ts', `const a = 'a'`);

project.getPreEmitDiagnostics().forEach((diagnostic) => {
console.log(diagnostic.getMessageText());
});
```

However, this will not work as expected and produce type errors. Here is a [StackBlitz Node.js project](https://stackblitz.com/edit/stackblitz-starters-esuaovhj?file=index.ts&view=editor) reproducing the issue along with using the TypeScript compiler directly which works as expected.

**Expected behavior**

The `moduleDetection` compiler setting should be respected and not produce diagnostics when set to `force`.

Contributor guide

Open the contributing guide

Research direction

Start with the Project setup using compilerOptions, createSourceFile, and getPreEmitDiagnostics shown in the reproduction, then compare its behavior with the direct TypeScript compiler example linked in the issue. Done means setting moduleDetection to Force prevents the duplicate top-level declaration diagnostics for the two in-memory files.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.