microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.08k stars 12.37k forks source link

Web TS Server tries watching for files outside of project root #59413

Open mjbvz opened 1 month ago

mjbvz commented 1 month ago

Acknowledgement

Comment

From https://github.com/microsoft/vscode/issues/221299

On vscode.dev, enabling project wide intellisense with TS 5.5 can cause exceptions in the developer console. Both of these are related to trying to watch/read files that are outside of the project root. We block these as these reads are invalid on web and in some cases can actually cause major perf issues

I believe there are two separate exceptions:

  1. Trying to watch for @typescript/lib-*:
Error: <syntax> TypeScript Server Error (5.5.2)
Could not read file outside of project root /vscode-vfs/github/mjbvz/node_modules/@typescript/lib-es5
Error: Could not read file outside of project root /vscode-vfs/github/mjbvz/node_modules/@typescript/lib-es5
    at PathMapper.toResource (tsserver.web.js:193032:19)
    at Object.realpath (tsserver.web.js:193494:26)
    at createDirectoryWatcherForPackageDir (tsserver.web.js:129937:40)
    at setDirectoryWatcher (tsserver.web.js:129981:7)
    at watchFailedLookupLocation (tsserver.web.js:129814:9)
    at watchFailedLookupLocationOfResolution (tsserver.web.js:129827:21)
    at watchFailedLookupLocationsOfExternalModuleResolutions (tsserver.web.js:129783:9)
    at Object.resolveLibrary2 [as resolveLibrary] (tsserver.web.js:129722:7)
    at InferredProject2.resolveLibrary (tsserver.web.js:182661:33)
    at pathForLibFileWorker (tsserver.web.js:126248:24)
    at TypeScriptServerError.create (extension.js:35385:16)
    at SingleTsServer.dispatchResponse (extension.js:34850:66)
    at SingleTsServer.dispatchMessage (extension.js:34781:30)
    at eval (extension.js:34753:18)
    at WorkerServerProcess._tsserver.onmessage (extension.js:45502:17)

In this case, my workspace is /vscode-vfs/github/mjbvz/vscode-markdown-footnotes. We should not try reading anything in a parent directory of this, such as /vscode-vfs/github/mjbvz/node_modules

I've caught the exception as a workaround as otherwise this actually ends up being a fatal error for VS Code

  1. After fixing the above, I still see normal watchdirectory requests for watching /vscode-vfs/github/mjbvz/node_modules and /vscode-vfs/github/mjbvz/node_modules/@types
Error: Could not read file outside of project root /vscode-vfs/github/mjbvz/node_modules
    at PathMapper.toResource (tsserver.web.js:193032:19)
    at FileWatcherManager.watchDirectory (tsserver.web.js:192930:35)
    at watchDirectory (tsserver.web.js:123288:67)
    at tsserver.web.js:123356:92
    at Object.watchDirectory (tsserver.web.js:123306:283)
    at ConfiguredProject2.watchDirectoryOfFailedLookupLocation (tsserver.web.js:182679:45)
    at createDirectoryWatcher (tsserver.web.js:130070:27)
    at createOrAddRefToDirectoryWatchOfFailedLookups (tsserver.web.js:129990:76)
    at createDirPathToWatcher (tsserver.web.js:129974:116)
    at createDirectoryWatcherForPackageDir (tsserver.web.js:129968:18)

If possible, we should never try accessing any files outside of the project root on web. In the updateOpen calls, we pass in a projectRootPath to try to prevent this

sheetalkamat commented 1 month ago

Though project root is associated with open file, projects arent just related to single open file. They are independently opened and kept alive even after the said open file is closed. (as we might get another open request - may be with different projectRoot). The projects never use projectRootPath from open files for their updates/status for that reason.

Having said that, the watchFile and watchDirectory implementation of web part of tsserver can easily account for things its not interested in and return noop watcher so i dont think we need to change anything in the tsserver project part as such