Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime error when using as configPlugin with "declarationMap": true #310

Closed
agilgur5 opened this issue Apr 20, 2022 · 3 comments · Fixed by #334
Closed

Runtime error when using as configPlugin with "declarationMap": true #310

agilgur5 opened this issue Apr 20, 2022 · 3 comments · Fixed by #334
Assignees
Labels
kind: regression Specific type of bug -- past behavior that worked is now broken scope: configPlugin Related to usage as a configPlugin for reading rollup.config.ts (vs. regular plugin) solution: workaround available There is a workaround available for this issue

Comments

@agilgur5
Copy link
Collaborator

agilgur5 commented Apr 20, 2022

What happens and why it is wrong

A runtime error occurs when your tsconfig.json has "declarationMap": true and you use this plugin as a configPlugin.

Per the error below, it occurs on this line in the code. Specifically, it appears that declarationDir is undefined because in the case of a configPlugin, Rollup's output isn't set what-so-ever, so output.file and output.dir are both undefined (see below for verbosity that verifies this).

Ironically, I actually wrote this code myself in #221 so I could write a PR to fix this myself. I think a default declarationDir equivalent to cwd would probably suffice for this -- let me know if you think a different solution makes sense, otherwise I could write that up pretty quickly.

To be specific, rpts2 only errors when declarationMap is turned on, everything else doesn't seem to be impacted, so things like emitFile etc still work when used as a configPlugin.
Current workaround is to disable declarationMap in tsconfig (or tsconfigOverrides) specifically when using as a configPlugin only.

Error:

$ rollup -c rollup.config.ts --configPlugin rollup-plugin-typescript2
[!] (plugin rpt2) TypeError: The "from" argument must be of type string. Received undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received undefined
    at new NodeError (internal/errors.js:322:7)
    at validateString (internal/validators.js:124:11)
    at Object.relative (path.js:1167:5)
    at <project>/node_modules/rollup-plugin-typescript2/src/index.ts:383:25
    at Array.map (<anonymous>)
    at emitDeclaration (<project>/node_modules/rollup-plugin-typescript2/src/index.ts:380:47)
    at <project>/node_modules/rollup-plugin-typescript2/src/index.ts:401:5
    at <project>/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:4967:15
    at baseForOwn (<project>/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:3032:24)
    at <project>/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:4936:18

Environment

Versions
  npmPackages:
    rollup: ^2.70.2 => 2.70.2 
    rollup-plugin-typescript2: ^0.31.2 => 0.31.2 
    typescript: ^4.6.3 => 4.6.3 

rollup.config.ts

:

My rollup config actually isn't relevant as I'm actually not using rpts2 in the rollup config, just for parsing the config itself (as I wrote it in TS)

tsconfig.json

:

(slightly simplified version as I actually have multiple tsconfigs that extend each other)

{
  // only build the rollup config for this parse through
  "files": ["rollup.config.ts"],
  // https://github.com/tsconfig/bases
  "extends": "@tsconfig/strictest/tsconfig.json",
  // see https://www.typescriptlang.org/tsconfig to better understand tsconfigs
  "compilerOptions": {
    // output to dist/ dir
    "outDir": "dist/",
    // output .d.ts declaration files for consumers
    "declaration": true,
    // output .d.ts.map declaration map files for consumers
    "declarationMap": true,
    // output .js.map sourcemap files for consumers
    "sourceMap": true,
    // use Node's module resolution algorithm, instead of the legacy TS one
    "moduleResolution": "node",
    // resolve JSON files
    "resolveJsonModule": true,
    // transpile JSX to React.createElement
    "jsx": "react",
    // ignored during builds, but commonly used when type-checking with `tsc`
    "noEmit": true,
  }
}

package.json

:

Think the envinfo output above is good enough; I've found the error in any case.

plugin output with verbosity 3

:
rpt2: built-in options overrides: {
    "noEmitHelpers": false,
    "importHelpers": true,
    "noResolve": false,
    "noEmit": false,
    "inlineSourceMap": false,
    "outDir": "<project>/node_modules/.cache/rollup-plugin-typescript2/placeholder",
    "moduleResolution": 2,
    "allowNonTsExtensions": true,
    "module": 5
}
rpt2: parsed tsconfig: {
    "options": {
        "strict": true,
        "allowUnusedLabels": false,
        "allowUnreachableCode": false,
        "exactOptionalPropertyTypes": true,
        "noFallthroughCasesInSwitch": true,
        "noImplicitOverride": true,
        "noImplicitReturns": true,
        "noPropertyAccessFromIndexSignature": true,
        "noUncheckedIndexedAccess": true,
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "importsNotUsedAsValues": 2,
        "checkJs": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "outDir": "<project>/node_modules/.cache/rollup-plugin-typescript2/placeholder",
        "declaration": true,
        "declarationMap": true,
        "sourceMap": true,
        "moduleResolution": 2,
        "resolveJsonModule": true,
        "jsx": 2,
        "noEmit": false,
        "configFilePath": "<project>/tsconfig.json",
        "noEmitHelpers": false,
        "importHelpers": true,
        "noResolve": false,
        "inlineSourceMap": false,
        "allowNonTsExtensions": true,
        "module": 5
    },
    "fileNames": [
        "<project>/rollup.config.ts"
    ],
    "typeAcquisition": {
        "enable": false,
        "include": [],
        "exclude": []
    },
    "raw": {
        "extends": "@tsconfig/strictest/tsconfig.json",
        "exclude": [
            "node_modules/",
            "dist/",
            "coverage/",
            "example/"
        ],
        "compilerOptions": {
            "outDir": "dist/",
            "declaration": true,
            "declarationMap": true,
            "sourceMap": true,
            "moduleResolution": "node",
            "resolveJsonModule": true,
            "jsx": "react",
            "noEmit": true
        },
        "files": [
            "rollup.config.ts"
        ],
        "compileOnSave": false
    },
    "errors": [],
    "wildcardDirectories": {},
    "compileOnSave": false
}
rpt2: typescript version: 4.6.3
rpt2: tslib version: 2.3.1
rpt2: rollup version: 2.70.2
rpt2: rollup-plugin-typescript2 version: 0.31.2
rpt2: plugin options:
{
    "verbosity": 3,
    "check": true,
    "clean": false,
    "cacheRoot": "<project>/node_modules/.cache/rollup-plugin-typescript2",
    "include": [
        "*.ts+(|x)",
        "**/*.ts+(|x)"
    ],
    "exclude": [
        "*.d.ts",
        "**/*.d.ts"
    ],
    "abortOnError": true,
    "rollupCommonJSResolveHack": false,
    "useTsconfigDeclarationDir": false,
    "transformers": [],
    "tsconfigDefaults": {},
    "objectHashIgnoreUnknownHack": false,
    "cwd": "<project>",
    "typescript": "version 4.6.3"
}
rpt2: rollup config:
{
    "input": "<project>/rollup.config.ts",
    "plugins": [
        {
            "name": "rpt2"
        }
    ],
    "treeshake": false
}
rpt2: tsconfig path: <project>/tsconfig.json
rpt2: included:
[
    "*.ts+(|x)",
    "**/*.ts+(|x)"
]
rpt2: excluded:
[
    "*.d.ts",
    "**/*.d.ts"
]
rpt2: Ambient types:
rpt2:     <project>/node_modules/@types/babel__core/index.d.ts
rpt2:     <project>/node_modules/@types/babel__generator/index.d.ts
rpt2:     <project>/node_modules/@types/babel__template/index.d.ts
rpt2:     <project>/node_modules/@types/babel__traverse/index.d.ts
rpt2:     <project>/node_modules/@types/cheerio/index.d.ts
rpt2:     <project>/node_modules/@types/enzyme/index.d.ts
rpt2:     <project>/node_modules/@types/estree/index.d.ts
rpt2:     <project>/node_modules/@types/graceful-fs/index.d.ts
rpt2:     <project>/node_modules/@types/istanbul-lib-coverage/index.d.ts
rpt2:     <project>/node_modules/@types/istanbul-lib-report/index.d.ts
rpt2:     <project>/node_modules/@types/istanbul-reports/index.d.ts
rpt2:     <project>/node_modules/@types/json5/index.d.ts
rpt2:     <project>/node_modules/@types/node/ts3.2/index.d.ts
rpt2:     <project>/node_modules/@types/parse-json/index.d.ts
rpt2:     <project>/node_modules/@types/prettier/index.d.ts
rpt2:     <project>/node_modules/@types/prop-types/index.d.ts
rpt2:     <project>/node_modules/@types/react/index.d.ts
rpt2:     <project>/node_modules/@types/resolve/index.d.ts
rpt2:     <project>/node_modules/@types/scheduler/index.d.ts
rpt2:     <project>/node_modules/@types/signature_pad/index.d.ts
rpt2:     <project>/node_modules/@types/stack-utils/index.d.ts
rpt2:     <project>/node_modules/@types/yargs/index.d.ts
rpt2:     <project>/node_modules/@types/yargs-parser/index.d.ts
rpt2: ambient types changed, redoing all semantic diagnostics
rpt2: transpiling '<project>/rollup.config.ts'
rpt2:     cache: '<project>/node_modules/.cache/rollup-plugin-typescript2/rpt2_886b5474ceab46316c300049e28ace720638ad6b/code/cache/8ff12b6a0f6d14ec54898a8a47dd4d86c8c3c981'
rpt2:     cache miss
rpt2:     cache: '<project>/node_modules/.cache/rollup-plugin-typescript2/rpt2_886b5474ceab46316c300049e28ace720638ad6b/syntacticDiagnostics/cache/8ff12b6a0f6d14ec54898a8a47dd4d86c8c3c981'
rpt2:     cache miss
rpt2:     cache: '<project>/node_modules/.cache/rollup-plugin-typescript2/rpt2_886b5474ceab46316c300049e28ace720638ad6b/semanticDiagnostics/cache/8ff12b6a0f6d14ec54898a8a47dd4d86c8c3c981'
rpt2:     cache miss
rpt2: generated declarations for '<project>/rollup.config.ts'
rpt2: generating target 1
rpt2: rolling caches
rpt2: emitting declarations for '<project>/rollup.config.ts' to 'rollup.config.d.ts'
[!] (plugin rpt2) TypeError: The "from" argument must be of type string. Received undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "from" argument must be of type string. Received undefined
    at new NodeError (internal/errors.js:322:7)
    at validateString (internal/validators.js:124:11)
    at Object.relative (path.js:1167:5)
    at <project>/node_modules/rollup-plugin-typescript2/src/index.ts:383:25
    at Array.map (<anonymous>)
    at emitDeclaration (<project>/node_modules/rollup-plugin-typescript2/src/index.ts:380:47)
    at <project>/node_modules/rollup-plugin-typescript2/src/index.ts:401:5
    at <project>/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:4967:15
    at baseForOwn (<project>/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:3032:24)
    at <project>/node_modules/rollup-plugin-typescript2/node_modules/lodash/lodash.js:4936:18
agilgur5 added a commit to agilgur5/react-signature-canvas that referenced this issue Apr 20, 2022
- fancy source maps for declarations

- apparently I left these out of `ts-library-base` so they didn't get
  copied here either
  - but most of my tsconfig there was copied too, so I suspect I left
    it out either because of @wessberg/rollup-plugin-ts's bugs with it
    or because TSDX previously had bugs with it
    - c.f. ezolenko/rollup-plugin-typescript2#221
      and the worse downstream version I had written first:
      jaredpalmer/tsdx#488

- Unfortunately I did encounter a small error with declaration maps when
  using rpts2 as a configPlugin, due to an unexpected edge case in code
  I wrote myself in the above PR
  - wrote up an issue for it and will probably PR it myself too:
    ezolenko/rollup-plugin-typescript2#310
  - as a workaround, I wrote a small `tsconfig.rollup.json` to be used
    with rpts2 as a configPlugin that disables `declarationMap`
    - and also adds some optimizations over my base tsconfig
    - took me a bit to figure out how to use options with configPlugins,
      as that was one of the reasons I didn't use @rollup/plugin-babel
      as the configPlugin
      - I still can't get it to read my `babel.config.js` even with
        options as it has no option for this (it auto-detects it) :/
agilgur5 added a commit to agilgur5/react-signature-canvas that referenced this issue Apr 20, 2022
- fancy source maps for declarations

- apparently I left these out of `ts-library-base` so they didn't get
  copied here either
  - but most of my tsconfig there was copied too, so I suspect I left
    it out either because of @wessberg/rollup-plugin-ts's bugs with it
    or because TSDX previously had bugs with it
    - c.f. ezolenko/rollup-plugin-typescript2#221
      and the worse downstream version I had written first:
      jaredpalmer/tsdx#488

- Unfortunately I did encounter a small error with declaration maps when
  using rpts2 as a configPlugin, due to an unexpected edge case in code
  I wrote myself in the above PR
  - wrote up an issue for it and will probably PR it myself too:
    ezolenko/rollup-plugin-typescript2#310
  - as a workaround, I wrote a small `tsconfig.rollup.json` to be used
    with rpts2 as a configPlugin that disables `declarationMap`
    - and also adds some optimizations over my base tsconfig
    - took me a bit to figure out how to use options with configPlugins,
      as that was one of the reasons I didn't use @rollup/plugin-babel
      as the configPlugin
      - I still can't get it to read my `babel.config.js` even with
        options as it has no option for this (it auto-detects it) :/
@ezolenko
Copy link
Owner

Does this work if you set useTsconfigDeclarationDir in plugin options to true?

@agilgur5
Copy link
Collaborator Author

agilgur5 commented Apr 20, 2022

As expected (since the erroring code is in the else when useTsconfigDeclarationDir is falsey), it does indeed work. I thought it might cause TS to actually write stuff since it's no longer controlled by Rollup in that case, but I think since I have noEmit set, it actually didn't output a rollup.config.js etc.
So that's another viable workaround if one has noEmit in their tsconfig set (if they're using rpts2 both as plugin and configPlugin, they may not).

Think it would still be good to get a real fix in as this is an actual unexpected edge case that my code didn't handle

@agilgur5 agilgur5 self-assigned this Apr 23, 2022
@agilgur5 agilgur5 added kind: regression Specific type of bug -- past behavior that worked is now broken solution: workaround available There is a workaround available for this issue labels Apr 23, 2022
@agilgur5 agilgur5 changed the title TypeError: The "from" argument must be of type string. Received undefined when using as configPlugin with "declarationMap": true Runtime error when using as configPlugin with "declarationMap": true Apr 30, 2022
@agilgur5
Copy link
Collaborator Author

agilgur5 commented Jun 2, 2022

Released in 0.32.0 🎉

Double-checked that the release works in one of my libraries as well 🙂

Repository owner locked as resolved and limited conversation to collaborators Jun 2, 2022
@agilgur5 agilgur5 added the scope: configPlugin Related to usage as a configPlugin for reading rollup.config.ts (vs. regular plugin) label Sep 4, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: regression Specific type of bug -- past behavior that worked is now broken scope: configPlugin Related to usage as a configPlugin for reading rollup.config.ts (vs. regular plugin) solution: workaround available There is a workaround available for this issue
Projects
None yet
2 participants