diff --git a/common/tools/dev-tool/src/commands/samples/publish.ts b/common/tools/dev-tool/src/commands/samples/publish.ts index 7da5e21320dc..4aac9df4afb1 100644 --- a/common/tools/dev-tool/src/commands/samples/publish.ts +++ b/common/tools/dev-tool/src/commands/samples/publish.ts @@ -102,6 +102,28 @@ function createPackageJson(info: SampleGenerationInfo, outputKind: OutputKind): }; } +/** + * Determines whether a module specifier is a package dependency. + * + * A dependency is a module specifier that does not refer to a node builtin and + * is not a relative path. + * + * Absolute path imports are not supported in samples (because the package base + * is not fixed relative to the source file). + * + * @param moduleSpecifier - the string given to `import` or `require` + * @returns - true if `moduleSpecifier` should be considered a reference to a + * node module dependency + */ +function isDependency(moduleSpecifier: string): boolean { + if (nodeBuiltins.includes(moduleSpecifier)) return false; + + // This seems like a reasonable test for "is a relative path" as long as + // absolute path imports are forbidden. + const isRelativePath = /^\.\.?\//.test(moduleSpecifier); + return !isRelativePath; +} + async function processSources( projectInfo: ProjectInfo, sources: string[], @@ -217,7 +239,7 @@ async function processSources( } }), summary: summary ?? fail(`${relativeSourcePath} does not include an @summary tag.`), - importedModules: importedModules.filter((name) => !nodeBuiltins.includes(name)), + importedModules: importedModules.filter(isDependency), usedEnvironmentVariables, azSdkTags }; @@ -328,10 +350,10 @@ async function makeSampleGenerationInfo( packageJson.dependencies[dependency]; if (dependencyVersion === undefined) { log.error( - `Dependency "${dependency}", imported by ${source.filePath}, has an unknown version.` + `Dependency "${dependency}", imported by ${source.filePath}, has an unknown version. (Are you missing "./" for a relative path?)` ); log.error( - "Specify a version for it by including it in the package's `devDependencies`." + `Specify a version for "${dependency}" by including it in the package's "devDependencies".` ); } diff --git a/common/tools/dev-tool/src/util/sampleGenerationInfo.ts b/common/tools/dev-tool/src/util/sampleGenerationInfo.ts index a51db7d79b4b..fc75baea7a88 100644 --- a/common/tools/dev-tool/src/util/sampleGenerationInfo.ts +++ b/common/tools/dev-tool/src/util/sampleGenerationInfo.ts @@ -18,7 +18,9 @@ export const DEFAULT_TYPESCRIPT_CONFIG = { compilerOptions: { target: "ES2018", module: "commonjs", + moduleResolution: "node", + resolveJsonModule: true, esModuleInterop: true, allowSyntheticDefaultImports: true,