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

fs, cjs modules: failed require prevents valid require from loading a module just written by writeFileSync #36638

Closed
vsemozhetbyt opened this issue Dec 26, 2020 · 2 comments
Labels
module Issues and PRs related to the module subsystem.

Comments

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Dec 26, 2020

  • Version: 15.5.0
  • Platform: Windows 10 x64
  • Subsystem: fs, cjs modules

What steps will reproduce the bug?

It seems there may be some race conditions.

Error (cannot find module...):

const fs = require('fs');

try { require('./test.json'); } catch (err) {}

fs.writeFileSync('test.json', '[]');
console.log(require('./test.json'));

With timeout — OK:

const fs = require('fs');

try { require('./test.json'); } catch (err) {}

fs.writeFileSync('test.json', '[]');
setTimeout(() => { console.log(require('./test.json')); }, 1000);

With callback — OK:

const fs = require('fs');

try { require('./test.json'); } catch (err) {}

fs.writeFile('test.json', '[]', () => { console.log(require('./test.json')); });

Promisified — OK:

const fs = require('fs/promises');

try { require('./test.json'); } catch (err) {}

(async function(path) {
  await fs.writeFile('test.json', '[]');
  console.log(require('./test.json'));
})();

Without failed try — OK:

const fs = require('fs');

fs.writeFileSync('test.json', '[]');
console.log(require('./test.json'));

Previously reported in nodejs/help#3144

@targos targos added the module Issues and PRs related to the module subsystem. label Dec 26, 2020
@RaisinTen RaisinTen added the confirmed-bug Issues with confirmed bugs. label Dec 26, 2020
@BridgeAR
Copy link
Member

It's not a race condition. All sync require calls use a single cache that's invalidated afterwards. Thus, as soon as there's anything async, the file is possible to be found.

@targos targos removed the confirmed-bug Issues with confirmed bugs. label Dec 28, 2020
@GrosSacASac
Copy link
Contributor

Maybe require should not be cached on failure

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