Skip to content

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

Closed

Description

@mediremi

When a binding's @scope has the same name as a module, the binding's scope is shadowed by the module:

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

URL.revokeObjectURL("some url")
let URL = {};

URL.revokeObjectURL("some url");

Activity

  1. cknitt commented on May 17, 2025

    @cknitt
    Member

    I think this is not a v12 issue per se, it's just that v12 doesn't escape URL with $$ anymore.

    If the module name is different, the same problem exists in v11:

    module URRL = {  
      @val @scope("URRL")
      external revokeObjectURL: string => unit = "revokeObjectURL"
    }
    
    URRL.revokeObjectURL("some url")

    https://rescript-lang.org/try?version=v11.1.4&module=esmodule&code=LYewJgrgNgpgBAVQEpIDJwLxwN5zgKDwAEA3AQyjiIGcBjEABxgAoAiZNVgSkLhgA8ALjABOAOwpwRMEiADWMAPIAjAFYxag5KgBccaoJEBLMQHNMAPjgQxRwZjitpshSvWbtrfAF98+DqgAdM7ySmoaWmhs1CDA8BAiUNz4QA

  2. mediremi commented on May 17, 2025

    @mediremi
    MemberAuthor

    Ah I didn't read the breaking changes section of v12.0.0-alpha.1 carefully enough - I see now that since #6831 we're not escaping browser globals anymore.

    I'll update this issue title and description to not be v12 specific 👍

  3. changed the title [-]v12 breaks bindings where `@scope` has same name as module[/-] [+]Bindings where `@scope` has same name as module can result in runtime errors[/+] on May 17, 2025
  4. cometkim commented on May 17, 2025

    @cometkim
  5. cometkim commented on May 18, 2025

    @cometkim
    Member

    I see. It should be something like:

    let URL$1 = {};
    
    URL.revokeObjectURL("some url");
    
    export {
      URL$1 as URL,
    }
  6. cometkim commented on May 18, 2025

    @cometkim
    Member

    Workaround (IMO the correct resolution):

    module URL = {  
      @val @scope("globalThis.URL")
      external revokeObjectURL: string => unit = "revokeObjectURL"
    }
    
    URL.revokeObjectURL("some url")
  7. cknitt commented on May 19, 2025

    @cknitt
    Member

    Although this can be broken in basically the same way:

    module URL = {  
      @val @scope("globalThis.URL")
      external revokeObjectURL: string => unit = "revokeObjectURL"
    }
    
    let globalThis = 0
    
    URL.revokeObjectURL("some url")
  8. cometkim commented on May 19, 2025

    @cometkim
    Member

    globalThis should be mangled by default.

    The output in v12 is:

    let URL = {};
    
    globalThis.URL.revokeObjectURL("some url");
    
    let $$globalThis = 0;
    
    export {
      URL,
      $$globalThis,
    }
    /*  Not a pure module */
  9. zth commented on May 20, 2025

    @zth
    Member

    One downside of leveraging globalThis is that it doesn't look handwritten. But I guess there's no other good way around this.

  10. cometkim commented on May 20, 2025

    @cometkim
    Member

    Maybe we can make it as a standard like @globalScope(..) ?

  11. zth commented on May 20, 2025

    @zth
    Member

    Are there any alternative ways around this? Could we detect when this unintended clash happens and only then emit globalThis?

  12. cknitt commented on May 20, 2025

    @cknitt
    Member

    globalThis should be mangled by default.

    Ah, right, it is mangled in v12, but not v11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions