Solana Anchorで「TypeError: Unknown file extension ".ts"」エラー

anchor-cli 0.25.0 で発生したエラー。

現象

anchor testを実施すると以下のエラーが発生。

ターミナル

*  Executing task: anchor test 

BPF SDK: /Users/user/.local/share/solana/install/releases/1.10.8/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
    Finished release [optimized] target(s) in 0.47s
cargo-build-bpf child: /Users/user/.local/share/solana/install/releases/1.10.8/solana-release/bin/sdk/bpf/dependencies/bpf-tools/llvm/bin/llvm-readelf --dyn-symbols /Users/user/Desktop/guess_the_number/target/deploy/guess.so

To deploy this program:
  $ solana program deploy /Users/user/Desktop/guess_the_number/target/deploy/guess.so
The program address will default to this keypair (override with --program-id):
  /Users/user/Desktop/guess_the_number/target/deploy/guess-keypair.json

Found a 'test' script in the Anchor.toml. Running it as a test suite!

Running test suite: "/Users/user/Desktop/guess_the_number/Anchor.toml"

yarn run v1.22.10
warning package.json: No license field
$ /Users/user/Desktop/guess_the_number/node_modules/.bin/ts-mocha -p ./tsconfig.json -t 1000000 'tests/**/*.ts'

TypeError: Unknown file extension ".ts" for /Users/user/Desktop/guess_the_number/tests/guess.ts
    at new NodeError (node:internal/errors:371:5)
    at Object.file: (node:internal/modules/esm/get_format:72:15)
    at defaultGetFormat (node:internal/modules/esm/get_format:85:38)
    at defaultLoad (node:internal/modules/esm/load:13:42)
    at ESMLoader.load (node:internal/modules/esm/loader:303:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:230:58)
    at new ModuleJob (node:internal/modules/esm/module_job:63:26)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:244:11)
    at async Promise.all (index 0)
    at ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at importModuleDynamicallyWrapper (node:internal/vm/module:437:15)
    at formattedImport (/Users/user/Desktop/guess_the_number/node_modules/mocha/lib/nodejs/esm-utils.js:7:14)
    at Object.exports.requireOrImport (/Users/user/Desktop/guess_the_number/node_modules/mocha/lib/nodejs/esm-utils.js:48:32)
    at Object.exports.loadFilesAsync (/Users/user/Desktop/guess_the_number/node_modules/mocha/lib/nodejs/esm-utils.js:103:20)
    at singleRun (/Users/user/Desktop/guess_the_number/node_modules/mocha/lib/cli/run-helpers.js:125:3)
    at Object.exports.handler (/Users/user/Desktop/guess_the_number/node_modules/mocha/lib/cli/run.js:374:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

package.json

{
    "scripts": {
        "lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
        "lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
    },
    "dependencies": {
        "@metaplex-foundation/js": "^0.13.3",
        "@project-serum/anchor": "^0.25.0",
        "@solana/spl-token": "^0.3.2",
        "axios": "^0.27.2",
        "sleep": "^6.3.0"
    },
    "devDependencies": {
        "@types/bn.js": "^5.1.0",
        "@types/chai": "^4.3.0",
        "@types/jest": "^28.1.7",
        "@types/mocha": "^9.1.1",
        "@types/sleep": "^0.0.8",
        "chai": "^4.3.6",
        "mocha": "^9.2.2",
        "npm": "^8.18.0",
        "prettier": "^2.6.2",
        "ts-mocha": "^10.0.0",
        "typescript": "^4.3.5"
    }
}

tsconfig.json

{
  "compilerOptions": {
    "types": ["mocha", "chai"],
    "typeRoots": ["./node_modules/@types"],
    "lib": ["es2015"],
    "module": "commonjs",
    "target": "es6",
    "esModuleInterop": true
  }
}

原因

原因はわからなかったが、本エラーが発生するトリガーがわかった。
@solana/web3.jsを以下のように追加すると本エラーが発生した。

npm add @solana/web3.js

たしか、Anchor web3.jsやMetaplex web3.jsの内部で@solana/web3.jsを利用していたため、指定しなくてもnode_modulesにはインストールされていたはず。

ここらへんの依存関係が原因と思われるが、詳細わからず(もしかしたら、全然違う可能性もあり)。

本件に関連しそうなGitHubのIssues

対応

根本解決作がわからず、@solana/web3.jsを追加する前のソースをGitHubから持ってきてやり直した。

なお、以下も試したが、一度でも@solana/web3.jsを追加していると解決できなかった。
(これでキャッシュは削除済みのはずだが、解決できず)

  • npm uninstall @solana/web3.js
  • npm cache clean --force
  • rm -rf ~/.npm
  • rm -rf node_modules
  • rm -f package-lock.json