Skip to content

Fix package.json dependency migration when moving from Swagger to TypeSpec modular#3811

Draft
Copilot wants to merge 5 commits intomainfrom
copilot/update-package-json-dependencies
Draft

Fix package.json dependency migration when moving from Swagger to TypeSpec modular#3811
Copilot wants to merge 5 commits intomainfrom
copilot/update-package-json-dependencies

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

When tsp-client runs against an existing Swagger-generated package, it preserves package.json, causing generateMetadata: false. The updatePackageFile path was invoked but never migrated @azure/core-client@azure-rest/core-client or updated the full set of modular dependencies, resulting in compile errors in the newly generated modular code.

Changes

  • packages/typespec-ts/src/index.ts — In the else if (hasPackageFile) branch, pass format-specific additional dependencies (e.g. fast-xml-parser: ^4.5.0 when XML serialization is used) to updatePackageFile. Standard monorepo deps like @azure/core-util are no longer passed manually — they are now handled by the canonical getAzureMonorepoDependencies comparison.

  • packages/rlc-common/src/metadata/buildPackageFile.ts — Extended updatePackageFile to handle Swagger→TypeSpec migration for Azure monorepo modular packages:

    • Import and call getAzureMonorepoDependencies to get the canonical expected dependency set
    • Compare existing package.json dependencies against the canonical set; update any dep that is missing or at a wrong version (canonical version wins)
    • Remove @azure/core-client (Swagger/autorest-only dep) when found in modular packages
    • For non-Azure-monorepo modular packages: keep the simpler @azure/core-client@azure-rest/core-client migration
    • LRO dependencies for monorepo packages are handled automatically via the getAzureMonorepoDependencies comparison
  • packages/rlc-common/test/integration/packageJson.spec.ts — Added tests for:

    • Full canonical dep migration (removes @azure/core-client, adds/updates all standard deps to canonical versions)
    • Updates @azure-rest/core-client to canonical version even if it already exists at a different version
    • No-op when all canonical deps are already at correct versions
    • LRO dependencies included when package has LRO operations
    • Format-specific additional deps (e.g. fast-xml-parser) merged into the canonical set
    • Outdated dep versions updated to canonical versions

Example

Before (Swagger-generated package.json, kept by tsp-client):

{
  "dependencies": {
    "@azure/core-client": "^1.9.3",
    "@azure/core-auth": "^1.0.0",
    "@azure/core-rest-pipeline": "^1.5.0",
    "tslib": "^2.6.2"
  }
}

After updatePackageFile runs during modular TypeSpec generation:

{
  "dependencies": {
    "@azure-rest/core-client": "^2.3.1",
    "@azure/core-auth": "^1.9.0",
    "@azure/core-rest-pipeline": "^1.20.0",
    "@azure/core-util": "^1.12.0",
    "@azure/logger": "^1.2.0",
    "tslib": "^2.8.1"
  }
}
Original prompt

This section details on the original issue you should resolve

<issue_title>package.json isn't correctly updated when moving from swagger -> TSP</issue_title>
<issue_description>I noticed this when running a tsp-client locally for an existing package, specifically the package.json had

"@azure/core-client": "^1.9.3",

which caused compile time issues because it was looking for @azure-rest/core-client in the newly generated code. I was able to resolve this by manually replacing the above with:

"@azure-rest/core-client": "^2.3.1",

We have dependencies and devdependencies diffs between autorest-packageJson generator and modular-packageJson generator.
This issue is because tsp-client will not clean-up metadata files, so the option generateMetadata will be false as packageJson exists. So our emitter will not update metadata files.
We need to add an additional step to update these dependencies when @azure/core-client is detected in packageJson in modular generation.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…e-client when moving from Swagger to TypeSpec modular

Co-authored-by: kazrael2119 <98569699+kazrael2119@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix package.json update when migrating from swagger to TSP Fix package.json dependency migration when moving from Swagger to TypeSpec modular Mar 4, 2026
@kazrael2119
Copy link
Member

@copilot when generateMetadata is true, we will call buildPackageFile, in this case, generateMetadata is false, emitter will not run buildPackageFile. so we need additionally update them

…-parser) to updatePackageFile when generateMetadata is false

Co-authored-by: kazrael2119 <98569699+kazrael2119@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 4, 2026

@copilot when generateMetadata is true, we will call buildPackageFile, in this case, generateMetadata is false, emitter will not run buildPackageFile. so we need additionally update ...

Fixed in 9e41525. The else if (hasPackageFile) branch in index.ts now computes and passes the same additional dependencies (@azure/core-util, fast-xml-parser for XML) as the shouldGenerateMetadata branch. updatePackageFile now also accepts and merges those dependencies (without overwriting existing versions), in addition to migrating @azure/core-client@azure-rest/core-client.

@kazrael2119
Copy link
Member

@copilot we need to have a check to compare origin packageJson with the one from getAzureMonorepoDependencies function in typespec-ts/src/index.ts, if they are inconsistent, use the one from getAzureMonorepoDependencies

…es canonical set and update inconsistencies

Co-authored-by: kazrael2119 <98569699+kazrael2119@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 4, 2026

@copilot we need to have a check to compare origin packageJson with the one from getAzureMonorepoDependencies function in typespec-ts/src/index.ts, if they are inconsistent, use the one from getAzur...

Done in 1b63394. updatePackageFile now calls getAzureMonorepoDependencies (with the model + any additional deps like fast-xml-parser) to get the canonical expected dependency set, then compares against the existing package.json. Any dep that's missing or has a different version is updated to the canonical version. @azure/core-client (Swagger-only dep) is also removed. The manual @azure/core-util addition in index.ts was removed since it's already included in getAzureMonorepoDependencies.

Co-authored-by: kazrael2119 <98569699+kazrael2119@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

package.json isn't correctly updated when moving from swagger -> TSP

2 participants