Skip to content

Fix: prune unreachable definitions when --type "*" is used with multiple exports#2284

Merged
arthurfiorette merged 10 commits intovega:nextfrom
alexchexes:fix/star-emits-unreachable
Jun 22, 2025
Merged

Fix: prune unreachable definitions when --type "*" is used with multiple exports#2284
arthurfiorette merged 10 commits intovega:nextfrom
alexchexes:fix/star-emits-unreachable

Conversation

@alexchexes
Copy link
Contributor

@alexchexes alexchexes commented Jun 19, 2025

With --type "*" (“export all”), the generator pruned unreachable definitions only when there was a single root export.
If two or more symbols were exported from the entry file, every transitive reference of every root leaked into definitions, producing a bloated schema.

Consider this entry file that we export with type: "*" (in test suite + mainTsOnly: true):

main.ts
import type { SomeInterface } from "./dep";

export type MyType = string;

export interface MyObject extends SomeInterface {
    bar?: number;
}

The resulting schema was:

schema.json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "MyObject": {
      "additionalProperties": false,
      "properties": {
        "bar": { "type": "number" },
        "foo": { "type": "string" }
      },
      "type": "object"
    },
    "MyType": { "type": "string" },
    "SomeInterface": {
      "additionalProperties": false,
      "properties": {
        "foo": { "type": "string" }
      },
      "type": "object"
    }
  }
}

SomeInterface was present even though it is not exported by the entry file.

If we just comment out export type MyType = string;:

main.ts
import type { SomeInterface } from "./dep";

// export type MyType = string;

export interface MyObject extends SomeInterface {
    bar?: number;
}

…the emitted schema no longer includes SomeInterface:

schema.json
{
  "$ref": "#/definitions/MyObject",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "MyObject": {
      "additionalProperties": false,
      "properties": {
        "bar": { "type": "number" },
        "foo": { "type": "string" }
      },
      "type": "object"
    }
  }
}

This happened because SchemaGenerator.createSchemaFromNodes failed to pass any root definitions to removeUnreachable when more than one export was present; it passed undefined, so the pruning step was skipped and every collected definition remained.

Fix

In SchemaGenerator.ts we now:

  • Collect root definitions for all exports.
  • Run removeUnreachable for each root.
  • Merge the reachable sets into the final schema.

Tests

Added fixture export-star-prune-unreachable in the valid-data-type suite which confirms that only the entry-file exports are emitted; unrelated definitions are pruned.

Impact

Output schemas are now minimal and deterministic when "*" is used, no matter how many items are exported from the main file.

No API changes.

Version

Published prerelease version: v2.5.0-next.4

Changelog

🎉 This release contains work from new contributors! 🎉

Thanks for all your work!

❤️ Alex (@alexchexes)

❤️ Valentyne Stigloher (@pixunil)

🚀 Enhancement

  • feat: Add --full-description option to include full comment in schema #2224 (@alexchexes)
  • feat(parser): support SpreadElement in array literals #2269 (@alexchexes)

🐛 Bug Fix

  • Fix: prune unreachable definitions when --type "*" is used with multiple exports #2284 (@alexchexes @arthurfiorette)
  • Fix: crash when a union includes symbol #2282 (@alexchexes)
  • fix: correctly generate anyOf on unions with string and boolean constant #2208 (@pixunil)
  • fix: fully unwrap union aliases in mapped keys to avoid generating incorrect additionalProperties #2232 (@alexchexes)
  • fix: avoid incorrect additionalProperties for Pick<..., AliasLiteralUnion> #2230 (@alexchexes)

🔩 Dependency Updates

Authors: 4

@alexchexes alexchexes changed the title Fix emitting unreachable objects when type: "*" is used and there is more than one export in the main file Fix: prune unreachable definitions when --type "*" is used with multiple exports Jun 19, 2025
@alexchexes alexchexes changed the title Fix: prune unreachable definitions when --type "*" is used with multiple exports Fix: prune unreachable definitions when --type "*" is used with multiple exports Jun 19, 2025
@arthurfiorette arthurfiorette merged commit c3b9504 into vega:next Jun 22, 2025
3 of 4 checks passed
@alexchexes alexchexes deleted the fix/star-emits-unreachable branch June 22, 2025 22:18
@github-actions
Copy link

github-actions bot commented Feb 4, 2026

🚀 PR was released in v2.5.0 🚀

@github-actions github-actions bot added released This issue/pull request has been released. and removed prerelease labels Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants