Skip to content

fix: detect and error on TS type name collisions during export#483

Open
jcrugzz wants to merge 1 commit intoAleph-Alpha:mainfrom
jcrugzz:detect-type-name-collisions
Open

fix: detect and error on TS type name collisions during export#483
jcrugzz wants to merge 1 commit intoAleph-Alpha:mainfrom
jcrugzz:detect-type-name-collisions

Conversation

@jcrugzz
Copy link

@jcrugzz jcrugzz commented Mar 5, 2026

Summary

  • Bug: When two different Rust types both #[derive(TS)] with the same TypeScript name (e.g. via #[ts(rename = "...")]), they write to the same output file. The current behavior at export_and_merge line 165-167 is silent last-writer-wins — the second type is silently dropped with return Ok(()), producing nondeterministic TypeScript output.
  • Fix: Replace the silent skip with a content comparison. If the file content matches exactly (idempotent re-run), allow it. If the declaration is already present in a merged file, allow it. Otherwise, return a new ExportError::Collision with an actionable error message suggesting #[ts(rename = "...")] or #[ts(export_to = "...")] to disambiguate.
  • Tests: Two new integration tests — idempotent_export (same type twice succeeds) and collision_is_detected (different types with same TS name errors).

What stays the same

  • Multi-type-per-file merging (different names, same file via export_to) — untouched
  • Cross-process first-write behavior (overwrite on fresh run) — unchanged
  • EXPORT_PATHS static type signature — unchanged

Test plan

  • cargo test --all-features --test integration collision — new tests pass
  • cargo test --no-default-features --test integration collision — passes without serde-compat
  • cargo test --all-features --test integration same_file_export — existing regression guard passes
  • cargo test --all-features — full suite (528+ tests) passes with zero failures

🤖 Generated with Claude Code

When two different Rust types derive TS with the same TypeScript name,
they write to the same output file. Previously this was silent
last-writer-wins. Now `export_and_merge` compares file content and
returns an actionable `ExportError::Collision` when the generated
output differs, while still allowing idempotent re-exports.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
jcrugzz added a commit to jcrugzz/ts-rs that referenced this pull request Mar 5, 2026
When TS_RS_AUTO_NAMESPACE=true (or Config::with_auto_namespace(true)),
all exported types are organized into {crate_name}/ subdirectories.
This prevents collisions entirely — different crates get different
directories with correct relative import paths between them.

Follow-up to Aleph-Alpha#483 which added collision detection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
jcrugzz added a commit to jcrugzz/ts-rs that referenced this pull request Mar 5, 2026
When TS_RS_AUTO_NAMESPACE=true (or Config::with_auto_namespace(true)),
all exported types are organized into {crate_name}/ subdirectories.
This prevents collisions entirely — different crates get different
directories with correct relative import paths between them.

Follow-up to Aleph-Alpha#483 which added collision detection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
4dawit pushed a commit to 4dawit/ts-rs that referenced this pull request Mar 5, 2026
* fix: detect and error on TS type name collisions during export

When two different Rust types derive TS with the same TypeScript name,
they write to the same output file. Previously this was silent
last-writer-wins. Now `export_and_merge` compares file content and
returns an actionable `ExportError::Collision` when the generated
output differs, while still allowing idempotent re-exports.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: add opt-in auto-namespace by crate name to avoid type collisions

When TS_RS_AUTO_NAMESPACE=true (or Config::with_auto_namespace(true)),
all exported types are organized into {crate_name}/ subdirectories.
This prevents collisions entirely — different crates get different
directories with correct relative import paths between them.

Follow-up to Aleph-Alpha#483 which added collision detection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant