Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Error: Emit context must have a scope set in order to create references to declarations. #5077

Open
4 tasks done
brrusino opened this issue Nov 12, 2024 · 2 comments
Open
4 tasks done
Assignees
Labels

Comments

@brrusino
Copy link

brrusino commented Nov 12, 2024

Describe the bug

Hello,

We recently migrated from TypeSpec 0.55.0 to 0.62.0 and are seeing the following failure when trying use our existing (and previously working) json schema emitter implementation:

PS C:\Repos\Chaos\ARM-Gateway\temp\TypeSpec\2024-01-01> tsp compile main-wrapper.tsp --debug --no-emit
TypeSpec compiler v0.62.0

Emitter "@typespec/json-schema" crashed! This is a bug.
Please file an issue at https://github.com/microsoft/typespec/issues

Error: Emit context must have a scope set in order to create references to declarations.
    at compilerAssert (file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/compiler/dist/src/core/diagnostics.js:169:11)
    at invokeReference (file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/compiler/dist/src/emitter-framework/asset-emitter.js:195:25)
    at file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/compiler/dist/src/emitter-framework/asset-emitter.js:183:28
    at withPatchedReferenceContext (file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/compiler/dist/src/emitter-framework/asset-emitter.js:540:20)
    at Object.emitTypeReference (file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/compiler/dist/src/emitter-framework/asset-emitter.js:144:20)
    at JsonSchemaEmitter.unionVariant (file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/json-schema/dist/src/json-schema-emitter.js:210:42)
    at file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/compiler/dist/src/emitter-framework/asset-emitter.js:377:55
    at withTypeContext (file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/compiler/dist/src/emitter-framework/asset-emitter.js:520:9)
    at invokeTypeEmitter (file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/compiler/dist/src/emitter-framework/asset-emitter.js:367:9)
    at Object.emitType (file:///C:/Repos/Chaos/ARM-Gateway/node_modules/@typespec/compiler/dist/src/emitter-framework/asset-emitter.js:268:28)

Reproduction

tspconfig is as follows:

emit:
  - "@typespec/json-schema"
options:
  "@typespec/json-schema":
    file-type: "json"
    emitAllModels: false
    emitAllRefs: true
linter:
  extends:
    - "@azure-tools/typespec-azure-rulesets/resource-manager"

Checklist

@chrisradek
Copy link
Member

chrisradek commented Nov 13, 2024

I was able to create a simple repro:
Playground

This happens whether emitAllModels is true or false.

Something about spreading a model that contains a Record instantiated with a union where one of the variants is a model reference.

This is seen in the wild with usage of Azure.ResourceManager.ManagedServiceIdentityProperty

@chrisradek
Copy link
Member

Another note - I tested the simple repro with @typespec/compiler and @typespec/json-schema version 0.55.0 and they encounter the same issue - so I suspect there were changes to some other TypeSpec libraries that now exercise this problem scenario.

@chrisradek chrisradek self-assigned this Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants