diff --git a/.chronus/changes/copilot-fix-interpolating-invalid-references-2026-1-9-13-16-6.md b/.chronus/changes/copilot-fix-interpolating-invalid-references-2026-1-9-13-16-6.md new file mode 100644 index 00000000000..6368670162f --- /dev/null +++ b/.chronus/changes/copilot-fix-interpolating-invalid-references-2026-1-9-13-16-6.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/compiler" +--- + +Don't report `non-literal-string-template` diagnostic when interpolating an invalid reference \ No newline at end of file diff --git a/packages/compiler/src/core/helpers/string-template-utils.ts b/packages/compiler/src/core/helpers/string-template-utils.ts index 3fd9cb660c6..349f74ad746 100644 --- a/packages/compiler/src/core/helpers/string-template-utils.ts +++ b/packages/compiler/src/core/helpers/string-template-utils.ts @@ -1,5 +1,6 @@ import { createDiagnosticCollector } from "../diagnostics.js"; import { createDiagnostic } from "../messages.js"; +import { isErrorType } from "../type-utils.js"; import type { Diagnostic, StringTemplate } from "../types.js"; export function isStringTemplateSerializable( @@ -35,12 +36,14 @@ export function explainStringTemplateNotSerializable( } // eslint-disable-next-line no-fallthrough default: - diagnostics.add( - createDiagnostic({ - code: "non-literal-string-template", - target: span, - }), - ); + if (!isErrorType(span.type)) { + diagnostics.add( + createDiagnostic({ + code: "non-literal-string-template", + target: span, + }), + ); + } } } } diff --git a/packages/compiler/test/checker/values/string-values.test.ts b/packages/compiler/test/checker/values/string-values.test.ts index 383adc1f08d..f8642586c83 100644 --- a/packages/compiler/test/checker/values/string-values.test.ts +++ b/packages/compiler/test/checker/values/string-values.test.ts @@ -87,6 +87,14 @@ describe("string templates", () => { "Value interpolated in this string template cannot be converted to a string. Only literal types can be automatically interpolated.", }); }); + + it("only emit invalid-ref error when interpolating an invalid reference, not non-literal-string-template", async () => { + const diagnostics = await diagnoseValue(`string("Some \${bad}")`); + expectDiagnostics(diagnostics, { + code: "invalid-ref", + message: "Unknown identifier bad", + }); + }); }); describe("validate literal are assignable", () => {