microsoft / rushstack

Monorepo for tools developed by the Rush Stack community
https://rushstack.io/
Other
5.93k stars 595 forks source link

[api-extractor] Unable to analyze the export #1972

Open miguelramos opened 4 years ago

miguelramos commented 4 years ago

Is this a feature or a bug?

Please describe the actual behavior. Try to export a typescript type importing from one file and exported from other file(index).

Log Stack:

 Error: Internal Error: Unable to analyze the export "Predicate" in
/Users/miguelramos/Public/workspace/prz-kit/dist/packages/support/@types/helpers.d.ts

You have encountered a software defect. Please consider reporting the issue to the maintainers of this application.
    at ExportAnalyzer._getExportOfAstModule (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/ExportAnalyzer.ts:643:13)
    at ExportAnalyzer._getExportOfSpecifierAstModule (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/ExportAnalyzer.ts:631:39)
    at ExportAnalyzer._tryMatchExportDeclaration (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/ExportAnalyzer.ts:448:21)
    at ExportAnalyzer.fetchReferencedAstEntity (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/ExportAnalyzer.ts:360:35)
    at ExportAnalyzer._tryGetExportOfAstModule (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/ExportAnalyzer.ts:678:26)
    at ExportAnalyzer._getExportOfAstModule (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/ExportAnalyzer.ts:637:51)
    at /Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/ExportAnalyzer.ts:322:53
    at Map.forEach (<anonymous>)
    at ExportAnalyzer._collectAllExportsRecursive (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/ExportAnalyzer.ts:313:40)
    at ExportAnalyzer.fetchAstModuleExportInfo (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/ExportAnalyzer.ts:253:12)
    at AstSymbolTable.fetchAstModuleExportInfo (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/analyzer/AstSymbolTable.ts:127:33)
    at Collector.analyze (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/collector/Collector.ts:233:74)
    at Function.invoke (/Users/miguelramos/Public/workspace/prz-kit/node_modules/@microsoft/api-extractor/src/api/Extractor.ts:222:15)
    at Object.<anonymous> (/Users/miguelramos/Public/workspace/prz-kit/tools/packages/gulp/build-tasks-gulp.ts:127:56)
    at step (/Users/miguelramos/Public/workspace/prz-kit/tools/packages/gulp/build-tasks-gulp.ts:34:23)
    at Object.next (/Users/miguelramos/Public/workspace/prz-kit/tools/packages/gulp/build-tasks-gulp.ts:15:53)

If the issue is a bug, how can we reproduce it? Please provide detailed steps and include a GitHub branch if applicable. Your issue will get resolved faster if you can make it easy to investigate. My files:

helpers.ts

/**
 * A type which represents a function that returns a Boolean value.
 *
 * @example
 * ```ts-no-run
 * const isGreaterThanTen: Predicate = (num: number) => num > 10;
 * isGreaterThanTen(2); // false
 * isGreaterThanTen(20); // true
 * ```
 * @public
 */

export type Predicate = <T>(value: T) => boolean;

index.ts

export { Predicate } from './helpers';

What is the expected behavior? Type being on dts file.

If this is a bug, please provide the tool version, Node.js version, and OS.

octogonz commented 4 years ago

@miguelramos Could you provide a more complete repro? I tried with the files you shared, but I didn't encounter any error.

The ideal would be a complete branch with a tsconfig.json file and instructions for building. Thanks!

miguelramos commented 4 years ago

Hey @octogonz sorry for the delay, but this is a private company project but i can share the tsconfig files. This is a monorepo (lerna), with gulp, rollup and babel.

tsconfig.json

{
  "compilerOptions": {
    "composite": true,
    "target": "es5",
    "module": "es2015",
    "lib": [
      "es2015",
      "es2016",
      "es2017",
      "es2018",
      "dom",
      "dom.iterable"
    ],
    "allowJs": true,
    "jsx": "preserve",
    "sourceMap": true,
    "outDir": "./dist",
    "downlevelIteration": true,
    "strict": true,
    "noUnusedLocals": true,
    "noImplicitReturns": true,
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "@company/support/*": ["packages/support"],
      "@company/support": ["packages/support/main.ts"],
      "@company/ui/*": ["packages/ui"],
      "@company/ui": ["packages/ui/index.ts"],
      "@company/composition/*": ["packages/composition"],
      "@company/data-query/*": ["packages/data-query"],
      "@company/data-query": ["packages/data-query/main.ts"],
      "@company/http": ["packages/http/index.ts"],
    },
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  },
  "exclude": [
    "**/*.spec.ts",
    "**/*.spec.js",
    "node_modules",
    "dist"
  ]
}

tsconfig.build.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "module": "esnext",
    "outDir": "dist",
    "moduleResolution": "node",
    "baseUrl": "./",
    "preserveSymlinks": true,
    "declaration": true,
    "sourceMap": true,
    "declarationMap": true,
    "declarationDir": "types",
    "allowSyntheticDefaultImports": true,
    "jsx": "preserve",
    "jsxFactory": "h",
    "rootDir": ".",
    "strict": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "types": ["jest", "webpack-env", "node"]
  },
  "exclude": ["./tsconfig.build.json", "./scripts"],
  "include": ["./**/*.ts", "./**/*.tsx", "./**/*.d.ts"]
}

babel config in gulp

babel({
        exclude: 'node_modules/**',
        extensions: ['.js', '.jsx', '.ts', '.tsx', '.vue'],
        include: [join(settings.sourceDir, '**/*')],
        plugins: [
          '@babel/plugin-syntax-jsx',
          [
            '@babel/plugin-proposal-decorators',
            {
              legacy: true
            }
          ],
          '@babel/plugin-proposal-class-properties'
        ],
        presets: [
          [
            '@babel/preset-env',
            {
              modules: false
            }
          ],
          '@babel/preset-typescript',
          '@vue/cli-plugin-babel/preset'
        ],
        // externalHelpers: true,
        runtimeHelpers: true
      }),

api-extractor.json

{
  "$schema": "https://developer.microsoft.com/json-schemas/api-extractor/v7/api-extractor.schema.json",
  "projectFolder": "./",
  "mainEntryPointFilePath": "../../dist/packages/support/@types/main.d.ts",
  "bundledPackages": [],
  "compiler": {
    "skipLibCheck": true,
    "tsconfigFilePath": "./tsconfig.build.json"
  },
  "apiReport": {
    "enabled": true,
    "reportFileName": "support.api.md",
    "reportFolder": "../../dist/packages/support/docs/"
  },
  "docModel": {
    "enabled": true
  },
  "dtsRollup": {
    "enabled": true,
    "untrimmedFilePath": "../../dist/packages/support/support-raw.d.ts",
    "betaTrimmedFilePath": "../../dist/packages/support/support-beta.d.ts",
    "publicTrimmedFilePath": "../../dist/packages/support/index.d.ts"
  },
  "tsdocMetadata": {
    "enabled": true,
    "tsdocMetadataFilePath": "../../dist/packages/support/tsdoc-metadata.json"
  },
  "messages": {
    "compilerMessageReporting": {
      "default": {
        "logLevel": "warning"
      }
    },
    "extractorMessageReporting": {
      "default": {
        "logLevel": "warning"
      }
    },
    "tsdocMessageReporting": {
      "default": {
        "logLevel": "warning"
      }
    }
  }
}
octogonz commented 4 years ago

Hey @octogonz sorry for the delay, but this is a private company project but i can share the tsconfig files. This is a monorepo (lerna), with gulp, rollup and babel.

What does API Extractor log with the --diagnostics CLI parameter?

The diagnostic log shows all the input files that API Extractor reads during analysis, which will be mostly .d.ts files without any implementation details in them. The .js and .ts files are generally NOT needed to repro a crash. So if you are not able to share your full source code, maybe you could share only the .d.ts files (e.g. with comments deleted)? (If you want to send a file to me privately, you can use Gitter or Twitter.)

It is difficult to debug this without more to go on.

miguelramos commented 4 years ago

@octogonz sorry for the delay. After a long dig on my setup i've realised that rollup tooling with babel plugin was messing up with final compilation, so i've striped out files and compiled with tsc on a testing project and all good, so tried to undersant what rollup plugin was doing, and yes order is important on toolings like this. I fixed the issua and all good now. Sorry for the inconvenience, and thanks for the tips.

benmccann commented 3 years ago

@octogonz there was a duplicate issue filed for this in https://github.com/microsoft/rushstack/issues/2220, which contains a reproduction. I can provide a reproduction as well if you're not able to use that one for whatever reason

octogonz commented 3 years ago

@benmccann I just looked at the repro from #2220, however it seems unrelated to your #1972. Your issue may be unrelated to both of these bugs. Even though they all crash in _getExportOfAstModule(), the underlying reasons could be somewhat different.