octokit / core.js

Extendable client for GitHub's REST & GraphQL APIs
MIT License
1.19k stars 310 forks source link

[BUG/ASK for HELP]: Issues with `@octokit/core@v6.0.0+` when using `ncc` to bundle code #666

Closed jamacku closed 8 months ago

jamacku commented 8 months ago

What happened?

This is probably a very silly beginner issue, but I can't figure out what is wrong. I'm using @octokit/core in most of my GHA and all of then breaks when updating to v6+. I use ncc for compiling TypeScript to single file as suggested in official typescript-action template.

All actions behave exactly the same after updating to v6. The call of ncc fails with:

Error: Module not found: Error: Package path . is not exported from package /home/runner/work/regression-sniffer/regression-sniffer/node_modules/@octokit/core (see exports field in /home/runner/work/regression-sniffer/regression-sniffer/node_modules/@octokit/core/package.json)
Did you mean './@octokit/core'?
Requests that should resolve in the current directory need to start with './'.

Example from one of my actions: https://github.com/redhat-plumbers-in-action/regression-sniffer/actions/runs/8140133727/job/22244735285?pr=50#step:6:8

This can be also 100% reproduce locally:

gh repo clone redhat-plumbers-in-action/regression-sniffer
cd regression-sniffer
yarn update @octokit/core
yarn
yarn run package # or yarn run all

package.json:

{
  "name": "regression-sniffer",
  "version": "1.0.0",
  "private": true,
  "description": "Loyal GitHub action that sniffs out any follow-up and revert commit of your cherry-picked commit",
  "main": "src/main.ts",
  "scripts": {
    "build": "tsc",
    "format": "prettier --write '**/*.ts'",
    "format-check": "prettier --check '**/*.ts'",
    "lint": "eslint src/**/*.ts",
    "package": "ncc build --source-map --license licenses.txt",
    "test": "vitest run --coverage",
    "update-snapshots": "vitest run --update",
    "all": "yarn install && yarn run build && yarn run format && yarn run lint && yarn run package && yarn test"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/redhat-plumbers-in-action/regression-sniffer.git"
  },
  "keywords": [
    "actions",
    "regression-detection",
    "revert",
    "follow-up"
  ],
  "author": "jamacku@redhat.com",
  "license": "MIT",
  "packageManager": "yarn@4.1.1",
  "type": "module",
  "dependencies": {
    "@actions/core": "^1.10.1",
    "@actions/github": "^6.0.0",
    "@octokit/core": "^5.1.0",
    "@octokit/plugin-throttling": "^8.1.3",
    "@probot/octokit-plugin-config": "^2.0.1",
    "deepmerge": "^4.3.1",
    "zod": "^3.22.4"
  },
  "devDependencies": {
    "@total-typescript/ts-reset": "^0.5.1",
    "@types/node": "^20.11.19",
    "@typescript-eslint/eslint-plugin": "^7.0.2",
    "@typescript-eslint/parser": "^7.0.2",
    "@vercel/ncc": "^0.38.1",
    "@vitest/coverage-v8": "^1.3.1",
    "eslint": "^8.56.0",
    "eslint-plugin-prettier": "^5.1.3",
    "prettier": "^3.2.5",
    "typescript": "^5.3.3",
    "vitest": "^1.3.1"
  }
}

Thank you for any help or suggestions.

Versions

@octokit/core: v6.0.0, v6.0.1 node: v20.11.1 npm: 10.2.4 yarn: 4.1.0

Relevant log output

ncc: Version 0.38.1
ncc: Compiling file index.js into ESM
ncc: Using typescript@5.3.3 (local user-provided)
Error: Module not found: Error: Package path . is not exported from package /home/runner/work/regression-sniffer/regression-sniffer/node_modules/@octokit/core (see exports field in /home/runner/work/regression-sniffer/regression-sniffer/node_modules/@octokit/core/package.json)
Did you mean './@octokit/core'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.
    at /home/runner/work/regression-sniffer/regression-sniffer/node_modules/@vercel/ncc/dist/ncc/index.js.cache.js:38:1896272
    at /home/runner/work/regression-sniffer/regression-sniffer/node_modules/@vercel/ncc/dist/ncc/index.js.cache.js:38:396262
    at _done (eval at create (/home/runner/work/regression-sniffer/regression-sniffer/node_modules/@vercel/ncc/dist/ncc/index.js.cache.js:21:75523), <anonymous>:9:1)
    at eval (eval at create (/home/runner/work/regression-sniffer/regression-sniffer/node_modules/@vercel/ncc/dist/ncc/index.js.cache.js:21:75523), <anonymous>:34:22)

Code of Conduct

github-actions[bot] commented 8 months ago

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

wolfy1339 commented 8 months ago

You don't need @octokit/core when using @actions/github

@actions/github requires v5 @octokit/core, which breaks newer @octokit/core versions defined as dependencies

wolfy1339 commented 8 months ago

Also, I recommend using esbuild instead of ncc as it creates more readable output files, and correctly ouputs ESM, and is much simpler. (From https://github.com/octokit/graphql-action/pull/255#issuecomment-1913661963)

Use https://github.com/actions/create-github-app-token/ as a reference

jamacku commented 8 months ago

@wolfy1339 Thank you for your help! I have removed @octokit/core, and it works as expected.

I'm just a bit confused as to why I'm able to import { Octokit } from '@octokit/core'; without an error.

https://github.com/redhat-plumbers-in-action/regression-sniffer/blob/main/src/octokit.ts#L2

Thank you for suggesting esbuild. I'll have a look.

wolfy1339 commented 8 months ago

In v6 of @octokit/core we switched to ESM, in v5 we were outputting CJS.

Since you are using plugins, you will have to stick with @octokit/core v5 until @probot/octokit-plugin-config is updated and remove @actions/github

gr2m commented 8 months ago

Also, I recommend using esbuild instead of ncc as it creates more readable output files, and correctly ouputs ESM, and is much simpler. (From octokit/graphql-action#255 (comment))

I second that. Use esbuild. I used ncc in the past but esbuild is the better tool to bundle code for actions if your source is ESM