diff --git a/packages/typespec-ts/src/framework/hooks/binder.ts b/packages/typespec-ts/src/framework/hooks/binder.ts index 2e7f542573..140e2ddfbf 100644 --- a/packages/typespec-ts/src/framework/hooks/binder.ts +++ b/packages/typespec-ts/src/framework/hooks/binder.ts @@ -232,6 +232,10 @@ class BinderImp implements Binder { this.resolveDependencyReferences(file); const importStructures = this.imports.get(file); if (importStructures) { + // Sort imports in place by module specifier to ensure consistent ordering + importStructures.sort((a, b) => + a.moduleSpecifier < b.moduleSpecifier ? -1 : 1 + ); for (const importStructure of importStructures) { file.addImportDeclaration(importStructure); }