Conditional generation is skipped for nested template paths on Windows
Describe the bug
conditionalGeneration (and the deprecated conditionalFiles) keys are written with POSIX separators in a template's config, for example conditionalFolder2/input.txt. The lookups in apps/generator/lib/generator.js build their key with path.relative, which returns backslashes on Windows, so a nested key never matches and the condition is silently ignored.
Executed on win32:
path.relative gives : "conditionalFolder2\input.txt"
config key is : "conditionalFolder2/input.txt"
lookup matches? : false
Because the lookup misses, shouldGenerate stays true and the file is generated even when its condition fails. An end-to-end run against the repository's own test template, with singleFile: 'true' (which fails the enum: ["false"] validation), shows the top-level cases behaving correctly while the nested one does not:
conditionalFile.txt exists (expected false): false
conditionalFolder exists (expected false): false
conditionalFolder2/input.txt exists (expected false): true <-- bug
The existing nested-path test only asserts the "should generate" direction, so it passes either way and the regression is invisible on the Windows CI leg.
There is a second, platform-independent part. When a conditionalFiles entry matches, the code sets conditionalPath to the top-level directory rather than the file key:
conditionalPath = relativeSourceDirectory;
so isGenerationConditionMet finds no config for that path and returns undefined. On POSIX that means a file whose condition is met gets skipped. The documented example in configuration-file.md uses a nested key, so documented usage is affected.
Expected behavior
A nested conditionalGeneration or conditionalFiles key is matched on every platform, and a matching conditionalFiles entry is evaluated against its own key.
How to Reproduce
Run the generator on Windows with a template whose config has a nested conditionalGeneration key and a parameter that fails the condition; the file is generated anyway. Output above is from executed runs.
I have a fix with regression tests ready and would like to open a PR for this.
Generated-by: Claude Code 2.x
AI assisted in investigating this bug and drafting this issue. I have reviewed and verified the analysis and the reproduction output myself.
Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.
hey, when did you face this issue, explain your use case and how to replicate it please
Hey @derberg,
I looked into this issue to help answer the use case and reproduction questions.
The issue stems from how apps/generator/lib/generator.js builds path keys when checking conditionalGeneration:
1. The Use Case
When a template organizes optional files inside nested directories, for example:
template/
transports/
websocket/client.js
kafka/client.js
And the template configures .ageneratorrc:
conditionalGeneration:
transports/websocket:
parameter: transport
validation:
const: websocket
transports/kafka:
parameter: transport
validation:
const: kafka
2. What Happens
There are two failure modes in lib/generator.js:
-
Windows path separators (lines 735 & 899):
path.relative()produces backslashes on Windows (transports\kafka), but config keys are written with POSIX slashes (transports/kafka). BecausetemplateConfig.conditionalGeneration[relativeDir]is a strict string lookup, it evaluates toundefined. On Windows, the condition is bypassed entirely and unrequested files get generated. -
Nested folder lookup truncation (line 900): In
generateFile(), the directory is extracted with:const relativeSourceDirectory = relativeSourceFile.split(path.sep)[0] || '.';This only grabs the top-level folder (
transports). If the condition is on a subfolder (transports/kafka):ignoredDirHandler()evaluates the subfolder condition and correctly skips creating the directory on disk.- But
generateFile()misses the condition because it only checkedtransports. It tries to renderclient.jsand write into the uncreated folder, crashing with:ENOENT: no such file or directory.
(There's also a minor bug on line 930 where deprecated conditionalFiles sets conditionalPath = relativeSourceDirectory instead of relativeSourceFile, causing nested files to be skipped).
3. How to Replicate
You can replicate this against the repository's own apps/generator/test/test-templates/react-template:
- Its
.ageneratorrcdefines a nested entry:conditionalGeneration: conditionalFolder2/input.txt: parameter: singleFile validation: enum: ["false"] - In
integration.test.js(line 180), the existing test only assertssingleFile: "false"(expecting the file to generate). - If you run it with
singleFile: "true"on Windows (or simulating backslashes inpath.relative):conditionalFolder2/input.txtis generated anyway when it should have been skipped.
4. Fix
- Normalize relative paths with
.split(path.sep).join('/')before looking up keys inconditionalGeneration. - In
generateFile(), check parent directories from closest to root so nested folder rules match child files. - Fix line 930 for
conditionalFiles.
I hope this report answers your questions :)