microsoft/typescript-go · error

failed to encode source file: %w

Error message

failed to encode source file: %w

What it means

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.

Source

Thrown at internal/api/session.go:1361

		configSourceFile := tsoptions.NewTsconfigSourceFileFromFilePath(configFileName, requestedPath, configFileContent)
		return s.encodeSourceFileResponse(configSourceFile.SourceFile)
	}

	return s.encodeSourceFileResponse(nil)
}

func (s *Session) encodeSourceFileResponse(sourceFile *ast.SourceFile) (any, error) {
	if sourceFile == nil {
		if s.useBinaryResponses {
			return RawBinary(nil), nil
		}
		return nil, nil
	}

	// Encode the full source file.
	data, _, err := encoder.EncodeSourceFile(sourceFile)
	if err != nil {
		return nil, fmt.Errorf("failed to encode source file: %w", err)
	}

	if s.useBinaryResponses {
		return RawBinary(data), nil
	}
	return &SourceFileResponse{
		Data: base64.StdEncoding.EncodeToString(data),
	}, nil
}

// handleGetSourceFileNames returns file names of all source files in a project.
func (s *Session) handleGetSourceFileNames(ctx context.Context, params *GetSourceFileNamesParams) ([]string, error) {
	sd, err := s.getSnapshotData(params.Snapshot)
	if err != nil {
		return nil, err
	}

	program, err := sd.getProgram(params.Project)

View on GitHub (pinned to 1bcfa18d79)

Solutions

  1. Update typescript-go to a build where parser and encoder are in sync
  2. If you only need file-level facts, use getSourceFileMetadata instead of the full encoded AST
  3. Reproduce with the single file and report it upstream with the input source

Example fix

// before
resp, err := getSourceFile(snapshot, project, file) // failed to encode source file

// after
meta, err := getSourceFileMetadata(snapshot, project, file) // metadata-only path avoids the encoder
Defensive patterns

Strategy: try-catch

Type guard

func isEncodeFailure(err error) bool {
    return err != nil && strings.Contains(err.Error(), "failed to encode source file")
}

Try / catch

resp, err := session.HandleRequest(ctx, string(api.MethodGetSourceFile), params)
if isEncodeFailure(err) {
    // encoder bug, not your input: fall back to metadata and report upstream
    meta, merr := session.HandleRequest(ctx, string(api.MethodGetSourceFileMetadata), params)
    if merr != nil { return merr }
    return meta
}
if err != nil { return err }

Prevention

When it happens

Trigger: 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.

Common situations: Nightly or mismatched typescript-go builds; exotic or generated TypeScript that exercises rarely-used syntax; clients relying on full AST dumps hitting encoder gaps.

Related errors


AI-assisted analysis of microsoft/typescript-go@1bcfa18d79 (2026-08-16). Data as JSON: /api/errors/08919d906f0645e2. Report an issue: GitHub.