Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 1 addition & 26 deletions packages/typespec-ts/src/framework/hooks/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,13 @@ class BinderImp implements Binder {
private project: Project;
private dependencies: Record<string, ReferenceableSymbol>;
private staticHelpers: Map<string, StaticHelperMetadata>;
private useSubpathImports: boolean;

constructor(project: Project, options: BinderOptions = {}) {
this.project = project;

provideDependencies(options.dependencies);
this.staticHelpers = options.staticHelpers ?? new Map();
this.dependencies = useDependencies();
this.useSubpathImports = options.useSubpathImports ?? false;
}

trackDeclaration(refkey: unknown, name: string, sourceFile: SourceFile): string {
Expand Down Expand Up @@ -185,27 +183,6 @@ class BinderImp implements Binder {
return importSpecifier;
}

/**
* Returns the #platform/ subpath import specifier for a static helper file
* that has a polyfill variant (-browser.mts or -react-native.mts sibling),
* or undefined if subpath imports are disabled or no variant exists.
* e.g. "src/static-helpers/serialization/get-binary-response.ts"
* -> "#platform/static-helpers/serialization/get-binary-response"
*/
private getPlatformImportSpecifier(declarationSourceFile: SourceFile): string | undefined {
if (!this.useSubpathImports) return undefined;
const filePath = declarationSourceFile.getFilePath();
const srcIndex = filePath.indexOf("/src/");
if (srcIndex === -1) return undefined;
// Check if a -browser.mts or -react-native.mts sibling exists
const basePath = filePath.replace(/\.ts$/, "");
const hasBrowserVariant = this.project.getSourceFile(basePath + "-browser.mts");
const hasReactNativeVariant = this.project.getSourceFile(basePath + "-react-native.mts");
if (!hasBrowserVariant && !hasReactNativeVariant) return undefined;
const relativePath = filePath.substring(srcIndex + "/src/".length);
return "#platform/" + relativePath.replace(/\.ts$/, "");
}

/**
* Applies all tracked imports to their respective source files.
*/
Expand Down Expand Up @@ -316,9 +293,7 @@ class BinderImp implements Binder {

if (file !== declarationSourceFile) {
this.trackReference(declarationKey, file);
// Use #platform/ subpath import specifier for static helpers in warp packages
const platformSpecifier = this.getPlatformImportSpecifier(declarationSourceFile);
const importTarget = platformSpecifier ?? declarationSourceFile;
const importTarget = declarationSourceFile;
const importDec = this.addImport(file, importTarget, name);
name = importDec.alias ?? name;
}
Expand Down
12 changes: 6 additions & 6 deletions packages/typespec-ts/src/framework/load-static-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { ModularEmitterOptions } from "../modular/interfaces.js";
import { resolveProjectRoot } from "../utils/resolve-project-root.js";
import { refkey } from "./refkey.js";
export const SourceFileSymbol = Symbol("SourceFile");

export interface StaticHelperMetadata {
name: string;
kind: "function" | "interface" | "typeAlias" | "class" | "enum";
Expand Down Expand Up @@ -95,21 +96,20 @@ export async function loadStaticHelpers(
overwrite: true,
});
addedFile.getImportDeclarations().map((i) => {
// Rewrite relative platform-types imports to #platform/ specifiers
// so that browser/react-native variants are resolved via subpath imports.
// Only rewrite imports to the default variant (not -browser/-react-native variants
// which are already platform-specific direct imports).
// Rewrite relative platform-types imports to @azure/core-rest-pipeline for azure packages
// (NodeReadableStream is now exported directly from @azure/core-rest-pipeline).
// Non-azure packages keep the relative import to the local platform-types.ts.

const specifier = i.getModuleSpecifierValue();
if (
specifier.startsWith(".") &&
specifier.includes("platform-types") &&
!specifier.includes("-browser") &&
!specifier.includes("-react-native")
) {
i.setModuleSpecifier("#platform/static-helpers/platform-types");
i.setModuleSpecifier("@azure/core-rest-pipeline");
}
});

for (const entry of Object.values(helpers)) {
if (!addedFile.getFilePath().endsWith(entry.location)) {
continue;
Expand Down
7 changes: 2 additions & 5 deletions packages/typespec-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
CreateRecorderHelpers,
MultipartHelpers,
PagingHelpers,
PlatformTypeHelpers,
PollingHelpers,
SerializationHelpers,
SimplePollerHelpers,
Expand Down Expand Up @@ -75,7 +74,7 @@ import {
import { Project } from "ts-morph";
import { provideBinder } from "./framework/hooks/binder.js";
import { provideSdkTypes } from "./framework/hooks/sdk-types.js";
import { loadStaticHelpers } from "./framework/load-static-helpers.js";
import { loadStaticHelpers, type StaticHelpers } from "./framework/load-static-helpers.js";
import { EmitterOptions } from "./lib.js";
import { buildClassicalClient } from "./modular/build-classical-client.js";
import { buildClassicOperationFiles } from "./modular/build-classical-operation-groups.js";
Expand Down Expand Up @@ -148,12 +147,11 @@ export async function $onEmit(context: EmitContext) {
...SimplePollerHelpers,
...UrlTemplateHelpers,
...MultipartHelpers,
...PlatformTypeHelpers,
...CloudSettingHelpers,
...XmlHelpers,
...(rlcOptions.generateTest ? CreateRecorderHelpers : {}),
...(rlcOptions.enableStorageCompat ? StorageCompatHelpers : {}),
},
} as unknown as StaticHelpers,
{
sourcesDir: dpgContext.generationPathDetail?.modularSourcesDir,
rootDir: dpgContext.generationPathDetail?.rootDir,
Expand All @@ -174,7 +172,6 @@ export async function $onEmit(context: EmitContext) {
dependencies: {
...extraDependencies,
},
useSubpathImports: true,
});
provideSdkTypes(dpgContext);

Expand Down
10 changes: 3 additions & 7 deletions packages/typespec-ts/src/modular/build-root-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ import { getModularClientOptions } from "../utils/client-utils.js";
import { SdkContext } from "../utils/interfaces.js";
import { getMethodHierarchiesMap } from "../utils/operation-util.js";
import { partitionAndEmitExports } from "./build-subpath-index.js";
import { AzureCoreDependencies } from "./external-dependencies.js";
import { getClassicalClientName } from "./helpers/naming-helpers.js";
import { isLroOnlyOperation } from "./helpers/operation-helpers.js";
import { ModularEmitterOptions } from "./interfaces.js";
import {
CloudSettingHelpers,
MultipartHelpers,
PagingHelpers,
PlatformTypeHelpers,
} from "./static-helpers-metadata.js";
import { CloudSettingHelpers, MultipartHelpers, PagingHelpers } from "./static-helpers-metadata.js";

export function buildRootIndex(
context: SdkContext,
Expand Down Expand Up @@ -157,7 +153,7 @@ function exportFileContentsType(context: SdkContext, rootIndexFile: SourceFile)
rootIndexFile,
[
resolveReference(MultipartHelpers.FileContents),
resolveReference(PlatformTypeHelpers.NodeReadableStream),
resolveReference(AzureCoreDependencies["NodeReadableStream"]),
],
true,
);
Expand Down
10 changes: 10 additions & 0 deletions packages/typespec-ts/src/modular/external-dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ export const AzureCoreDependencies: CoreDependencies = {
name: "ErrorResponse",
module: "@azure-rest/core-client",
},
getBinaryStreamResponse: {
kind: "externalDependency",
module: "@azure-rest/core-client",
name: "getBinaryStreamResponse",
},
NodeReadableStream: {
kind: "externalDependency",
module: "@azure/core-rest-pipeline",
name: "NodeReadableStream",
},
};

export const AzureIdentityDependencies = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
KnownCollectionFormat,
ServiceOperation,
} from "../../utils/operation-util.js";
import { AzurePollingDependencies } from "../external-dependencies.js";
import { AzureCoreDependencies, AzurePollingDependencies } from "../external-dependencies.js";
import {
buildModelDeserializer,
buildPropertyDeserializer,
Expand All @@ -70,7 +70,6 @@ import {
} from "../serialization/serialize-utils.js";
import {
PagingHelpers,
PlatformTypeHelpers,
PollingHelpers,
SerializationHelpers,
StorageCompatHelpers,
Expand Down Expand Up @@ -1043,7 +1042,7 @@ export function getOperationFunction(
statements.push(`const ${streamableMethodVarName} = _${name}Send(${sendParameterList});`);
const binaryHelper =
wrapReturn && wrapReturnIsBinary
? SerializationHelpers.getBinaryStreamResponse
? AzureCoreDependencies["getBinaryStreamResponse"]
: SerializationHelpers.getBinaryResponse;
statements.push(
`const ${resultVarName} = await ${resolveReference(binaryHelper)}(${streamableMethodVarName});`,
Expand Down Expand Up @@ -3003,7 +3002,7 @@ export function buildNonModelResponseTypeDeclaration(
let typeBody: string;

if (isBinary) {
const nodeReadableStreamRef = resolveReference(PlatformTypeHelpers.NodeReadableStream);
const nodeReadableStreamRef = resolveReference(AzureCoreDependencies["NodeReadableStream"]);
typeBody = `{
/**
* BROWSER ONLY
Expand Down
13 changes: 0 additions & 13 deletions packages/typespec-ts/src/modular/static-helpers-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ export const SerializationHelpers = {
name: "getBinaryResponse",
location: "serialization/get-binary-response.ts",
},
getBinaryStreamResponse: {
kind: "function",
name: "getBinaryStreamResponse",
location: "serialization/get-binary-stream-response.ts",
},
areAllPropsUndefined: {
kind: "function",
name: "areAllPropsUndefined",
Expand Down Expand Up @@ -150,14 +145,6 @@ export const MultipartHelpers = {
},
} as const;

export const PlatformTypeHelpers = {
NodeReadableStream: {
kind: "typeAlias",
name: "NodeReadableStream",
location: "platform-types.ts",
},
} as const;

export const CloudSettingHelpers = {
AzureClouds: {
kind: "enum",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,6 @@ export function updatePackageFile(
// Update Core Client dependency
if (needsCoreClientUpdate) {
delete deps["@azure/core-client"];
if (!("@azure-rest/core-client" in deps)) {
deps["@azure-rest/core-client"] = "^2.3.1";
}
packageInfo.dependencies = deps;
}

Expand All @@ -146,6 +143,12 @@ export function updatePackageFile(
};
}

packageInfo.dependencies = {

@JialinHuang803 JialinHuang803 Jun 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not quite certain about this. Did we do batch update on the existing sdks to bump these dependency versions? If so, do we still need to update here?

...packageInfo.dependencies,
"@azure/core-rest-pipeline": "^1.24.0",
"@azure-rest/core-client": "^2.7.0",
};

// Update constantPaths metadata for Azure packages
if (needsConstantPathsUpdate && packageInfo["//metadata"]) {
const metadata = packageInfo["//metadata"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ export function getAzureMonorepoDependencies(config: AzureMonorepoInfoConfig) {
// revert this change after sdk repo update.
const runtimeDeps = {
...dependencies,
"@azure-rest/core-client": "^2.3.1",
"@azure-rest/core-client": "^2.7.0",
...(hasLro && {
"@azure/abort-controller": "^2.1.2",
}),
"@azure/core-auth": "^1.9.0",
...(hasLro && {
"@azure/core-lro": "^3.1.0",
}),
"@azure/core-rest-pipeline": "^1.20.0",
"@azure/core-rest-pipeline": "^1.24.0",
"@azure/core-util": "^1.12.0",
"@azure/logger": "^1.2.0",
tslib: "^2.8.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,11 @@ function getEntryPointInformation(config: PackageCommonInfoConfig) {
module: "./dist/esm/index.js",
types: "./dist/commonjs/index.d.ts",
browser: "./dist/browser/index.js",
imports: {
"#platform/*": {
browser: "./src/*-browser.mts",
default: "./src/*.ts",
} as Record<string, string>,
},
exports: resolveWarpExports(config.exports, config.generateReactNativeTarget),
};

if (config.generateReactNativeTarget) {
result["react-native"] = "./dist/react-native/index.js";
(result["imports"]["#platform/*"] as Record<string, string>)["react-native"] =
"./src/*-react-native.mts";
// Reorder so react-native comes before default
const importsEntry = result["imports"]["#platform/*"] as Record<string, string>;
result["imports"]["#platform/*"] = {
browser: importsEntry["browser"],
"react-native": importsEntry["react-native"],
default: importsEntry["default"],
};
}

return result;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("loadStaticHelpers", () => {
).rejects.toThrowError(/invalid helper kind/);
});

it("should rewrite platform-types imports to #platform subpath without extension", async () => {
it("should rewrite platform-types imports to @azure/core-rest-pipeline for azure monorepo", async () => {
const helpers = {
usesPlatformImport: {
kind: "function",
Expand All @@ -91,7 +91,7 @@ describe("loadStaticHelpers", () => {
.getSourceFiles()
.find((file) => file.getFilePath().endsWith("/static-helpers/platform-import.ts"));
assert(sourceFile);
const importDecl = sourceFile.getImportDeclaration("#platform/static-helpers/platform-types");
const importDecl = sourceFile.getImportDeclaration("@azure/core-rest-pipeline");
expect(importDecl).toBeDefined();
});
});
Loading
Loading