Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[token/ts] TypeError: fields must be array of Layout instances #2683

Closed
Admdebian opened this issue Dec 30, 2021 · 35 comments
Closed

[token/ts] TypeError: fields must be array of Layout instances #2683

Admdebian opened this issue Dec 30, 2021 · 35 comments

Comments

@Admdebian
Copy link

Admdebian commented Dec 30, 2021

If I import the library I get:

TypeError: fields must be array of Layout instances

NPM package:

@solana/spl-token: ^0.2.0-alpha.0

/** TODO: docs */
export const initializeMintInstructionData = struct<InitializeMintInstructionData>([
u8('instruction'),
u8('decimals'),
publicKey('mintAuthority'),
u8('freezeAuthorityOption'),
publicKey('freezeAuthority'),
]);

Update

The problem disappear if I install:

npm install -S @solana/buffer-layout

NPM package:

@solana/buffer-layout: ^4.0.0

Maybe it's an npm dependencies resolution problem.

@Admdebian Admdebian changed the title TypeError: fields must be array of Layout instances [token][ts] TypeError: fields must be array of Layout instances Dec 30, 2021
@Admdebian Admdebian changed the title [token][ts] TypeError: fields must be array of Layout instances [token/ts] TypeError: fields must be array of Layout instances Dec 30, 2021
@jordaaash
Copy link
Contributor

Hmm

"@solana/buffer-layout": "^4.0.0",

The package declares that dependency. Can you post your package.json?

@Admdebian
Copy link
Author

{
  "name": "MyProject",
  "version": "1.0.0",
  "private": true,
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest",
    "lint": "eslint . --ext .js,.jsx,.ts,.tsx"
  },
  "keywords": [
    "blockchain"
  ],
  "author": "Admdebian",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.16.0",
    "@babel/preset-env": "^7.16.4",
    "@babel/preset-flow": "^7.16.0",
    "@babel/runtime": "^7.16.3",
    "@types/bs58": "^4.0.1",
    "@types/jest": "^27.0.2",
    "babel-jest": "^27.3.1",
    "babel-plugin-transform-async-to-generator": "^6.24.1",
    "eslint": "^8.2.0",
    "jest": "^27.3.1",
    "metro-react-native-babel-preset": "^0.66.2",
    "tslint": "^6.1.3",
    "typescript": "^4.4.4"
  },
  "jest": {
    "transform": {
      "^.+\\.[t|j]sx?$": "babel-jest"
    },
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js",
      "jsx",
      "json",
      "node"
    ]
  },
  "files": [
    "/src"
  ],
  "dependencies": {
    "@bonfida/spl-name-service": "^0.1.22",
    "@project-serum/serum": "^0.13.60",
    "@pythnetwork/client": "^2.5.1",
    "@solana/buffer-layout": "^4.0.0",
    "@solana/spl-token": "^0.2.0-alpha.0",
    "@solana/web3.js": "^1.30.2",
    "bip39": "^3.0.4",
    "bs58": "^4.0.1",
    "ed25519-hd-key": "^1.2.0"
  }
}

@Admdebian
Copy link
Author

Hmm

"@solana/buffer-layout": "^4.0.0",

The package declares that dependency. Can you post your package.json?

Yes, the dependency is correct. My project requires that I save the dependency in the main package.json

@jordaaash
Copy link
Contributor

It would help to see your entire project. The error here (TypeError: fields must be array of Layout instances) doesn't give enough context, as I can't tell where it's coming from.

@Admdebian
Copy link
Author

It would help to see your entire project. The error here (TypeError: fields must be array of Layout instances) doesn't give enough context, as I can't tell where it's coming from.

I made a new project with only a sample code and the problem disappeared. I think is not related with this library.

Sorry. Thanks for your time!

@KunalBagaria
Copy link

Having this issue myself now with the latest @solana/pay library.

@rez-cpu
Copy link

rez-cpu commented Feb 13, 2022

Its the package version, this is what current combo that works:
"@solana/pay": "^0.1.3",
"@solana/spl-token": "^0.2.0-alpha.1",
"@solana/buffer-layout": "^4.0.0",
"@solana/web3.js": "^1.34.0",

@cqfd
Copy link

cqfd commented Mar 4, 2022

I'm having this same issue while attempting to use the 0.2.0 createMint function in an anchor test (anchor test ends up running yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts).

The fix for me was to add an explicit package.json dependency on "@solana/buffer-layout": "^4.0.0",, and then delete my node_modules directory and re-install from scratch (🤦‍♂️).

@scottwilson312
Copy link

scottwilson312 commented Mar 4, 2022

    "@solana/buffer-layout": "^4.0.0",
    "@solana/wallet-adapter-base": "^0.9.3",
    "@solana/wallet-adapter-react": "^0.15.3",
    "@solana/wallet-adapter-react-ui": "^0.9.5",
    "@solana/wallet-adapter-wallets": "^0.15.2",
    "@solana/web3.js": "^1.35.1"

I have this build and after rebuilding from scratch today, I got this error. The fix above worked for me, putting buffer-layout in package.json 👍

@wedvjin
Copy link

wedvjin commented Mar 5, 2022

i used yarn instead of npm to install dependencies on a clean project and it worked

"dependencies": {
        "@project-serum/anchor": "^0.22.1",
        "@solana/buffer-layout": "^4.0.0",
        "@solana/spl-token": "^0.2.0",
        "@solana/web3.js": "^1.34.0"
    },

@thedonmon
Copy link

I'm running into this same issue when calling any of the spl functions.

    const walletAta = await spl.getAssociatedTokenAddress(new PublicKey(mint), walletKeyPair.publicKey, false, spl.TOKEN_PROGRAM_ID, spl.ASSOCIATED_TOKEN_PROGRAM_ID);
    const escrowAta = await spl.getAssociatedTokenAddress(new PublicKey(mint), escrowPaymentAccount, false, spl.TOKEN_PROGRAM_ID, spl.ASSOCIATED_TOKEN_PROGRAM_ID);

"dependencies": {
        "@project-serum/anchor": "^0.22.1",
        "@solana/buffer-layout": "^4.0.0",
        "@solana/spl-token": "^0.2.0",
        "@solana/web3.js": "^1.34.0"
    },

@straversi
Copy link

I'm also running into this issue, even with the provided suggestion of explicitly listing buffer-layout as a dep. Maybe it should be re-opened?

@jordaaash
Copy link
Contributor

@straversi I've reopened. Please provide a complete, clear reproduction of the issue. So far all we've got to go on is 8 different code snippets, which is kind of impossible to debug.

FWIW, we use @solana/spl-token in Solana Pay and we don't need to import @solana/buffer-layout, so I'm skeptical that's the actual issue. Dependency issues like this sometimes come from how the dependencies are installed or what the build tooling and config is (Create React App, Webpack, Babel, esbuild, etc)

@jordaaash jordaaash reopened this Mar 22, 2022
@straversi
Copy link

straversi commented Mar 22, 2022

Of course, thank you!

echo '{
  "name": "spl-repro",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@metaplex-foundation/mpl-token-metadata": "1.2.4",
    "@solana/spl-token": "0.2.0",
    "@solana/web3.js": "1.30.2"
  }
}
' > package.json

npm i

# I'm running node v14.18.2
node

> require('@solana/spl-token');

That's all it takes for me.

@jordaaash
Copy link
Contributor

Thanks! Do you get the same issue when installing with yarn?

@charlie-amihan
Copy link

I am getting the same issue with npm install, yarn install solves the issue for me.

@steveluscher
Copy link
Contributor

Yeah, this is unfortunate. Sorry folks.

Basically when npm lays out the dependency tree, it ends up looking like this.

image

spl-token and the root package each end up with their own copy of buffer-layout.

Next, initializeMint tries to make use of buffer-layout-utils which again has its own copy of buffer-layout in it.

image

By the time these instances of Layout – all constructed from different packages of JavaScript – make their way to node_modules/@solana/spl-token/node_modules/@solana/buffer-layout/lib/Layout.js, this instanceof check fails.

image

You know, and I know, that those values are instances of Layout, but since they didn't come from literally the same class, they will never pass an instanceof check.

I'll have a think on this.

@steveluscher
Copy link
Contributor

If someone gets there before I do tomorrow, the answer is going to have something to do with carefully setting peerDependencies in various package.json files.

@steveluscher
Copy link
Contributor

Another thing we might consider, one day, is reimplementing this not to depend on classes, but rather tagged data structures.

// Instead of...
new Float(...)

// Do...
float(...)

// Which produces not an instance of a class, but rather a tagged object…
{__type: 'float', ...}

No classes, no instances, no problems.

@steveluscher
Copy link
Contributor

steveluscher commented Mar 23, 2022

After a few hundred npm link backflips, I figured out a short-term fix for this. We're going to have to upgrade buffer-layout in unison (solana-labs/solana#23899) and republish Every Package™. Stay tuned.

@steveluscher
Copy link
Contributor

Upgrade @solana/web3.js in your package.json to 1.37.0 and the fields must be array of Layout instances error should go away.

Namely, @straversi's example above works for me when I do that. #2683 (comment)

@steveluscher
Copy link
Contributor

Feel free to reopen this if your problems still persist. Closing for now.

@Henry-E
Copy link

Henry-E commented Apr 27, 2022

I'm having this same issue while attempting to use the 0.2.0 createMint function in an anchor test (anchor test ends up running yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts).

This worked for me but I had to also delete package-lock.json

@mschneider
Copy link
Contributor

After a few hundred npm link backflips, I figured out a short-term fix for this. We're going to have to upgrade buffer-layout in unison (solana-labs/solana#23899) and republish Every Package™. Stay tuned.

@steveluscher I saw similar issues with BN js previously. Did you consider to re-export the whole buffer-layout package in web3? This way there's a sure fire way to access the "correct" version in downstream code. Not sure of the drawbacks and longterm issues related to this approach, but it is what anchor does fyi:

https://github.com/project-serum/anchor/blob/master/ts/src/index.ts#L5

@steveluscher
Copy link
Contributor

Oof. The fact that Anchor re-exports all of web3.js is actually pretty troublesome until I finish solana-labs/solana-web3.js#1122.

@WrRaThY
Copy link

WrRaThY commented May 3, 2022

started happening to me after I updated anchor js to 0.24.2

@keitaj
Copy link

keitaj commented May 14, 2022

started happening to me after I updated anchor js to 0.24.2

I happened too but I updated @solana/web3.js and @solana/spl-token and the error did not happen.

 "dependencies": {
    "@project-serum/anchor": "^0.24.2",
    "@project-serum/serum": "0.13.61",
    "@solana/buffer-layout": "^4.0.0",
    "@solana/spl-token": "^0.2.0",
    "@solana/web3.js": "^1.41.10",
    "@types/chai": "^4.3.1"
  },

@aaronovz1
Copy link

This is still an extremely annoying problem. How does one work around it if it's a dependency out of our control and can't be updated, or updated in a reasonable timeframe?

@steveluscher
Copy link
Contributor

Any time you run into this and can produce a minimal repro (eg. a GitHub repro that I can check out that's broken) or straight up give me access to your project, I'm happy to debug and make the fixes necessary, or a recommendation of what to do.

@karan-ksrk
Copy link

karan-ksrk commented Jul 30, 2022

These Two Combination works for me

  "dependencies": {
    "@solana/buffer-layout": "^3.0.0",
    "@solana/web3.js": "^1.41.10",
}

@h4rkl
Copy link

h4rkl commented Aug 18, 2022

If you're using yarn you can specify child dependancy resolutions in your package.json file using:

  "resolutions": {
    "@solana/buffer-layout": "^4.0.0"
  },

This will force all packages to use the specified dependancy version rather than the "3.x.x" version which looks like its causing the conflict.

NPMs equivalent is overrides - https://docs.npmjs.com/cli/v8/configuring-npm/package-json#overrides

@ExoticCoder16
Copy link

Upgrade @solana/web3.js in your package.json to 1.37.0 and the fields must be array of Layout instances error should go away.

Namely, @straversi's example above works for me when I do that. #2683 (comment)

Yes ! Just by typing is not enough, I just re-install solana web3.js package ( npm install --save @solana/web3.js ), it works instantly ! Thank you !

@georgiod9
Copy link

spl-token and the root package each end up with their own copy of buffer-layout.

I have stumbled upon the same issue, and have also deduced that the problem stems from the duplicity of that same package in 3 places.

@zsyborg
Copy link

zsyborg commented Jun 2, 2023

rekt, this issue troubling in 2023 too, same old thing, tried yarn, npm, pnpm. Tried reordering the packages in package.json
this issuede my existing running project. Critical Issue

@reza2870
Copy link

"@metaplex-foundation/mpl-token-metadata": "1.2.4",

Yes
I did all ways
Didn't work for me

@solana-labs solana-labs locked as resolved and limited conversation to collaborators Feb 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests