generator icon indicating copy to clipboard operation
generator copied to clipboard

[BUG] Generator constructor allows missing targetDir when output is fs, causing TypeError

Open devxdh opened this issue 4 weeks ago • 4 comments

Describe the bug.

In @asyncapi/generator, targetDir is documented as an optional parameter only when using in-memory string output (output: 'string'). When generating files to the filesystem (default output: 'fs'), targetDir is required.

However, if an entrypoint option is passed in the constructor options, the constructor currently allows targetDir to be omitted or passed as undefined even when output defaults to 'fs':

const generator = new Generator('my-template', undefined, { entrypoint: 'file.js' });

This passes constructor validation without throwing. Later, when generator.generate() is called, setupFSOutput() executes xfs.mkdirpSync(this.targetDir) and crashes with an unhandled TypeError:

TypeError [ERR_INVALID_ARG_TYPE]: The "paths[0]" argument must be of type string. Received undefined
    at Object.resolve (node:path)
    at Generator.setupFSOutput (apps/generator/lib/generator.js:250:9)

What causes this:

In apps/generator/lib/generator.js#L93-L95:

if (!templateName) throw new Error('No template name has been specified.');
if (!entrypoint && !targetDir) throw new Error('No target directory has been specified.');
if (!['fs', 'string'].includes(output)) throw new Error(`Invalid output type ${output}. Valid values are 'fs' and 'string'.`);

The check if (!entrypoint && !targetDir) assumes that if entrypoint is provided, targetDir is optional. But targetDir is only optional if output === 'string'. When output === 'fs', filesystem generation always requires a target directory.

Expected behavior

The constructor should fail-fast with a clear error: Error: No target directory has been specified. whenever output === 'fs' and targetDir is missing or undefined.

Screenshots

No response

How to Reproduce

Option 1: 1-line command in terminal from repo root:

node -e "const Generator = require('./apps/generator/lib/generator'); const path = require('path'); new Generator(path.resolve('apps/generator/test/test-templates/react-template'), undefined, { entrypoint: 'test-file.md.js' }).generateFromFile(path.resolve('apps/generator/test/docs/dummy.yml')).catch(err => console.error('CRASHED:', err.message));"

Observed Output:

CRASHED: The "paths[0]" argument must be of type string. Received undefined

Option 2: Calling setupFSOutput() directly on an instance:

node -e "const Generator = require('./apps/generator/lib/generator'); new Generator('my-template', undefined, { entrypoint: 'index.js' }).setupFSOutput().catch(err => console.error('CRASHED:', err.message));"

🖥️ Device Information [optional]

  • Operating System (OS): Fedora Linux Workstation 44
  • Browser: Chrome
  • Browser Version: 154

👀 Have you checked for similar open issues?

  • [x] I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

Yes I am willing to submit a PR!

devxdh avatar Nov 06 '26 16:11 devxdh

The existing constructor test only checks new Generator('testTemplate'), so it does not cover the entrypoint bypass. The regression cases fit in the constructor suite in apps/generator/test/generator.test.js: [1]

  • Missing targetDir with an entrypoint should throw for both default output and explicit output: 'fs'.
  • Missing targetDir with an entrypoint and output: 'string' should remain accepted.
  • A valid target directory with an entrypoint and filesystem output should remain accepted.

Keep the string-mode entrypoint requirement separate: setupOutput() already rejects output: 'string' without an entrypoint. That is worth preserving when adjusting constructor validation. [2]

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about generator with Dosu Add Dosu to your team

dosubot[bot] avatar Nov 06 '26 16:11 dosubot[bot]

Hi maintainers! 👋

I have reproduced the bug and prepared a complete fix with 4 regression unit tests in apps/generator/test/generator.test.js (all 134 unit tests and ESLint passing locally) along with a release changeset.

Could a maintainer please review and approve this issue (/approve) so we can reopen and run CI on PR #2315? Thank you!

yash2006kr avatar Nov 08 '26 02:11 yash2006kr

Thanks @yash2006kr, Please always check pr description of an issue to see if the author is working on it. As the author of this issue I've explicitly mentioned that I'm willing to open a pr. For more information read contributing guidelines. Feel free to work on other open issues :)

devxdh avatar Nov 08 '26 03:11 devxdh

Understood! Thanks for letting me know @devxdh, all the best with your PR! 🙂

yash2006kr avatar Nov 08 '26 03:11 yash2006kr