Skip to content

Conversation

@cknitt
Copy link
Member

@cknitt cknitt commented Dec 21, 2025

Fix @scope shadowing (fixes #7478)

Summary

When @scope("URL") is used inside a module named URL, the local module binding shadows the global URL and the JS output calls the wrong object. This PR rewrites the scope base to globalThis.URL when the scope name is shadowed by a local module binding.

Example:

module URL = {
  @val @scope("URL")
  external revokeObjectURL: string => unit = "revokeObjectURL"
}

URL.revokeObjectURL("some url")

Output after this change:

globalThis.URL.revokeObjectURL("some url");

Why this approach

  • The problem is shadowing: a local module name hides the intended global scope.
  • We only rewrite the scope base when a local binding would shadow it.
  • This keeps normal code unchanged and avoids adding globalThis everywhere.

Tests

  • tests/tests/src/ExternalScopeShadow.res covers the shadowed scope and module alias case.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 21, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@8100

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@8100

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@8100

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@8100

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@8100

@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@8100

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@8100

commit: 6888c01

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

Looks good to me.
I assume the generated js is the desired one.

@cknitt
Copy link
Member Author

cknitt commented Dec 22, 2025

Yes, JS output is fine.

@cknitt cknitt merged commit ff9d6d3 into rescript-lang:master Dec 22, 2025
25 checks passed
@cknitt cknitt deleted the more-shadow branch December 22, 2025 10:03
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.

Bindings where @scope has same name as module can result in runtime errors

2 participants