Skip to content

[dev-tool] Fix samples-dev cross-imports under Node strip-types -- from copilot#38825

Draft
jeremymeng wants to merge 1 commit into
Azure:mainfrom
jeremymeng:fix/samples-dev-ts-imports-38822
Draft

[dev-tool] Fix samples-dev cross-imports under Node strip-types -- from copilot#38825
jeremymeng wants to merge 1 commit into
Azure:mainfrom
jeremymeng:fix/samples-dev-ts-imports-38822

Conversation

@jeremymeng
Copy link
Copy Markdown
Member

Fixes #38822

Problem

After #37255 migrated dev-tool to run TypeScript natively via Node's strip-types loader, dev-tool samples run started failing with ERR_MODULE_NOT_FOUND on any sample that imports a sibling helper:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '.../samples-dev/utils/helpers.js' imported from .../samples-dev/createAttestationClient.ts

Node's strip-types loader requires .js import specifiers to resolve to a real .js file on disk, but only .ts files exist in samples-dev/. The previous tsx loader rewrote the extensions transparently; Node strip-types does not.

Fix

  • Rewrite all 47 affected samples-dev/**/*.ts files to use .ts relative import specifiers (which Node strip-types accepts natively).
  • Add allowImportingTsExtensions: true to tsconfig.samples.base.json so samples-dev/ continues to type-check cleanly.
  • Teach dev-tool samples publish to rewrite .ts.js for both the TypeScript and JavaScript camera-ready outputs:
    • generation.ts – new rewriteRelativeTsImports helper invoked from postProcess.
    • transforms.ts – new convertTsExtensionToJs helper wired into importDeclarationToCommonJs.
    • generation.tscreateTsconfig now strips allowImportingTsExtensions so the published samples/.../tsconfig.json still compiles with stock tsc.
  • Teach samples processor to treat a relative .ts path as also satisfying the "imported by any other module" rule.

Verification

  • dev-tool samples run samples-dev no longer fails with ERR_MODULE_NOT_FOUND (verified on @azure/attestation).
  • dev-tool samples publish -f on affected packages produces import lines that are byte-identical to what's already committed in samples/ (TS uses ./utils/helpers.js, JS uses require("./utils/helpers.js")) — so no regeneration of the published samples/ is required as part of this PR.
  • All 45 dev-tool unit tests pass; tsc clean; lint clean.

Notes

  • I deliberately did not regenerate the committed samples/ folders for the affected packages — running publish against the current main shows pre-existing drift (formatting changes, removed files, etc.) that is unrelated to this fix.

Fixes Azure#38822

After PR Azure#37255 migrated dev-tool to run TypeScript natively via Node's strip-types loader, `dev-tool samples run` failed with ERR_MODULE_NOT_FOUND on any sample that imports a sibling helper. Node's strip-types loader requires `.js` import specifiers to resolve to a real `.js` file on disk, but only `.ts` files exist in `samples-dev/`.

This change:

* Rewrites all 47 affected `samples-dev/**/*.ts` files to use `.ts` relative import specifiers (which Node strip-types accepts natively).

* Adds `allowImportingTsExtensions: true` to `tsconfig.samples.base.json` so `samples-dev/` type-checks cleanly.

* Teaches `dev-tool samples publish` to rewrite `.ts` -> `.js` for both the TypeScript and JavaScript camera-ready outputs (so the published `samples/` folder remains byte-identical for those import lines).

* Strips `allowImportingTsExtensions` from the generated `samples/.../tsconfig.json` so the published TypeScript samples still compile with stock `tsc`.

* Updates the `samples processor` to treat a relative `.ts` path as also satisfying the `imported by any other module` rule.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actions github-actions Bot added App Configuration Azure.ApplicationModel.Configuration Attestation Cognitive - Form Recognizer Cosmos dev-tool Issues related to the Azure SDK for JS dev-tool Event Hubs Search Storage Storage Service (Queues, Blobs, Files) labels Jun 4, 2026
@jeremymeng jeremymeng marked this pull request as draft June 4, 2026 18:52
@jeremymeng jeremymeng changed the title [dev-tool] Fix samples-dev cross-imports under Node strip-types [dev-tool] Fix samples-dev cross-imports under Node strip-types -- from copilot Jun 4, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes dev-tool samples run failures introduced by running dev-tool TypeScript natively via Node’s strip-types loader by aligning samples-dev relative import specifiers with what Node can resolve at runtime, while preserving the existing published samples/ output shape.

Changes:

  • Update affected sdk/**/samples-dev/**/*.ts files to use relative *.ts import specifiers (instead of *.js) so Node’s TS loader can resolve sibling modules.
  • Update sample publishing (dev-tool samples publish) to rewrite relative *.ts specifiers back to *.js in the emitted camera-ready TypeScript and JavaScript outputs.
  • Update samples processing/type-checking configuration to allow .ts import extensions and to treat .ts paths as satisfying the “imported by another module” rule.

Reviewed changes

Copilot reviewed 52 out of 52 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tsconfig.samples.base.json Allows .ts extensions in import specifiers while type-checking samples-dev/.
sdk/storage/storage-blob/samples-dev/snapshots.ts Switch relative sibling import to .ts for Node TS loader compatibility.
sdk/storage/storage-blob/samples-dev/errorsAndResponses.ts Switch relative sibling import to .ts for Node TS loader compatibility.
sdk/search/search-documents/samples-dev/vectorSearch.ts Switch relative imports (including type-only) to .ts.
sdk/search/search-documents/samples-dev/stickySession.ts Switch relative imports (including type-only) to .ts.
sdk/search/search-documents/samples-dev/setup.ts Switch relative type-only import to .ts.
sdk/search/search-documents/samples-dev/searchClientOperations.ts Switch relative imports (including type-only) to .ts.
sdk/search/search-documents/samples-dev/bufferedSenderManualFlush.ts Switch relative imports (including type-only) to .ts.
sdk/search/search-documents/samples-dev/bufferedSenderAutoFlushTimer.ts Switch relative imports (including type-only) to .ts.
sdk/search/search-documents/samples-dev/bufferedSenderAutoFlushSize.ts Switch relative imports (including type-only) to .ts.
sdk/formrecognizer/ai-form-recognizer/samples-dev/readDocument.ts Switch relative imports to .ts.
sdk/formrecognizer/ai-form-recognizer/samples-dev/extractLayout.ts Switch relative import to .ts.
sdk/formrecognizer/ai-form-recognizer/samples-dev/extractGeneralDocument.ts Switch relative import to .ts.
sdk/formrecognizer/ai-form-recognizer/samples-dev/analyzeW2TaxForm.ts Switch relative import to .ts.
sdk/formrecognizer/ai-form-recognizer/samples-dev/analyzeReceipt.ts Switch relative import to .ts.
sdk/formrecognizer/ai-form-recognizer/samples-dev/analyzeInvoice.ts Switch relative import to .ts.
sdk/formrecognizer/ai-form-recognizer/samples-dev/analyzeIdentityDocument.ts Switch relative import to .ts.
sdk/formrecognizer/ai-form-recognizer/samples-dev/analyzeBusinessCard.ts Switch relative import to .ts.
sdk/eventhub/eventhubs-checkpointstore-blob/samples-dev/receiveEventsWithApiSpecificStorage.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ThroughputBucket.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ServerSideScripts.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/SasTokenAuth.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/Query/FullTextSearch.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/PriorityLevel.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ItemManagement.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/IndexManagement.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/HierarchicalPartitioning.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ExecuteBulkOperations.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ExcludedLocations.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/EntraAuth.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/Diagnostics.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/DatabaseManagement.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ContainerManagement.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ClientSideEncryption.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ChangeFeedIterator/ChangeFeedIteratorLatestVersion.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ChangeFeedIterator/ChangeFeedIteratorAllVersionsAndDeletes.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ChangeFeedIterator/ChangeFeedHierarchicalPartitionKey.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/ChangeFeed.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/BulkUpdateWithSproc.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/Bulk.ts Switch relative import to .ts.
sdk/cosmosdb/cosmos/samples-dev/AlterQueryThroughput.ts Switch relative import to .ts.
sdk/attestation/attestation/samples-dev/setAttestationPolicy.ts Switch relative imports to .ts.
sdk/attestation/attestation/samples-dev/modifyPolicyManagementCertificates.ts Switch relative imports to .ts.
sdk/attestation/attestation/samples-dev/getPolicyManagementCertificates.ts Switch relative import to .ts.
sdk/attestation/attestation/samples-dev/getAttestationPolicy.ts Switch relative import to .ts.
sdk/attestation/attestation/samples-dev/createAttestationClient.ts Switch relative import to .ts.
sdk/attestation/attestation/samples-dev/attestEnclaves.ts Switch relative imports to .ts.
sdk/appconfiguration/app-configuration/samples-dev/updateSyncTokenSample.ts Switch relative import to .ts.
sdk/ai/ai-projects/samples-dev/agents/tools/agentComputerUse.ts Switch relative import to .ts.
common/tools/dev-tool/src/util/samples/transforms.ts Rewrite .ts relative specifiers to .js when converting imports to CommonJS require(...).
common/tools/dev-tool/src/util/samples/processor.ts Treat .ts relative paths as satisfying the “imported by any other module” rule.
common/tools/dev-tool/src/util/samples/generation.ts Rewrite .ts specifiers to .js during publish post-processing and strip allowImportingTsExtensions from published tsconfig.json.

Comment on lines +347 to +351
function rewriteRelativeTsImports(content: string): string {
return content.replace(
/(\b(?:import|export|from)\s*\(?\s*)(['"])(\.\.?\/[^'"]*?)\.ts(['"])/g,
"$1$2$3.js$4",
);
Comment on lines +329 to +352
/**
* Rewrites relative `.ts` import/export specifiers in TypeScript sample source
* text to use `.js` extensions.
*
* Samples authored under `samples-dev/` use `.ts` extensions on relative imports
* so that Node's native TypeScript loader can resolve sibling sample files when
* developers run them directly. Published TypeScript samples, however, are
* compiled with `tsc` into `dist/` and must reference the emitted `.js` files
* to be runnable. This helper performs that rewrite on the copied sample text.
*
* Matches the following forms:
* - `import ... from "./foo.ts"`
* - `import "./foo.ts"`
* - `import("./foo.ts")` (dynamic import)
* - `export ... from "./foo.ts"`
*
* @param content - the raw module source text
*/
function rewriteRelativeTsImports(content: string): string {
return content.replace(
/(\b(?:import|export|from)\s*\(?\s*)(['"])(\.\.?\/[^'"]*?)\.ts(['"])/g,
"$1$2$3.js$4",
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

App Configuration Azure.ApplicationModel.Configuration Attestation Cognitive - Form Recognizer Cosmos dev-tool Issues related to the Azure SDK for JS dev-tool Event Hubs Search Storage Storage Service (Queues, Blobs, Files)

Projects

Status: Untriaged

Development

Successfully merging this pull request may close these issues.

[EngSys] execute samples failures when samples import from other module

2 participants