{"record":{"id":"922c4d15cb15832c","repo":"microsoft/typescript-go","slug":"stdioserveroptions-cwd-is-required","errorCode":null,"errorMessage":"StdioServerOptions.Cwd is required","messagePattern":"StdioServerOptions\\.Cwd is required","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"internal/api/server.go","lineNumber":48,"sourceCode":"\t// CollectTiming enables per-request server processing-time measurement.\n\t// When enabled, the server accumulates each request's processing time into\n\t// running totals and a recent-request ring buffer. Response messages are\n\t// left unchanged; the client folds this data into its own timing snapshot\n\t// on demand via getServerTiming / resetServerTiming requests.\n\tCollectTiming bool\n}\n\n// StdioServer runs an API session over STDIO using MessagePack protocol.\n// This is the entry point for the synchronous STDIO-based API used by\n// native TypeScript tooling integration.\ntype StdioServer struct {\n\toptions *StdioServerOptions\n}\n\n// NewStdioServer creates a new STDIO-based API server.\nfunc NewStdioServer(options *StdioServerOptions) *StdioServer {\n\tif options.Cwd == \"\" {\n\t\tpanic(\"StdioServerOptions.Cwd is required\")\n\t}\n\n\treturn &StdioServer{\n\t\toptions: options,\n\t}\n}\n\n// Run starts the server and blocks until the connection closes.\nfunc (s *StdioServer) Run(ctx context.Context) error {\n\tvar transport Transport\n\tif s.options.PipePath != \"\" {\n\t\tt, err := NewPipeTransport(s.options.PipePath)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to create pipe transport: %w\", err)\n\t\t}\n\t\tdefer t.Close()\n\t\ttransport = t\n\t} else {","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/microsoft/typescript-go/blob/1bcfa18d79a3be41772223d5c05dfe4480e614ff/internal/api/server.go#L30-L66","documentation":"NewStdioServer panics with \"StdioServerOptions.Cwd is required\" when the options struct is constructed with an empty Cwd field. Cwd becomes SessionOptions.CurrentDirectory of the underlying project session, so it anchors every relative path (tsconfig resolution, file names, default library location) and cannot be empty. This is a fail-fast programmer-error check in the constructor: it crashes the process instead of returning an error.","triggerScenarios":"Calling api.NewStdioServer(&api.StdioServerOptions{...}) without setting Cwd; building StdioServerOptions from a flag/env var that is empty because the flag was not parsed or the env var is missing; constructing a zero-valued StdioServerOptions and setting only In/Out streams.","commonSituations":"Embedding the typescript-go API server in a host process (LSP/tooling integration) and forgetting the working directory; tests that wire os.Stdin/os.Stdout but skip Cwd; a CLI where the cwd flag defaults to \"\" when the process is spawned without arguments; refactors that move server construction before cwd detection.","solutions":["Set Cwd to the absolute path of the workspace root the server should treat as the project's current directory","If Cwd comes from a flag/env var, validate it is non-empty before calling NewStdioServer and surface a proper error instead of the panic","In tests, pass t.TempDir() or the test's working directory"],"exampleFix":"// before\nsrv := api.NewStdioServer(&api.StdioServerOptions{In: os.Stdin, Out: os.Stdout}) // panics\n\n// after\ncwd, _ := os.Getwd()\nsrv := api.NewStdioServer(&api.StdioServerOptions{In: os.Stdin, Out: os.Stdout, Cwd: cwd})","handlingStrategy":"validation","validationCode":"// Go host code: validate before constructing the server.\nif options.Cwd == \"\" {\n    return fmt.Errorf(\"StdioServerOptions.Cwd is required: set it to the workspace root\")\n}\nif !filepath.IsAbs(options.Cwd) {\n    options.Cwd, _ = filepath.Abs(options.Cwd)\n}\nsrv := api.NewStdioServer(options)","typeGuard":null,"tryCatchPattern":"// Panics cannot be caught in Go; convert to an error path before construction:\nfunc newServer(opts api.StdioServerOptions) (*api.StdioServer, error) {\n    if strings.TrimSpace(opts.Cwd) == \"\" {\n        return nil, errors.New(\"cwd must be set to the workspace root\")\n    }\n    return api.NewStdioServer(&opts), nil\n}","preventionTips":["Populate Cwd from a required CLI flag or os.Getwd() with an error check","Fail fast in main() with a clear message instead of letting the constructor panic","In tests, default Cwd to t.TempDir()"],"tags":["panic","configuration","server","initialization","embedding"],"backgroundTag":null,"analyzedSha":"1bcfa18d79a3be41772223d5c05dfe4480e614ff","analyzedAt":"2026-08-16T02:12:00.115Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}