hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

hardhat can't handle json and ts file with the same name

Open islishude opened this issue 1 year ago • 1 comments

Version of Hardhat

2.22.8

What happened?

I have a project like the following

$ tree task 
task
├── notfound.json
├── notfound.ts
└── ok.ts

1 directory, 3 files

there are two file with the same name, notfound.json and notfound.ts

hardhat can't handle this case and has no specific errors

Minimal reproduction steps

Init hardhat project

cd `mktemp -d`
npx hardhat init                              
Need to install the following packages:
[email protected]
Ok to proceed? (y) y

888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

👷 Welcome to Hardhat v2.22.8 👷‍

✔ What do you want to do? · Create a TypeScript project
✔ Hardhat project root: · /private/var/folders/ns/ft0h42296vbct3jtq5rpq71h0000gn/T/tmp.EJmsOluKXV
✔ Do you want to add a .gitignore? (Y/n) · y
✔ Do you want to install this sample project's dependencies with npm (hardhat @nomicfoundation/hardhat-toolbox)? (Y/n) · y

npm install --save-dev "hardhat@^2.22.8" "@nomicfoundation/hardhat-toolbox@^5.0.0"
npm warn deprecated [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm warn deprecated [email protected]: Glob versions prior to v9 are no longer supported
npm warn deprecated [email protected]: Glob versions prior to v9 are no longer supported
npm warn deprecated [email protected]: Glob versions prior to v9 are no longer supported
npm warn deprecated [email protected]: Glob versions prior to v9 are no longer supported

added 577 packages, and audited 578 packages in 21s

92 packages are looking for funding
  run `npm fund` for details

25 vulnerabilities (22 low, 3 high)

To address issues that do not require attention, run:
  npm audit fix

Some issues need review, and may require choosing
a different dependency.

Run `npm audit` for details.

✨ Project created ✨

See the README.md file for some example tasks you can run

Give Hardhat a star on Github if you're enjoying it! ⭐️✨

     https://github.com/NomicFoundation/hardhat

Add task

task
├── notfound.json
├── notfound.ts
└── ok.ts

1 directory, 3 files
$ cat task/notfound.json 
{}%                                                                                                     
$ cat task/notfound.ts  
import { task } from "hardhat/config";

console.log("pre notfound");

task("notfound").setAction(async () => {
  console.log("notfound");
});

console.log("post notfound");
$ cat task/ok.ts      
import { task } from "hardhat/config";

console.log("pre ok");

task("ok").setAction(async () => {
  console.log("ok");
});

console.log("post ok");
$ cat hardhat.config.ts 
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

import "./task/notfound";
import "./task/ok";

const config: HardhatUserConfig = {
  solidity: "0.8.24",
};

export default config;
$ npx hardhat ok       
pre ok
post ok
ok
npx hardhat notfound
pre ok
post ok
Error HH303: Unrecognized task 'notfound'

For more info go to https://hardhat.org/HH303 or run Hardhat with --show-stack-traces
npx hardhat         
pre ok
post ok
Hardhat version 2.22.8

Usage: hardhat [GLOBAL OPTIONS] [SCOPE] <TASK> [TASK OPTIONS]

GLOBAL OPTIONS:

  --config              A Hardhat config file. 
  --emoji               Use emoji in messages. 
  --flamegraph          Generate a flamegraph of your Hardhat tasks 
  --help                Shows this message, or a task's help if its name is provided 
  --max-memory          The maximum amount of memory that Hardhat can use. 
  --network             The network to connect to. 
  --show-stack-traces   Show stack traces (always enabled on CI servers). 
  --tsconfig            A TypeScript config file. 
  --typecheck           Enable TypeScript type-checking of your scripts/tests 
  --verbose             Enables Hardhat verbose logging 
  --version             Shows hardhat's version. 


AVAILABLE TASKS:

  check                 Check whatever you need
  clean                 Clears the cache and deletes all artifacts
  compile               Compiles the entire project, building all artifacts
  console               Opens a hardhat console
  coverage              Generates a code coverage report for tests
  flatten               Flattens and prints contracts and their dependencies. If no file is passed, all the contracts in the project will be flattened.
  gas-reporter:merge 
  help                  Prints this message
  node                  Starts a JSON-RPC server on top of Hardhat Network
  ok                 
  run                   Runs a user-defined script after compiling the project
  test                  Runs mocha tests
  typechain             Generate Typechain typings for compiled contracts
  verify                Verifies a contract on Etherscan or Sourcify


AVAILABLE TASK SCOPES:

  ignition              Deploy your smart contracts using Hardhat Ignition
  vars                  Manage your configuration variables

To get help for a specific task run: npx hardhat help [SCOPE] <TASK>

Search terms

Error HH303: Unrecognized task

islishude avatar Aug 15 '24 04:08 islishude

Hi @islishude. This seems to be a bug in ts-node, the tool we use to run typescript code without having to compile it.

If you have this:

// index.ts
import "./foo/bar"

// foo/bar.ts
console.log("bar")

// foo/bar.json
{}

and run ts-node index.ts, then "bar" doesn't get printed. Running the same code with tsx does work.

The reason I say this is a bug is that, in pure JavaScript code, js files have precedence over JSON files with the same name (you can check that by running a similar example with javascript and require).

The next major version of Hardhat is going to migrate to tsx, but I don't have an ETA for that.

Sadly, I can't think of any workaround other than renaming the files :disappointed:

fvictorio avatar Sep 04 '24 08:09 fvictorio

resolved on v3

islishude avatar Aug 14 '25 02:08 islishude

Thanks for letting us know!

fvictorio avatar Aug 14 '25 09:08 fvictorio