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

contentlayer build produces TypeError: The "code" argument must be of type number. Received an instance of Object #495

Open
Xenfo opened this issue Jun 24, 2023 · 22 comments

Comments

@Xenfo
Copy link

Xenfo commented Jun 24, 2023

The error is non-fatal and still generates the documents.

Generated 9 documents in .contentlayer
TypeError: The "code" argument must be of type number. Received an instance of Object
    at process.set [as exitCode] (node:internal/bootstrap/node:124:9)
    at Cli.runExit (/home/xenfo/projects/website/node_modules/clipanion/lib/advanced/Cli.js:232:26)
    at run (file:///home/xenfo/projects/website/node_modules/@contentlayer/cli/src/index.ts:39:3)
    at main (/home/xenfo/projects/website/node_modules/contentlayer/bin/cli.cjs:5:3) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Contentlayer config:

import { defineDocumentType, makeSource } from 'contentlayer/source-files';

export const Post = defineDocumentType(() => ({
  name: 'Post',
  filePathPattern: 'posts/*.mdx',
  contentType: 'mdx',
  fields: {
    title: {
      type: 'string',
      description: 'The title of the post',
      required: true
    },
    description: {
      type: 'string',
      description: 'The description of the post',
      required: true
    },
    publishedOn: {
      type: 'date',
      description: 'The publishing date of the post',
      required: true
    },
    editedOn: {
      type: 'date',
      description: 'The editing date of the post',
      required: true
    },
    status: {
      type: 'enum',
      description: 'The status of the post',
      options: ['draft', 'published', 'edited'],
      required: true
    }
  },
  computedFields: {
    slug: {
      type: 'string',
      description: 'The slug of the post',
      resolve: (post) => post._raw.sourceFileName.replace(/\.mdx?$/, '')
    }
  }
}));

export default makeSource({
  contentDirPath: 'data',
  contentDirExclude: ['themes'],
  documentTypes: [Post]
});
@Xenfo
Copy link
Author

Xenfo commented Jun 24, 2023

I just ran a build on Vercel and it seems like there is no error there, not sure what's going on.

@schickling
Copy link
Collaborator

Thanks for opening this issue. Can you please provide minimal a reproduction repo with instructions how to reproduce the problem you're experiencing? Thank you!

@Xenfo
Copy link
Author

Xenfo commented Jun 24, 2023

  1. Clone https://github.com/Xenfo/cl-demo
  2. yarn install
  3. yarn contentlayer build

@schickling schickling added this to the 0.3.4 milestone Jun 25, 2023
@schickling
Copy link
Collaborator

I couldn't reproduce the problem you've described:

CleanShot 2023-06-28 at 17 30 26@2x

@schickling schickling removed this from the 0.3.4 milestone Jun 28, 2023
@Xenfo
Copy link
Author

Xenfo commented Jun 28, 2023

Seems like something was deprecated on Node 19, and removed in Node 20, which is where the issue lies.

cl-demo on  main [!] via  v20.3.1 took 22s
❯ rtx use node@18
Downloading node-v18.16.1-linux-x64.tar.gz...
-> https://nodejs.org/dist/v18.16.1/node-v18.16.1-linux-x64.tar.gz
Installing node-v18.16.1-linux-x64...
Installed node-v18.16.1-linux-x64 to /home/xenfo/.local/share/rtx/installs/node/18.16.1
rtx node@18.16.1 9.5.1                                                                                             ✓ 15s
cl-demo on  main [!?] via  v18.16.1 took 15s
❯ npm i -g

cl-demo on  main [!?] via  v18.16.1
❯ node -v
v18.16.1

cl-demo on  main [!?] via  v18.16.1
❯ npm i -g yarn

added 1 package in 474ms
Reshimming rtx 18.16.1...

cl-demo on  main [!?] via  v18.16.1
❯ yarn
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed in 0s 217ms
➤ YN0000: ┌ Link step
➤ YN0008: │ esbuild@npm:0.17.19 must be rebuilt because its dependency tree changed
➤ YN0008: │ protobufjs@npm:7.2.3 must be rebuilt because its dependency tree changed
➤ YN0008: │ contentlayer@npm:0.3.3 must be rebuilt because its dependency tree changed
➤ YN0000: └ Completed in 1s 257ms
➤ YN0000: Done in 1s 650ms

cl-demo on  main [!?] via  v18.16.1 took 2s
❯ yarn contentlayer build
Generated 1 documents in .contentlayer

cl-demo on  main [!?] via  v18.16.1
❯ rtx use node@19
Downloading node-v19.9.0-linux-x64.tar.gz...
-> https://nodejs.org/dist/v19.9.0/node-v19.9.0-linux-x64.tar.gz
WARNING: node-v19.9.0-linux-x64 is in LTS Maintenance mode and nearing its end of life.
It only receives *critical* security updates, *critical* bug fixes and documentation updates.
Installing node-v19.9.0-linux-x64...
Installed node-v19.9.0-linux-x64 to /home/xenfo/.local/share/rtx/installs/node/19.9.0
rtx node@19.9.0 9.6.3                                                                                              ✓ 13s
cl-demo on  main [!?] via  v19.9.0 took 12s
❯ npm i -g yarn

added 1 package in 339ms
Reshimming rtx 19.9.0...

cl-demo on  main [!?] via  v19.9.0
❯ yarn contentlayer build
Generated 1 documents in .contentlayer
(node:115156) [DEP0164] DeprecationWarning: Implicit coercion to integer for exit code is deprecated.
(Use `node --trace-deprecation ...` to show where the warning was created)

cl-demo on  main [!?] via  v19.9.0
❯ rtx use node@20

cl-demo on  main [!?] via  v20.3.1
❯ yarn
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed
➤ YN0000: ┌ Fetch step
➤ YN0000: └ Completed
➤ YN0000: ┌ Link step
➤ YN0008: │ esbuild@npm:0.17.19 must be rebuilt because its dependency tree changed
➤ YN0008: │ protobufjs@npm:7.2.3 must be rebuilt because its dependency tree changed
➤ YN0008: │ contentlayer@npm:0.3.3 must be rebuilt because its dependency tree changed
➤ YN0000: └ Completed in 1s 27ms
➤ YN0000: Done in 1s 368ms

cl-demo on  main [!?] via  v20.3.1
❯ yarn contentlayer build
Generated 1 documents in .contentlayer
TypeError: The "code" argument must be of type number. Received an instance of Object
    at process.set [as exitCode] (node:internal/bootstrap/node:124:9)
    at Cli.runExit (/tmp/cl-demo/node_modules/clipanion/lib/advanced/Cli.js:232:26)
    at run (file:///tmp/cl-demo/node_modules/@contentlayer/cli/src/index.ts:39:3)
    at main (/tmp/cl-demo/node_modules/contentlayer/bin/cli.cjs:5:3) {
  code: 'ERR_INVALID_ARG_TYPE'
}

cl-demo on  main [!?] via  v20.3.1
❯
(node:115156) [DEP0164] DeprecationWarning: Implicit coercion to integer for exit code is deprecated.

@schickling
Copy link
Collaborator

I still couldn't reproduce it via GitPod.

CleanShot 2023-06-29 at 09 34 22@2x

@Xenfo
Copy link
Author

Xenfo commented Jun 29, 2023

I’m not sure what’s going on with GitPod but I was able to reproduce this on two separate machines, one in WSL and the other being a Mac.

image

@Xenfo
Copy link
Author

Xenfo commented Jun 29, 2023

Just reproduced on GitPod too

image

@Andrew-Sem
Copy link

@Xenfo, hey, did you solve this somehow?

@Xenfo
Copy link
Author

Xenfo commented Jul 12, 2023

@Xenfo, hey, did you solve this somehow?

No, it’s annoying but I just let it happen since everything is still generated properly.

@schickling schickling added this to the 0.3.5 milestone Jul 17, 2023
@dstroot
Copy link

dstroot commented Aug 12, 2023

Same here - issue occuring on Node v20.5.0. DOES NOT occur with Node 18.17.

@Slyracoon23
Copy link

Slyracoon23 commented Sep 10, 2023

I have the same issue Node v20.5.1
Switching to Node v18.17.1 did do the trick

@YannHulot
Copy link

Same problem here. Not sure if there is anything to do about it.

@CodeSpent
Copy link

CodeSpent commented Oct 16, 2023

This is related to Node Deprecation DEP0164 (process.exit(code), process.exitCode coercion to integer.

This will offer a deprecation warning on Node 19.x.x, but as of Node 20.0.0, it will throw an error.

In relation to contentlayer, the CLI is attempting to assign a resulting object with documentCount's existence as a means of coercing to a positive exit code.

Within CLI.js function runExit, assignment to process.exitCode should be a valid integer.

image

It is worth noting that this doesn't affect functionality of CLI commands. It just impacts the ability for CLI commands to report a valid exit code, so it will always be assumed to be an error.

@gustaveWPM
Copy link

Hello, same problem here.
This error message is confusing: I thought I had done something wrong in my Contentlayer configuration file.

Thank you all for your investigations.

@andri2621
Copy link

Hello, same problem here. This error message is confusing: I thought I had done something wrong in my Contentlayer configuration file.

Thank you all for your investigations.

Me too. I just upgrade my node to v20.9.0, and this happen.
I think i should downgrade again.

@gustaveWPM
Copy link

Hello, same problem here. This error message is confusing: I thought I had done something wrong in my Contentlayer configuration file.
Thank you all for your investigations.

Me too. I just upgrade my node to v20.9.0, and this happen. I think i should downgrade again.

No, don't worry.
Even if the error message occurs, the code runs...

It's just very confusing.

@jrolfs
Copy link

jrolfs commented Nov 15, 2023

So it seems the BuildCommand has been yielding an object (of type GenerateInfo) to clipanion's Cli#runExit API which instead expects an exit code number.

I suspect this has gone unnoticed for so long due to the now legacy process.exitCode coercion behavior in Node.js. I will open a PR with my attempt at a fix, but I've also applied it as a patch via Yarn for now.

Here's what the patch looks like using yarn patch:

.yarn/patches/@contentlayer-cli-npm-0.3.3-e932de1bc4.patch

diff --git a/dist/commands/BuildCommand.js b/dist/commands/BuildCommand.js
index 1cc67c8c9b28622c2391c1da70754ab99dcc35d6..27b35388b71bc8cb64463329e9b1ef251b3382b4 100644
--- a/dist/commands/BuildCommand.js
+++ b/dist/commands/BuildCommand.js
@@ -4,7 +4,7 @@ import { BaseCommand } from './_BaseCommand.js';
 class BuildCommand extends BaseCommand {
     constructor() {
         super(...arguments);
-        this.executeSafe = () => pipe(this.clearCacheIfNeeded(), T.chain(() => core.getConfig({ configPath: this.configPath })), T.tap((config) => (config.source.options.disableImportAliasWarning ? T.unit : T.fork(core.validateTsconfig))), T.chain((config) => core.generateDotpkg({ config, verbose: this.verbose })), T.tap(core.logGenerateInfo), OT.withSpan('@contentlayer/cli/commands/BuildCommand:executeSafe'));
+        this.executeSafe = () => pipe(this.clearCacheIfNeeded(), T.chain(() => core.getConfig({ configPath: this.configPath })), T.tap((config) => (config.source.options.disableImportAliasWarning ? T.unit : T.fork(core.validateTsconfig))), T.chain((config) => core.generateDotpkg({ config, verbose: this.verbose })), T.tap(core.logGenerateInfo), T.map(() => 0), OT.withSpan('@contentlayer/cli/commands/BuildCommand:executeSafe'));
     }
 }
 BuildCommand.paths = [['build']];

@imadbz
Copy link

imadbz commented May 12, 2024

still getting this error
+1

@MildTomato
Copy link

MildTomato commented May 17, 2024

also just got this error
running node v20
+1

@zce
Copy link

zce commented May 17, 2024

🪦 #429 (comment)

@psr-ai
Copy link

psr-ai commented May 18, 2024

Issue goes away on node version 18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.