microsoft / typespec

Home Page:https://typespec.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: Crash when spreading a model that doesn't exist due to Versioning

tjprescott opened this issue · comments

Describe the bug

The following TypeSpec crashes.

@added(Versions.v2)
model MyOptions {
  blah: string;
}

@added(Versions.v1)
@put op foo(
  ...MyOptions
): Foo;

Here we are trying to spread in a model that doesn't exist until v2. Since we cannot decorate the spread operation, this seems like a reasonable approach if you are trying to avoid having two versions of the operation with @added and @removed (which has it's own issues). This should arguably be treated like spreading in an empty model, which works fine.

Reproduction

https://cadlplayground.z22.web.core.windows.net/?c=aW1wb3J0ICJAdHlwZXNwZWMvdmVyc2lvbmluZyI7DQrSIGh0dHDEGg0KdXNpbmcgVskwO8gTSHR0cMUiQHNlcnZpY2Uoe25hbWU6ICJEZW1vIFPGFSJ9KQ0KQMdvZWQox0lzKQ0KxC5zcGFjZSDEMcVLZW51bchscyB7DQogIHYxLMUHMg0KfcQjbW9kZWwgRm9vxh%2FGc3N0cuYAm8UjQGFkZMtrLnYyKcg4TXlPcHTKXWJsYWjfPi52MeQAwnB1dCBvcCBmb28oxDkuLi7JSw0KKTrkAJE7&e=%40typespec%2Fopenapi3&options=%7B%7D

Checklist

Agree this should not crash. Not sure I agree that it should behave as an empty model in this case. You could argue that because spread of an empty model is a no-op, it's ok, but I'm more concerned about treating spread differently from other kinds of references. It may make it hard to reason about what the behavior is going to be when referencing a type which doesn't exist at a particular version.

@bterlson Would you prefer a diagnostic in this case then?

IIRC, the suggested path forward to be to treat it like spreading never which should trigger an existing diagnostic. Though my concern would be that it doesn't actually unblock the original scenario and necessitates some ugly workarounds.