{"record":{"id":"08919d906f0645e2","repo":"microsoft/typescript-go","slug":"failed-to-encode-source-file-w","errorCode":null,"errorMessage":"failed to encode source file: %w","messagePattern":"failed to encode source file: %w","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/api/session.go","lineNumber":1361,"sourceCode":"\t\tconfigSourceFile := tsoptions.NewTsconfigSourceFileFromFilePath(configFileName, requestedPath, configFileContent)\n\t\treturn s.encodeSourceFileResponse(configSourceFile.SourceFile)\n\t}\n\n\treturn s.encodeSourceFileResponse(nil)\n}\n\nfunc (s *Session) encodeSourceFileResponse(sourceFile *ast.SourceFile) (any, error) {\n\tif sourceFile == nil {\n\t\tif s.useBinaryResponses {\n\t\t\treturn RawBinary(nil), nil\n\t\t}\n\t\treturn nil, nil\n\t}\n\n\t// Encode the full source file.\n\tdata, _, err := encoder.EncodeSourceFile(sourceFile)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to encode source file: %w\", err)\n\t}\n\n\tif s.useBinaryResponses {\n\t\treturn RawBinary(data), nil\n\t}\n\treturn &SourceFileResponse{\n\t\tData: base64.StdEncoding.EncodeToString(data),\n\t}, nil\n}\n\n// handleGetSourceFileNames returns file names of all source files in a project.\nfunc (s *Session) handleGetSourceFileNames(ctx context.Context, params *GetSourceFileNamesParams) ([]string, error) {\n\tsd, err := s.getSnapshotData(params.Snapshot)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tprogram, err := sd.getProgram(params.Project)","sourceCodeStart":1343,"sourceCodeEnd":1379,"githubUrl":"https://github.com/microsoft/typescript-go/blob/1bcfa18d79a3be41772223d5c05dfe4480e614ff/internal/api/session.go#L1343-L1379","documentation":"getSourceFile failed while serializing the AST: encoder.EncodeSourceFile returned an error, which is an internal encoder failure on this particular syntax tree (for example a node shape the wire encoder does not support), not a client-input problem. The error is unwrapped, so there is no ErrClientError to branch on.","triggerScenarios":"A source file containing AST shapes the encoder version cannot serialize; version skew where parser changes landed without matching encoder support; corrupted program state after unusual snapshot sequences.","commonSituations":"Nightly or mismatched typescript-go builds; exotic or generated TypeScript that exercises rarely-used syntax; clients relying on full AST dumps hitting encoder gaps.","solutions":["Update typescript-go to a build where parser and encoder are in sync","If you only need file-level facts, use getSourceFileMetadata instead of the full encoded AST","Reproduce with the single file and report it upstream with the input source"],"exampleFix":"// before\nresp, err := getSourceFile(snapshot, project, file) // failed to encode source file\n\n// after\nmeta, err := getSourceFileMetadata(snapshot, project, file) // metadata-only path avoids the encoder","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"func isEncodeFailure(err error) bool {\n    return err != nil && strings.Contains(err.Error(), \"failed to encode source file\")\n}","tryCatchPattern":"resp, err := session.HandleRequest(ctx, string(api.MethodGetSourceFile), params)\nif isEncodeFailure(err) {\n    // encoder bug, not your input: fall back to metadata and report upstream\n    meta, merr := session.HandleRequest(ctx, string(api.MethodGetSourceFileMetadata), params)\n    if merr != nil { return merr }\n    return meta\n}\nif err != nil { return err }","preventionTips":["Update typescript-go when parser/encoder versions can skew","Use getSourceFileMetadata when you do not need the full AST","Capture the offending file contents in logs (redacted as needed) to attach to an upstream issue"],"tags":["serialization","encoder","internal-error","source-file","api"],"backgroundTag":null,"analyzedSha":"1bcfa18d79a3be41772223d5c05dfe4480e614ff","analyzedAt":"2026-08-16T02:12:00.115Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}