dsherret / dsherret/ts-morph

`project.resolveSourceFileDependencies()` behavior depends on whether files are added manually

Open
#1,653 0 comments 0 reactions 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: 27.0.0

If I don't manually add files to the project and rely on the `tsconfig.json` `"include"` key, `project.resolveSourceFileDependencies()` returns nothing. If I add the same files with a glob using `addSourceFilesAtPaths()`, `project.resolveSourceFileDependencies()` returns a lot of stuff.

### To Reproduce

```ts
import { Project } from "ts-morph";

function main() {
const project = new Project({
tsConfigFilePath: "../input-project/tsconfig.json",
libFolderPath:
"../input-project/node_modules/typescript/lib",
});
console.log("Automatic:")
console.log(project.getSourceFiles().map(x => x.getFilePath()));
console.log(project.resolveSourceFileDependencies().map(x => x.getFilePath()));

console.log("Manual:")
const sourceFile = "../input-project/*.ts";
console.log(project.addSourceFilesAtPaths(sourceFile).map(x => x.getFilePath()));
console.log(project.getSourceFiles().map(x => x.getFilePath()));
console.log(project.resolveSourceFileDependencies().map(x => x.getFilePath()));
return;
}
main();
```
And `../input-project` looks like:

#### `a.ts`

Empty file

#### `tsconfig.json`
```json
{
"compilerOptions": {
"target": "ESNext",
"noEmit": true,
"allowImportingTsExtensions": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"lib": ["es5"],
"types": []
},
"include": ["*.ts"]
}
```

#### `package.json`

```json
{
"dependencies": {
"typescript": "^5.3.2"
}
}
```

### To Trigger
First run `npm i` in the `input-project` folder, then run `npm i ts-morph` in the folder with the script, then run the script.

### Actual output
```
Automatic:
source files: [
'/path/to/project/input-project/a.ts'
]
deps: []
Manual:
added source files: [
'/path/to/project/input-project/a.ts'
]
source files: [
'/path/to/project/input-project/a.ts'
]
deps: [
'/path/to/project/input-project/node_modules/typescript/lib/lib.es5.d.ts',
'/path/to/project/input-project/node_modules/typescript/lib/lib.decorators.d.ts',
'/path/to/project/input-project/node_modules/typescript/lib/lib.decorators.legacy.d.ts'
]

```

### Expected behavior

Adding the same files specified in `tsconfig.json` again shouldn't change the output of `resolveSourceFileDependencies()`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.