'Why is VS Code unable to find some references to my TypeScript code within the same workspace?

I've been experiencing issues with VS Code being unable to find references to my TypeScript modules and exports outside of my project's src directory.

For instance, our team keeps our test code under a __tests__ directory, relative to the project root, so our project structure looks something like this:

|-- src
|     |-- MyService.ts
|
|-- __tests__
|     |-- MyService.test.ts
|     |-- tsconfig.json
|
|-- tsconfig.json
|-- tsconfig.jest.json
|-- package.json

Taking the MyService.ts module as an example, if I try to find all references to the file, or try to find references to the MyService class that it exports, VS Code will initially fail to find the references that exist in MyService.test.ts. However, if I actually open the MyService.test.ts file in the IDE, then VS Code will be able to discover the references.

It seems like the background process/server that handles all of the TypeScript intellisense/magic for the IDE is not discovering the code under the __tests__ directory until I actually open it.

For what it's worth, I'm currently using VSCode 1.67.1 on an M1 macbook

I've created a git repo with a sample project that demonstrates this issue, here:

https://github.com/jsholzer/vscode-find-ref-issue

Just clone the repo, open the folder with VS Code, open the src/MyService.ts file and try to find or peek references to the MyService class. It should not find the reference in the test.

Lastly, here are the contents of each of the tsconfig files

  1. tsconfig.json
{
  "include": ["./src/**/*.ts"],
  "compilerOptions": {
    "outDir": "./dist",
    "rootDir": ".",
    "strict": false,
    "esModuleInterop": true,
    "module":"CommonJS",
    "moduleResolution": "node",
    "allowJs": true,
    "lib":["ESNext"],
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule":true,
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true,
    "target": "es2021",
    "skipLibCheck": true,
  }
}

  1. tsconfig.jest.json And our tsconfig for our tests (tsconfig.jest.json), looks like this
{
  "extends": "./tsconfig.json",
  "include": ["**/*.ts"],
  "exclude": ["node_modules", "dist"],
  "compilerOptions": {
    "paths":{
      "~/src/*": ["./src/*"]
    },
    "plugins": [
      {
        "transform": "ts-auto-mock/transformer",
        "cacheBetweenTests": false
      }
    ]
  },
}

  1. tests/tsconfig.json
{
  "extends": "../tsconfig.jest.json"
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source