From 572401153e869a7385ba232de3ada1f6f29956a0 Mon Sep 17 00:00:00 2001 From: Qiaoqiao Zhang <55688292+qiaozha@users.noreply.github.com> Date: Fri, 17 Jan 2025 16:22:52 +0800 Subject: [PATCH] fix-windows-env-issue-in-model-namespace-hierarchy (#3008) * fix-windows-env-issue-in-model-namespace-hierarchy * fix ci * Update packages/typespec-ts/src/modular/emitModels.ts --- .scripts/ci.yml | 2 +- .vscode/launch.json | 0 packages/typespec-test/package.json | 1 + .../typespec-ts/src/modular/buildProjectFiles.ts | 12 +++++++++--- packages/typespec-ts/src/modular/buildRootIndex.ts | 14 +++++++++----- .../typespec-ts/src/modular/buildSubpathIndex.ts | 14 +++++++++----- packages/typespec-ts/src/modular/emitModels.ts | 3 ++- 7 files changed, 31 insertions(+), 15 deletions(-) mode change 100755 => 100644 .vscode/launch.json diff --git a/.scripts/ci.yml b/.scripts/ci.yml index 4eae381522..c3140dd04a 100644 --- a/.scripts/ci.yml +++ b/.scripts/ci.yml @@ -241,7 +241,7 @@ stages: vmImage: $[coalesce(variables['OSVmImage'], '')] steps: - template: build.yml - - script: node $(Build.SourcesDirectory)/common/scripts/install-run-rushx.js smoke-test:contoso + - script: node $(Build.SourcesDirectory)/common/scripts/install-run-rushx.js smoke-test:contoso && node $(Build.SourcesDirectory)/common/scripts/install-run-rushx.js smoke-test:todo workingDirectory: $(Build.SourcesDirectory)/packages/typespec-test displayName: "Generate Contoso Code From TypeSpec" diff --git a/.vscode/launch.json b/.vscode/launch.json old mode 100755 new mode 100644 diff --git a/packages/typespec-test/package.json b/packages/typespec-test/package.json index ce7e8d8309..59555f2447 100644 --- a/packages/typespec-test/package.json +++ b/packages/typespec-test/package.json @@ -27,6 +27,7 @@ "scripts": { "smoke-test": "node --max-old-space-size=4096 ./eng/smoke-test.js", "smoke-test:contoso": "node --max-old-space-size=4096 ./eng/smoke-test.js -- contoso", + "smoke-test:todo": "node --max-old-space-size=4096 ./eng/smoke-test.js -- todo_non_branded", "smoke-test:workers": "node ./eng/smoke-test-main.js", "build": "echo skip", "format": "echo skipped", diff --git a/packages/typespec-ts/src/modular/buildProjectFiles.ts b/packages/typespec-ts/src/modular/buildProjectFiles.ts index 4bce4215bd..6736f0cfab 100644 --- a/packages/typespec-ts/src/modular/buildProjectFiles.ts +++ b/packages/typespec-ts/src/modular/buildProjectFiles.ts @@ -74,16 +74,22 @@ export function getModuleExports( function getModelSubpaths(emitterOptions: ModularEmitterOptions) { const outputProject = useContext("outputProject"); const modelFiles = outputProject.getSourceFiles( - path.join(emitterOptions.modularOptions.sourceRoot, `models/**/*.ts`) + path.join( + emitterOptions.modularOptions.sourceRoot.replace(/\\/g, "/"), + `models/**/*.ts` + ) ); const subpath = new Set(); for (const modelFile of modelFiles) { - const filepath = modelFile.getFilePath(); + const filepath = modelFile.getFilePath().replace(/\\/g, "/"); if (!filepath.endsWith("index.ts")) { continue; } subpath.add( - path.relative(emitterOptions.modularOptions.sourceRoot, filepath) + path.relative( + emitterOptions.modularOptions.sourceRoot.replace(/\\/g, "/"), + filepath + ) ); } return Array.from(subpath); diff --git a/packages/typespec-ts/src/modular/buildRootIndex.ts b/packages/typespec-ts/src/modular/buildRootIndex.ts index b5c3e97f1a..d6ce9235d4 100644 --- a/packages/typespec-ts/src/modular/buildRootIndex.ts +++ b/packages/typespec-ts/src/modular/buildRootIndex.ts @@ -234,17 +234,21 @@ function exportModules( folders = project .getDirectories() .filter((dir) => { - const targetPath = join(srcPath, subfolder, moduleName); - return dir.getPath().replace(/\\/g, "/").startsWith(targetPath); + const formattedDir = dir.getPath().replace(/\\/g, "/"); + const targetPath = join(srcPath, subfolder, moduleName).replace( + /\\/g, + "/" + ); + return formattedDir.startsWith(targetPath); }) .map((dir) => { - return dir.getPath(); + return dir.getPath().replace(/\\/g, "/"); }); } else { - folders = [join(srcPath, subfolder, moduleName)]; + folders = [join(srcPath, subfolder, moduleName).replace(/\\/g, "/")]; } for (const folder of folders) { - const apiFilePattern = join(folder, "index.ts"); + const apiFilePattern = join(folder, "index.ts").replace(/\\/g, "/"); const modelsFile = project.getSourceFile(apiFilePattern); if (!modelsFile) { continue; diff --git a/packages/typespec-ts/src/modular/buildSubpathIndex.ts b/packages/typespec-ts/src/modular/buildSubpathIndex.ts index 5c921107e6..75c058098c 100644 --- a/packages/typespec-ts/src/modular/buildSubpathIndex.ts +++ b/packages/typespec-ts/src/modular/buildSubpathIndex.ts @@ -32,17 +32,21 @@ export function buildSubpathIndexFile( folders = emitterOptions.project .getDirectories() .filter((dir) => { - const targetPath = join(srcPath, subfolder, subpath); + const formattedDir = dir.getPath().replace(/\\/g, "/"); + const targetPath = join(srcPath, subfolder, subpath).replace( + /\\/g, + "/" + ); return ( - dir.getPath().replace(/\\/g, "/").startsWith(targetPath) && - !emitterOptions.project.getSourceFile(`${dir}/index.ts`) + formattedDir.startsWith(targetPath) && + !emitterOptions.project.getSourceFile(`${formattedDir}/index.ts`) ); }) .map((dir) => { - return dir.getPath(); + return dir.getPath().replace(/\\/g, "/"); }); } else { - folders = [join(srcPath, subfolder, subpath)]; + folders = [join(srcPath, subfolder, subpath).replace(/\\/g, "/")]; } for (const folder of folders) { const apiFilePattern = diff --git a/packages/typespec-ts/src/modular/emitModels.ts b/packages/typespec-ts/src/modular/emitModels.ts index ab3d55a78d..a76ea94cdd 100644 --- a/packages/typespec-ts/src/modular/emitModels.ts +++ b/packages/typespec-ts/src/modular/emitModels.ts @@ -249,7 +249,8 @@ export function getModelNamespaces( ) { if ( model.clientNamespace.startsWith("Azure.ResourceManager") || - model.clientNamespace.startsWith("Azure.Core") + model.clientNamespace.startsWith("Azure.Core") || + model.crossLanguageDefinitionId === "TypeSpec.Http.File" // filter out the TypeSpec.Http.File model similar like what java does here https://github.com/microsoft/typespec/blob/main/packages/http-client-java/emitter/src/code-model-builder.ts#L2589 ) { return []; }