Skip to content

Bug: scripts/generate/run.js fails if target directories do not exist #5683

@EricForgy

Description

@EricForgy

Environment

  • OpenZeppelin Contracts Upgradeable v5.3.0
  • Node.js (Edit: was on v20+, but upgraded to LTS v22.15.1 with same issue)
  • Using npm and Hardhat on macOS
  • Fresh clone of openzeppelin-contracts-upgradeable

Details

When running npm run generate on a fresh clone of the repo, the script fails with an ENOENT error if the target directory (e.g., contracts/utils/math/) doesn’t already exist.

This happens because fs.writeFileSync() is called without ensuring the output directory is present.


Code to reproduce bug

  1. Clone the repo:

    git clone https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable.git
    cd openzeppelin-contracts-upgradeable
  2. Install dependencies:

    npm install
  3. Run the generator:

    npm run generate
  4. You’ll see:

    Error: ENOENT: no such file or directory, open 'contracts/utils/math/SafeCast.sol'

Suggested Fix

Add this line to scripts/generate/run.js before writing output:

fs.mkdirSync(path.dirname(output), { recursive: true });

This ensures the parent directories exist before attempting to write files.

Happy to open a PR if this direction makes sense!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions