Sub topic 12
Go to definition and editor behavior across projects
This chapter assumes you have the two-project setup from earlier chapters: a core project and an app project that lists core in references, plus a solution-style root tsconfig.json. It also assumes you use an editor that talks to TypeScript's language server, such as VS Code. Here you will learn what the editor does with that setup and how to fix the usual surprises.
What the editor actually runs
Editors do not run tsc --build. They start tsserver, the TypeScript language server, which answers questions such as "where is this symbol defined?" and "what errors does this file have?". The server keeps one in-memory project per tsconfig.json it needs. For each file you open, it must decide which project owns that file.
The decision follows these steps:
- Starting in the file's directory, find the nearest
tsconfig.json. - If that config includes the file, that config is the owning project.
- If the config is a solution-style config (
"files": []plusreferences), look through its referenced projects for one that includes the file. - If no project includes the file, put it in an inferred project with default compiler options.
Step 4 explains a common complaint. A file that no include pattern matches gets no strict mode and none of your paths, and it may show odd errors. The files are fine. They are just outside every project.
Source redirect: why edits show up without a rebuild
When app imports from core, a command-line tsc --build reads core's emitted .d.ts files. The editor behaves differently by default. Since TypeScript 3.7, tsserver uses a feature called source of project reference redirect. When an import would resolve to a referenced project's output declaration file, such as packages/core/dist/greet.d.ts, the server substitutes the matching source file, packages/core/src/greet.ts.
This has three practical effects:
- Go to Definition on
greetinmain.tsjumps togreet.ts, not to a generated.d.tsfile. - Edits propagate immediately. If you rename a parameter or change a return type in
core,appshows the new type at once. You do not need to runtsc --buildfirst. distdoes not have to exist. On a fresh clone you can open the repository and get working navigation before anything has been built.
The server still treats the projects as separate. Each one keeps its own options, so a strict setting in core does not leak into app.
When you land in a `.d.ts` file anyway
You can still end up in generated declarations. This happens when the import resolves through node_modules, for example a workspace package linked by npm, pnpm, or yarn. The node_modules/@acme/core link points at a package whose types field names dist/index.d.ts. The server then sees a declaration file, and the redirect applies only if that file is the output of a project the current project references.
If the redirect does not apply, you get the .d.ts file. This is where declarationMap matters. With "declarationMap": true, the compiler writes a .d.ts.map file next to each declaration file, and the editor uses it to hop from the declaration to the original .ts line. It only works if three things hold:
- The
.d.ts.mapfiles were emitted. Note thatcompositeturns ondeclarationbut notdeclarationMap, so you must set the latter yourself. - The map's
sourcesentries can be resolved on disk, which means the source files exist where the map says. - The editor sees the current build output. A stale
distjumps to old line numbers.
A package that publishes to npm often leaves src out of the tarball. Go to Definition on the published version then stops at the .d.ts file. That is expected and not a bug in your setup.
The editor options for project references
Three compiler options change how tsserver treats references. They affect the editor only for the most part. Set them in the tsconfig.json of the project that you open files from.
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"declarationMap": true,
"rootDir": "src",
"outDir": "dist",
"disableSourceOfProjectReferenceRedirect": true,
"disableReferencedProjectLoad": true
},
"include": ["src"],
"references": [{ "path": "../core" }]
}| Option | Effect | When to use it |
|---|---|---|
| `disableSourceOfProjectReferenceRedirect` | The editor reads referenced projects' `.d.ts` output instead of their source. | Very large repositories where loading every referenced project's source makes the editor slow. |
| `disableReferencedProjectLoad` | The server does not load all referenced projects when it opens a project. It loads them only when needed. | Solution configs that list hundreds of projects. |
| `disableSolutionSearching` | The project is not considered when the server searches a solution config for a file's owner. | A project that should never be picked up as an owner, such as a generated-code project. |
Turning off the source redirect has a cost. The editor then depends on dist, so cross-project edits appear only after a rebuild, and a missing dist produces "cannot find module" errors in the editor. Pair it with tsc --build --watch in a terminal, so the declaration files stay current.
Find All References and Rename
Find All References and Rename Symbol work across project boundaries, but only for projects the server has loaded. With the source redirect on, the server loads the referenced projects it needs, so renaming greet in core also updates the call in app.
The server cannot see a project that is not connected to the one you are working in. Suppose a third project, tools, imports core but is not reachable through any references chain from your open file. If nothing has loaded tools, a rename of greet may skip it. The reliable fix is the solution-style root config, because it lists every project and lets the server find all of them. If you rename a widely used export, build afterwards with tsc --build and check that the other projects still compile.
Symptoms and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Go to Definition opens `dist/greet.d.ts` | `declarationMap` is off, or the import resolves through `node_modules` | Enable `declarationMap`, rebuild, and restart the server |
| Editor shows old types after a change in `core` | `disableSourceOfProjectReferenceRedirect` is on, or `dist` is stale | Run `tsc --build --watch`, or remove the option |
| Red errors on a file that builds fine | The file is in an inferred project | Add it to an `include`, or add a project for it, such as a test project |
| "Cannot find module" in the editor but `tsc --build` succeeds | `dist` is missing and the redirect is off, or a reference is missing | Add the `references` entry, or build once |
| Editor is slow in a large repository | All referenced projects are loaded | Set `disableReferencedProjectLoad` |
| Rename misses a usage in another package | That project was not loaded | Open a file from it, or rely on the root solution config |
Restarting the server and inspecting what it chose
The server caches its project decisions. After you change references, include, or composite, it may keep acting on the old state. In VS Code, open the command palette and run TypeScript: Restart TS Server. Two other commands help with diagnosis:
TypeScript: Go to Project Configurationopens thetsconfig.jsonthat owns the current file. If it opens the wrong one, or reports that none was found, you have found the problem.TypeScript: Open TS Server logwrites a log that lists the projects loaded and which config each file landed in. This needs thetypescript.tsserver.logsetting turned on first.
You can run the same check from the command line. tsc --showConfig -p packages/app prints the resolved options, and tsc --listFilesOnly -p packages/app lists every file the project includes. If your file is missing from the second list, the editor is right to treat it as outside the project.
# Which files does the app project own?
tsc --listFilesOnly -p packages/app
# What options does it actually resolve to?
tsc --showConfig -p packages/appExercise
In a two-project repository, app references core. You change a function signature in core/src/greet.ts and the editor immediately shows the new type in app, though you have not run tsc --build and core/dist is empty. Then you set disableSourceOfProjectReferenceRedirect: true in app's tsconfig and restart the server. What do you expect to see, and what do you need to do to get correct types again?
Show answer
With the option on, the editor reads core's declaration output instead of its source. Because core/dist is empty, app reports that it cannot find the module or its types. Run tsc --build (or keep tsc --build --watch running) so core emits its .d.ts files. After that the types are correct, and they update only when core is rebuilt. Go to Definition will also land in dist/greet.d.ts, and it will reach greet.ts only if declarationMap is enabled and the map files were emitted.