[dev-tool] Fix samples-dev cross-imports under Node strip-types -- from copilot#38825
Draft
jeremymeng wants to merge 1 commit into
Draft
[dev-tool] Fix samples-dev cross-imports under Node strip-types -- from copilot#38825jeremymeng wants to merge 1 commit into
jeremymeng wants to merge 1 commit into
Conversation
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>
Contributor
There was a problem hiding this comment.
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/**/*.tsfiles to use relative*.tsimport specifiers (instead of*.js) so Node’s TS loader can resolve sibling modules. - Update sample publishing (
dev-tool samples publish) to rewrite relative*.tsspecifiers back to*.jsin the emitted camera-ready TypeScript and JavaScript outputs. - Update samples processing/type-checking configuration to allow
.tsimport extensions and to treat.tspaths 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", | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #38822
Problem
After #37255 migrated
dev-toolto run TypeScript natively via Node's strip-types loader,dev-tool samples runstarted failing withERR_MODULE_NOT_FOUNDon any sample that imports a sibling helper:Node's strip-types loader requires
.jsimport specifiers to resolve to a real.jsfile on disk, but only.tsfiles exist insamples-dev/. The previoustsxloader rewrote the extensions transparently; Node strip-types does not.Fix
samples-dev/**/*.tsfiles to use.tsrelative import specifiers (which Node strip-types accepts natively).allowImportingTsExtensions: truetotsconfig.samples.base.jsonsosamples-dev/continues to type-check cleanly.dev-tool samples publishto rewrite.ts→.jsfor both the TypeScript and JavaScript camera-ready outputs:generation.ts– newrewriteRelativeTsImportshelper invoked frompostProcess.transforms.ts– newconvertTsExtensionToJshelper wired intoimportDeclarationToCommonJs.generation.ts–createTsconfignow stripsallowImportingTsExtensionsso the publishedsamples/.../tsconfig.jsonstill compiles with stocktsc.samples processorto treat a relative.tspath as also satisfying the "imported by any other module" rule.Verification
dev-tool samples run samples-devno longer fails withERR_MODULE_NOT_FOUND(verified on@azure/attestation).dev-tool samples publish -fon affected packages produces import lines that are byte-identical to what's already committed insamples/(TS uses./utils/helpers.js, JS usesrequire("./utils/helpers.js")) — so no regeneration of the publishedsamples/is required as part of this PR.dev-toolunit tests pass;tscclean; lint clean.Notes
samples/folders for the affected packages — running publish against the currentmainshows pre-existing drift (formatting changes, removed files, etc.) that is unrelated to this fix.