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

Module stat cache keeps broken results in cache #26926

Closed
ZauberNerd opened this issue Mar 26, 2019 · 5 comments
Closed

Module stat cache keeps broken results in cache #26926

ZauberNerd opened this issue Mar 26, 2019 · 5 comments
Labels
module Issues and PRs related to the module subsystem.

Comments

@ZauberNerd
Copy link
Contributor

  • Version: v11.11.0
  • Platform: Linux bb-laptop 5.0.4-arch1-1-ARCH deps: update openssl to 1.0.1j #1 SMP PREEMPT Sat Mar 23 21:00:33 UTC 2019 x86_64 GNU/Linux
  • Subsystem: module

Actual behavior:
requireing a non-existing file throws a MODULE_NOT_FOUND error, creating the file afterwards and trying to require again still throws.

Expected behavior:
The second call to require should resolve to the actual file content.

Try the following three lines in a REPL (Node v11.10.1 vs Node v11.11.0):

require('./non-existent.json');
require('fs').writeFileSync('./non-existent.json', '1');
require('./non-existent.json');

It seems like the bug was introduced with this commit: d94f4c23fe - I built Node.js with this commit and with its parent commit and was able to observe the above mentioned behavior.

@addaleax
Copy link
Member

/cc @BridgeAR

@addaleax addaleax added the module Issues and PRs related to the module subsystem. label Mar 26, 2019
ZauberNerd added a commit to untool/untool that referenced this issue Mar 26, 2019
Node v11.11.0 introduced a bug where re-requiring a file which
previously did not exist won't resolve to that file.
Therefore we switch to the async-node target of Webpack for our server-
side build which uses the vm and fs modules to load chunks, so that we
don't run into the above mentioned bug.

Read more: nodejs/node#26926
ZauberNerd added a commit to untool/untool that referenced this issue Mar 26, 2019
Node v11.11.0 introduced a bug where re-requiring a file which
previously did not exist won't resolve to that file.
Therefore we switch to the async-node target of Webpack for our server-
side build which uses the vm and fs modules to load chunks, so that we
don't run into the above mentioned bug.

Read more: nodejs/node#26926
dmbch pushed a commit to untool/untool that referenced this issue Mar 26, 2019
Node v11.11.0 introduced a bug where re-requiring a file which
previously did not exist won't resolve to that file.
Therefore we switch to the async-node target of Webpack for our server-
side build which uses the vm and fs modules to load chunks, so that we
don't run into the above mentioned bug.

Read more: nodejs/node#26926
@thetutlage
Copy link

Yes, facing the exact same issue. Discovered while I was writing some tests

BethGriggs pushed a commit that referenced this issue Apr 4, 2019
This makes sure multiple require calls will not fail in case a file
was created after the first attempt.

PR-URL: #26928
Fixes: #26926
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
BethGriggs pushed a commit that referenced this issue Apr 9, 2019
This makes sure multiple require calls will not fail in case a file
was created after the first attempt.

PR-URL: #26928
Fixes: #26926
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
BethGriggs pushed a commit that referenced this issue Apr 10, 2019
This makes sure multiple require calls will not fail in case a file
was created after the first attempt.

PR-URL: #26928
Fixes: #26926
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Jan Krems <jan.krems@gmail.com>
@lwr
Copy link

lwr commented Jan 20, 2020

not works for paths cache (node_modules)

test case

  1. create 1.js
    // 1.js
    try { require('anything-needs-lookup'); } catch (e) {}
    require('./any-related-js');
    require('fs').mkdirSync('./node_modules');
    require('fs').writeFileSync('./node_modules/non-existent.json', '1');
    // module._compile('', ''); // we use this hack to workaround this issue
    console.log(require('non-existent.json'))
  2. run command
    rm -rf node_modules && echo '' > any-related-js.js && node 1

Actual behavior:
test in node 13.6.0

internal/modules/cjs/loader.js:976
  throw err;
  ^

Error: Cannot find module 'non-existent.json'
Require stack:
- /path/to/test/1.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:973:15)
    at Function.Module._load (internal/modules/cjs/loader.js:855:27)
    at Module.require (internal/modules/cjs/loader.js:1033:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/path/to/test/1.js:10:1)
    at Module._compile (internal/modules/cjs/loader.js:1144:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1164:10)
    at Module.load (internal/modules/cjs/loader.js:993:32)
    at Function.Module._load (internal/modules/cjs/loader.js:892:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/path/to/test/1.js' ]
}

Expected behavior:
-- (uncomment the line that workaround this problem) --
output 1

@BridgeAR
Copy link
Member

@lwr your issue is unrelated to the issue originally mentioned.

I am not certain if your example ever used to work. I tried almost all Node.js versions down to v6.17.1 and all threw the Cannot find module error. It is not really intended to create not found node_modules on the fly during the loading phase and to reload them in the same phase.

@lwr
Copy link

lwr commented Jan 22, 2020

@BridgeAR I am sorry for something wrong in the test case

one line added to the test

try { require('anything-needs-lookup'); } catch (e) {}
+ require('./any-related-js');

and the test command changed to

rm -rf node_modules && echo '' > any-related-js.js && node 1

the test is passed in 10.17.0 / failed in 13.6.0

overcache pushed a commit to overcache/follow-redirects that referenced this issue Jan 14, 2021
nodejs under v15.5.1 has a bug: it will cache MODULE_NOT_FOUND when require a non-exits file([issue](nodejs/node#26926)). 

./debug.js try to require('debug'), if the 'debug' pkg not exits, nodejs will cache the result. Even install debug later in the same process(spawn npm install), require('debug') will still throw error

so, we should make 'debug' pacakge as dependencies.
overcache pushed a commit to overcache/follow-redirects that referenced this issue Jan 14, 2021
nodejs between v11.x - v15.5.1 has a bug: it will cache MODULE_NOT_FOUND when require a non-exits file([issue](nodejs/node#26926)). 

./debug.js try to require('debug'), if the 'debug' pkg not exits, nodejs will cache the result. Even install debug later in the same process(spawn npm install), require('debug') will still throw error

so, we should make 'debug' pacakge as dependencies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants