Skip to content

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

Description

@hoodmane

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

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

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

package.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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions