microsoft/typescript-go · error · Error

Failed to extract version from version.go

Error message

Failed to extract version from version.go

What it means

Per-checker type dumps are written by typeTracer.DumpTypes: it builds a JSON array where each traced type is serialized with json.MarshalWrite into a strings.Builder, with the type's line number matching its type ID. This error wraps a marshal failure for the specific typ.Id() that failed, after which the whole types_N.json for that checker is abandoned (StopTracing wraps it further as 'failed to dump types for checker %d'). The most common real cause is an unencodable value inside a type descriptor — e.g. a NaN/Inf float, or a value type the JSON shim rejects.

Source

Thrown at Herebyfile.mjs:1217

        try {
            deferred.resolve(this._action());
        }
        catch (e) {
            deferred.reject(e);
        }
    }
}

const getVersion = memoize(() => {
    if (nativePreviewReleaseVersion) {
        return nativePreviewReleaseVersion;
    }

    const f = fs.readFileSync("./internal/core/version.go", "utf8");

    const match = f.match(/var version\s*=\s*"(\d+\.\d+\.\d+)(-[^"]+)?"/);
    if (!match) {
        throw new Error("Failed to extract version from version.go");
    }

    let version = match[1];
    if (options.setPrerelease) {
        version += `-${options.setPrerelease}`;
    }
    else if (match[2]) {
        version += match[2];
    }

    return version;
});

function getPublishTag() {
    if (publishAsTypescript) {
        const version = getVersion();
        if (!version) {
            throw new Error("Publishing as 'typescript' requires a version before selecting an npm tag.");

View on GitHub (pinned to 1bcfa18d79)

Solutions

  1. Reproduce with the same --trace flags and inspect the reported type ID (message includes it) using the checker's traced type list to find the offending value.
  2. Sanitize descriptor values in buildTypeDescriptor: convert NaN/Inf floats to strings or omit them; keep only encodable types (strings, numbers, bools, maps, slices).
  3. If using a custom JSON shim, test json.MarshalWrite against the failing descriptor directly and fix the shim (or fall back to encoding/json).
  4. Report upstream with the minimal repro — type-dependent marshal failures usually indicate a descriptor-field bug.

Example fix

// before: raw float that can be NaN leaks into the descriptor
args["value"] = f

// after: keep descriptors JSON-safe
if math.IsNaN(f) || math.IsInf(f, 0) {
	args["value"] = fmt.Sprintf("%v", f)
} else {
	args["value"] = f
}
Defensive patterns

Strategy: try-catch

Try / catch

if err := tr.StopTracing(); err != nil {
	if strings.Contains(err.Error(), "failed to marshal type") {
		// message includes the failing type ID; timing trace may still be usable,
		// but types_%d.json for that checker is incomplete — report upstream with repro
		log.Printf("type serialization failed: %v", err)
	}
}

Prevention

When it happens

Trigger: StopTracing (which calls tracer.DumpTypes per checker) when buildTypeDescriptor produces a descriptor containing a value the marshaller refuses: NaN/±Inf floats from type display data, func/chan values, cycles not normalized by recursionIdentityMap, or a custom JSON shim (json.MarshalWrite from the project's json package) rejecting a shape it doesn't support.

Common situations: Tracing checkers that observed exotic types (arithmetic producing NaN in symbol displays, very deep/generic recursion); swapping or version-skewing the project's custom JSON encoder; changes to buildTypeDescriptor adding raw checker values that encoding/json would reject.

Related errors


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