CodeVetter · Repo Unpack · Behavior traces
microsoft/TypeScript
Three flows were traced end to end. Batch compile: main -> runMain -> execute.CommandLine -> tscCompilation -> tsoptions parse/config discovery -> NewCachedFSCompilerHost -> compiler.NewProgram (processAllProgramFiles -> checker pool) -> EmitAndReportStatistics -> EmitFilesAndReportErrors (bind+check diagnostics, Emit) -> report + exit status. LSP: runLSP -> flag parse -> bundled.WrapFS(osvfs.FS()) -> lsp.NewServer -> Server.Run -> read/dispatch loop -> handlerMap -> project.Session snapshots -> ls.LanguageService. API: runAPI -> StdioServer.Run -> ipc transport accept -> StandaloneSession -> proto method table dispatch. A fourth flow covers watch/incremental rebuilds and a fifth covers VS Code extension activation.
- Startup (compile): main() -> runMain() -> core.ApplyDebugStackLimit() -> osutil.Args() -> execute.CommandLine(ctx, newSystem(), args, nil) -> os.Exit(result.Status); newSystem builds osSys with cwd, bundled.WrapFS(osvfs.FS()), bundled.LibPath().
- Compile flow order: CommandLine -> tscCompilation -> report early errors -> handle --init/--version/--help -> resolve configFileName (-p or findConfigFile ancestor walk) -> tsoptions.GetParsedCommandLineOfConfigFile -> CreateReportErrorSummary -> branch to watcher.start / performIncrementalCompilation / performCompilation.
- performCompilation: NewContentMapperHost (gated on RunExternalCode) -> compiler.NewCachedFSCompilerHost -> startTracingIfNeeded -> compiler.NewProgram -> tsc.EmitAndReportStatistics -> stopTracing -> CommandLineResult.
- Inside NewProgram: processAllProgramFiles parses and resolves all root+dependency files (parse tasks -> fileLoader -> module.Resolver), then initCheckerPool, verifyCompilerOptions, collectContentMapperOptionDiagnostics.
- Emit path: EmitAndReportStatistics -> EmitFilesAndReportErrors -> GetDiagnosticsOfAnyProgram(ctx, programLike, ...) with per-file GetBindDiagnostics then GetSemanticDiagnostics -> programLike.Emit(EmitOptions{WriteFile}) -> SortAndDeduplicateDiagnostics -> ReportDiagnostic each -> listFiles -> ReportErrorSummary -> exit-status mapping (EmitSkipped+diagnostics => DiagnosticsPresent_OutputsSkipped).
- Incremental compile: performIncrementalCompilation reads oldProgram via incremental.ReadBuildInfoProgram(host) before parsing, then incremental.NewProgram(program, oldProgram, CreateHost(host), sys.Now, testing) computes the change set and EmitAndReportStatistics runs against the incremental program.
- LSP startup: `tsc --lsp --stdio` -> runLSP -> flag.Parse (--pprofDir, --clientProcessId; --pipe/--socket parsed but rejected, only stdio supported) -> bundled.WrapFS(osvfs.FS()), osvfs.GetGlobalTypingsCacheLocation() -> lsp.NewServer(ServerOptions{In,Out,Err,Cwd,FS,NpmInstall,Spawn,...}) -> s.Run(ctx).
- LSP request handling: Server.Run reads messages into requestQueue; handleRequestOrNotification consults handlers()[req.Method]; document requests are wrapped by registerLanguageServiceDocumentRequestHandler which resolves session.GetLanguageService(ctx, params.TextDocumentURI()) and returns async work that runs the handler, then EnqueuePublishGlobalDiagnostics.
- LSP lifecycle: initialize -> handleInitialize creates the project.Session (s.session set; requests before initialize get ErrorCodeServerNotInitialized via the register wrappers); shutdown -> handleShutdown; exit -> handleExit; a parent-process watchdog goroutine polls isProcessAlive(parentPID) every 5s and cancels the server context if the editor dies.
- API session startup: `tsc --api` -> runAPI -> parseAPIFlags (--cwd, --pipe, --callbacks, --async, --timing, --runExternalCode) -> api.NewStdioServer -> Run: create pipe or stdio transport -> bundled.WrapFS(osvfs.FS()) optionally wrapped by callbackFS -> project.SessionInit -> NewStandaloneSession -> transport.Accept -> protocol (msgpack sync or JSON-RPC async) -> conn serve loop.
- Watch flow: tscCompilation sees Watch.IsTrue() -> createWatcher(sys, config, commandLineOptions, raw, reporters, testing) -> watcher.start(ctx) -> returns CommandLineResult{Watcher: watcher}; watching is implemented over watchmanager which multiplexes fswatch backends.
- VS Code flow: extension activate() -> SessionManager.start -> getExe(context) resolves the tsgo binary -> client.start(exe) builds ServerOptions running `[exe, --lsp, ...pprofArgs]` with stdio transport -> NativePreviewLanguageClient (vscode-languageclient) connects; setContext 'typescript.native-preview.serverRunning' toggles command enablement.
- Program reuse on edit: Project updates call Program.UpdateProgram/ReuseProgram, which replaces only the changed SourceFile when safe and bails to a full NewProgram on redirect files, helper/JSX-runtime import requirements, or changed supplemental (content-mapped) files.