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

Closed
4 tasks done
brrusino opened this issue Nov 12, 2024 · 2 comments · Fixed by #5189
Closed
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
github-merge-queue bot pushed a commit that referenced this issue Nov 25, 2024
Fixes #5077 

This PR fixes an issue where passing in a literal union to a template
would cause the `@typespec/json-schema` package to crash:

[playground](https://[typespec.io](https://typespec.io/playground/?c=aW1wb3J0ICJAdHlwZXNwZWMvanNvbi1zY2hlbWEiOw0KDQp1c2luZyBUeXBlU3BlYy5Kc29uU8UfxR5AxDDGEA0KbmFtZXNwYWNlIEV4YW1wbGXFI21vZGVsIEZvbyB7DQogIMRqOiBUZW1wbGF0ZTzEGHwgbnVsbD47DQp9yjHJIlQ%2Bxjlwcm9wOiBUxCY%3D&e=%40typespec%2Fjson-schema&options=%7B%22options%22%3A%7B%22%40typespec%2Fjson-schema%22%3A%7B%22emitAllRefs%22%3Atrue%2C%22emitAllModels%22%3Afalse%7D%7D%7D)/playground/?c=aW1wb3J0ICJAdHlwZXNwZWMvanNvbi1zY2hlbWEiOw0KDQp1c2luZyBUeXBlU3BlYy5Kc29uU8UfxR5AxDDGEA0KbmFtZXNwYWNlIEV4YW1wbGXFI21vZGVsIEZvbyB7DQogIMRqOiBUZW1wbGF0ZTzEGHwgbnVsbD47DQp9yjHJIlQ%2Bxjlwcm9wOiBUxCY%3D&e=%40typespec%2Fjson-schema&options=%7B%22options%22%3A%7B%22%40typespec%2Fjson-schema%22%3A%7B%22emitAllRefs%22%3Atrue%2C%22emitAllModels%22%3Afalse%7D%7D%7D)

In this scenario, the template instantiation will be inlined - similar
to what the openapi3 emitter does. However, the scope of the parent
referencing the model literal wasn't being preserved.

So where this used to crash:
```tsp
import "@typespec/json-schema";
using TypeSpec.JsonSchema;

@jsonschema
namespace Example;

model Foo {
  type: Template<Foo | null>;
}

model Template<T> {
  prop: T;
}
```

This would now generate the output:
```yaml
$schema: https://json-schema.org/draft/2020-12/schema
$id: Foo.yaml
type: object
properties:
  type:
    type: object
    properties:
      prop:
        anyOf:
          - $ref: Foo.yaml
          - type: "null"
    required:
      - prop
required:
  - type
```

---------

Co-authored-by: Christopher Radek <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants