generator icon indicating copy to clipboard operation
generator copied to clipboard

Conditional generation is skipped for nested template paths on Windows

Open rajanpanth opened this issue 3 months ago • 3 comments

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.

rajanpanth avatar Sep 04 '26 05:09 rajanpanth

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.

github-actions[bot] avatar Sep 04 '26 05:09 github-actions[bot]

hey, when did you face this issue, explain your use case and how to replicate it please

derberg avatar Sep 17 '26 14:09 derberg

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:

  1. Windows path separators (lines 735 & 899): path.relative() produces backslashes on Windows (transports\kafka), but config keys are written with POSIX slashes (transports/kafka). Because templateConfig.conditionalGeneration[relativeDir] is a strict string lookup, it evaluates to undefined. On Windows, the condition is bypassed entirely and unrequested files get generated.

  2. 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 checked transports. It tries to render client.js and 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:

  1. Its .ageneratorrc defines a nested entry:
    conditionalGeneration:
      conditionalFolder2/input.txt:
        parameter: singleFile
        validation:
          enum: ["false"]
    
  2. In integration.test.js (line 180), the existing test only asserts singleFile: "false" (expecting the file to generate).
  3. If you run it with singleFile: "true" on Windows (or simulating backslashes in path.relative):
    • conditionalFolder2/input.txt is generated anyway when it should have been skipped.

4. Fix

  • Normalize relative paths with .split(path.sep).join('/') before looking up keys in conditionalGeneration.
  • 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 :)

devxdh avatar Oct 30 '26 05:10 devxdh