generator icon indicating copy to clipboard operation
generator copied to clipboard

[BUG] noOverwriteGlobs skips generating files on first run in an empty directory

Open devxdh opened this issue 4 weeks ago • 1 comments

Describe the bug.

The noOverwriteGlobs option is designed to protect files from being overwritten when regenerating a template, preserving manual user modifications. Non-existent files should be generated normally on the first run.

In apps/generator/lib/generator.js#shouldOverwriteFile, the generator correctly implements this principle:

const fileExists = await exists(path.resolve(this.targetDir, filePath));
if (!fileExists) return true;
return !this.noOverwriteGlobs.some(globExp => minimatch(filePath, globExp));

If the file does not exist on disk (!fileExists), generation is allowed.

However, in the React renderer pipeline (apps/generator/lib/renderer/react.js#saveContentToFile), noOverwriteGlobs is checked a second time without checking if the file exists on disk:

const finalFileName = path.basename(filePath);
const shouldOverwrite = !noOverwriteGlobs.some(globExp => minimatch(finalFileName, globExp));

if (shouldOverwrite) {
  await writeFile(filePath, content, { mode: permissions });
} else {
  await log.debug(logMessage.skipOverwrite(filePath));
}

Because saveContentToFile matches finalFileName against noOverwriteGlobs unconditionally: On the very first run into an empty directory, any file matching noOverwriteGlobs (for example *.md, README.md, or config.json) is silently never created, and the generator logs: File <targetDir>/README.md already exists and won't be overwritten. even though the file does not exist on disk.

Non-Renderable vs Renderable Inconsistency:

For static non-renderable files (such as static .txt files), generateFile() in generator.js uses shouldOverwriteFile and copies the file on the first run. For React template files, saveContentToFile drops the file on the first run.

Expected behavior

Files matching noOverwriteGlobs should still be generated on the initial generation if they do not yet exist in the target directory. noOverwriteGlobs should only prevent overwriting existing files on disk.

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'); const os = require('os'); const fs = require('fs'); const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'gen-')); new Generator(path.resolve('apps/generator/test/test-templates/react-template'), tmp, { forceWrite: true, noOverwriteGlobs: ['*.md'], templateParams: { version: '1.0.0', mode: 'production', singleFile: 'false', singleFolder: 'false' } }).generateFromFile(path.resolve('apps/generator/test/docs/dummy.yml')).then(() => { const files = fs.readdirSync(tmp); console.log('Generated files:', files); console.log('test-file.md created on fresh run?:', files.includes('test-file.md')); });"

Option 2: Using the Generator class in a script:

const Generator = require('./apps/generator/lib/generator');
const path = require('path');
const fs = require('fs');
const os = require('os');

const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'gen-fresh-'));
const templatePath = path.resolve('apps/generator/test/test-templates/react-template');
const docPath = path.resolve('apps/generator/test/docs/dummy.yml');

(async () => {
  const gen = new Generator(templatePath, tmpDir, {
    forceWrite: true,
    noOverwriteGlobs: ['*.md'],
    templateParams: {
      version: '1.0.0',
      mode: 'production',
      singleFile: 'false',
      singleFolder: 'false'
    }
  });

  await gen.generateFromFile(docPath);

  const files = fs.readdirSync(tmpDir);
  console.log('Files in fresh target directory:', files);
  console.log('Was test-file.md created on initial run?:', files.includes('test-file.md'));

  fs.rmSync(tmpDir, { recursive: true, force: true });
})();

Observed Output:

Files in fresh target directory: [
  'AnonymousSchema_11.cs',
  'AnonymousSchema_6.cs',
  'DummyArrayRank.cs',
  'DummyCreated.cs',
  'DummyInfo.cs',
  'DummyObject.cs',
  'DummyRecursiveObject.cs',
  'asyncapi.yaml',
  'conditionalFile.txt',
  'conditionalFolder',
  'conditionalFolder2'
]
Was test-file.md created on initial run?: false

🖥️ 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

This overlaps with #2270: @Adi-204 reproduced the same first-run failure there with noOverwriteGlobs: ['User.ts'], alongside a separate directory-pattern problem for renamed React outputs. That thread is useful context for coordinating the fixes. [1]

One regression-test gap worth covering in your PR: the existing noOverwriteGlobs integration test pre-creates test-file.md, so it only checks preservation, not initial creation. Add a case that generates into a fresh directory with the glob enabled, asserts the file’s contents, then edits it and regenerates to verify preservation. [2]

Include a <File name="…"> case: the existence check needs to use the final destination after metadata.fileName is applied, not the source-derived output path. The renderer resolves that name immediately before its overwrite check. [3]

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]