Skip to content

fix: prevent external import map collisions (#2)#8

Merged
danfma merged 1 commit into
mainfrom
fix/issue-2-external-import-collisions
Apr 9, 2026
Merged

fix: prevent external import map collisions (#2)#8
danfma merged 1 commit into
mainfrom
fix/issue-2-external-import-collisions

Conversation

@danfma

@danfma danfma commented Apr 9, 2026

Copy link
Copy Markdown
Owner

Summary

  • Replace unconditional _externalImportMap[key] = entry with RegisterExternalImportMapping that keeps the first registered entry and emits an AmbiguousConstruct warning on collision
  • Add CompileLibrary(source, assemblyName) and CompileConsumer(source, params compilations) overloads to TranspileHelper to support multi-library test scenarios
  • Add two tests: local-wins-over-referenced collision, and referenced-vs-referenced collision with diagnostic

Test plan

  • All 308 TUnit tests pass
  • LocalImportMapping_WinsOverReferencedCollision — local [Import] wins, diagnostic emitted
  • ReferencedImportCollision_EmitsDiagnosticAndKeepsFirstMapping — first referenced library wins, diagnostic emitted

Closes #2

)

Replace unconditional dictionary assignment with RegisterExternalImportMapping
that keeps the first registered entry and emits a warning diagnostic on
collision. Add multi-library CompileConsumer overload and two collision tests.

Closes #2
Copilot AI review requested due to automatic review settings April 9, 2026 13:35
@coderabbitai

coderabbitai Bot commented Apr 9, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@danfma has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 6 minutes and 13 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 6 minutes and 13 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f2a07d2c-7bf7-4455-920f-637ec9cd26be

📥 Commits

Reviewing files that changed from the base of the PR and between 2d9c47d and 073ef35.

📒 Files selected for processing (3)
  • MetaSharp.Compiler.TypeScript/Transformation/TypeTransformer.cs
  • MetaSharp.Tests/CrossPackageImportTests.cs
  • MetaSharp.Tests/TranspileHelper.cs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-2-external-import-collisions

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses non-deterministic external import resolution caused by simple-name collisions across referenced assemblies by detecting collisions, keeping the first registered mapping (so locals win via registration order), and emitting an MS0003 warning when collisions occur.

Changes:

  • Introduce RegisterExternalImportMapping to prevent _externalImportMap overwrites and emit an AmbiguousConstruct warning on collisions.
  • Add CompileLibrary(source, assemblyName) and CompileConsumer(source, params compilations) overloads to support multi-library test scenarios.
  • Add new cross-package tests for local-vs-referenced and referenced-vs-referenced import collisions.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
MetaSharp.Compiler.TypeScript/Transformation/TypeTransformer.cs Prevents external import map collisions by registering first mapping and warning on conflicts.
MetaSharp.Tests/TranspileHelper.cs Adds compilation helper overloads to build multiple distinct referenced libraries for tests.
MetaSharp.Tests/CrossPackageImportTests.cs Adds tests covering collision scenarios and diagnostic emission.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +344 to +348
var consumer = """
[assembly: TranspileAssembly]

public class App
{

Copilot AI Apr 9, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is named as if it verifies the "keeps first mapping" behavior, but the consumer source never references Moment, so no import statement is emitted and the assertion only checks that a diagnostic exists. To actually validate the winning mapping, reference Moment in the consumer (e.g., as a property type/new instance) and assert the generated TS imports from moment-a (and not moment-b).

Copilot uses AI. Check for mistakes.

@danfma danfma left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re: Copilot's comment on ReferencedImportCollision_EmitsDiagnosticAndKeepsFirstMapping — the first test (LocalImportMapping_WinsOverReferencedCollision) already validates that the winning mapping appears in the generated output and the loser doesn't. This second test focuses specifically on the referenced-vs-referenced scenario where the primary concern is collision detection — the diagnostic is the meaningful assertion here.

@danfma
danfma merged commit 8ea0f93 into main Apr 9, 2026
5 checks passed
@danfma
danfma deleted the fix/issue-2-external-import-collisions branch April 9, 2026 13:54
danfma added a commit that referenced this pull request Apr 9, 2026
…Coverage (#3)

Adds the Microsoft code coverage extension so tests can be run with the
--coverage flag. Usage:

  dotnet run --project tests/MetaSharp.Tests/ -- \
    --coverage --coverage-output-format cobertura \
    --coverage-output coverage.cobertura.xml \
    --results-directory TestResults

Current baseline: 74.1% line coverage, 66.0% branch coverage.
  - MetaSharp.Compiler.TypeScript: 75.5% lines, 66.5% branches
  - MetaSharp.Compiler: 49.8% lines, 56.7% branches

All four gaps listed in the issue have been closed by prior PRs:
  - [Name] on types/nested types: PR #7 (NameAttributeConsistencyTests)
  - GenerateGuard with fields: PR #6
  - _externalImportMap collisions: PR #8 (RegisterExternalImportMapping)
  - Coverage tooling: this commit

CLAUDE.md updated with the coverage command.

Closes #3
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.

Transitive external imports can be overwritten by simple-name collisions

2 participants