skygragon / leetcode-cli

A cli tool to enjoy leetcode!
MIT License
3.65k stars 457 forks source link

Unable to show with ID #241

Open spgyip opened 10 months ago

spgyip commented 10 months ago

Problem Summary

There is a warning message from nodejs when leetcode is launched, I have no idea how to fix it.

░▒▓    ~/Codes/leetcode ▓▒░ leetcode                                                                                              ░▒▓ ✔  base   at 17:10:43  ▓▒░
login: (node:49081) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)

The real problem is that it's unable to show with ID, I am not sure if it's related to the Warning problem.

For example, I can show with keyword 'two-sum'

░▒▓    ~/Codes/leetcode ▓▒░ leetcode show 'two-sum'                                                                                         ░▒▓ ✔  base   at 17:18:10  ▓▒░
[1] Two Sum

https://leetcode.cn/problems/two-sum/description/

* algorithms
* Easy (53.17%)
* Total Accepted:    5.1M
* Total Submissions: 9.6M
* Testcase Example:  '[2,7,11,15]\n9'

....

But fails with ID, with returns "Problem not found"

░▒▓    ~/Codes/leetcode ▓▒░ leetcode show 1                                                                                                 ░▒▓ ✔  base   at 17:18:13  ▓▒░
[ERROR] Problem not found!

The same "Problem not found" is returned when submitting.

░▒▓    ~/Codes/leetcode ▓▒░ leetcode submit 1.two-sum.go                                                                                  ░▒▓ 1 ✘  base   at 17:21:31  ▓▒░
[ERROR] Problem not found!

How to reproduce

Always.

Environment

spgyip commented 10 months ago

The nodejs warning problem

The nodejs warning problem is caused by the dependent package winston. This is my dependent tree

░▒▓    ~/node_modules ▓▒░ npm list winston                                                                                                  ░▒▓ ✔  base   at 18:46:00  ▓▒░
supergui@ /Users/supergui
└─┬ leetcode-cli@2.6.2
  └─┬ prompt@1.0.0
    └── winston@2.1.1

It can be simply verified by require the package with node.

# Add `--trace-warnings` to trace warning stack
node --trace-warnings
> require("winston")
> (node:77783) Warning: Accessing non-existent property 'padLevels' of module exports inside circular dependency
    at emitCircularRequireWarning (node:internal/modules/cjs/loader:887:11)
    at Object.get (node:internal/modules/cjs/loader:903:5)
    at exports.setLevels (/Users/supergui/node_modules/winston/lib/winston/common.js:35:14)
    at Object.<anonymous> (/Users/supergui/node_modules/winston/lib/winston.js:84:8)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)

The trace warning are helpful to retrieve the problem is caused by winston.js:84:8.

This problem is solved with the latest 2.x version of winston, we can install a winston@2.x then verify it

░▒▓    ~/node_modules ▓▒░ npm install winston@2.x                                                                                           ░▒▓ ✔  base   at 18:52:16  ▓▒░

added 1 package, removed 25 packages, changed 2 packages, and audited 218 packages in 692ms

Now we can see the winston is at version 2.4.7

░▒▓    ~/node_modules ▓▒░ npm list                                                                                                          ░▒▓ ✔  base   at 18:52:30  ▓▒░
supergui@ /Users/supergui
├── leetcode-cli@2.6.2
└── winston@2.4.7

The warning message is gone now

░▒▓    ~/node_modules ▓▒░ node --trace-warnings                                                                                             ░▒▓ ✔  base   at 18:52:32  ▓▒░
Welcome to Node.js v20.11.0.
Type ".help" for more information.
> require("winston")

Fix leetcode-cli

So, we can fix the leetcode's dependencies manually. We use npm-check-updates to help upgrading package.json, we must install the tool first.

# Install ncu
npm i -g npm-check-updates
# Update all dependencies to latest minor version
# This will update the `prompt@1.0.0` to `prompt@1.3.0`
ncu -u
# Install all packages
npm install
# `winston` is indirect dependency.
# Must install the latest 2.x version of winston manually.
npm install winston@2.x
spgyip commented 10 months ago

Without the warning message, however, the leetcode-cli's show ID problem is not fixed still.

░▒▓    ~/node_modules/leetcode-cli ▓▒░ leetcode show 1                                                                                      ░▒▓ ✔  base   at 19:13:06  ▓▒░
[ERROR] Problem not found!
spgyip commented 10 months ago

Got the problem, maybe I will raise a PR to fix this.

lib/core.js#L90C6-L90C6

keyword = Number(keyword) || keyword;
const problem = problems.find(function(x) {
      return x.fid === keyword || x.name === keyword || x.slug === keyword;
});

As shown, the type of x.fid is actually String, when querying by ID, unfortunately the keyword is converted to type Number. This will make x.fid === keyword be false.

{
    state: 'None',
    id: 1981,
    fid: '1831',
    name: 'Maximum Transaction Each Day',
    slug: 'maximum-transaction-each-day',
    link: 'https://leetcode.cn/problems/maximum-transaction-each-day/description/',
    locked: true,
    percent: 76.16959064327486,
    level: 'Medium',
    starred: false,
    category: 'database'
}