microsoft / typespec

https://typespec.io/
MIT License
4.51k stars 216 forks source link

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

Open brrusino opened 3 days ago

brrusino commented 3 days ago

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 commented 2 days ago

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 commented 2 days ago

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.