Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Fix fs readdir and readdirSync #1299

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 31 additions & 2 deletions prelude/bootstrap.js
Expand Up @@ -1188,7 +1188,7 @@ function payloadFileSync(pointer) {

const options = readdirOptions(options_, false);

if (!options || options.withFileTypes) {
if (!options) {
return ancestor.readdirSync.apply(fs, arguments);
}

Expand All @@ -1209,7 +1209,7 @@ function payloadFileSync(pointer) {

const options = readdirOptions(options_, true);

if (!options || options.withFileTypes) {
if (!options) {
return ancestor.readdir.apply(fs, arguments);
}

Expand Down Expand Up @@ -2065,6 +2065,35 @@ function payloadFileSync(pointer) {
const modulePath = revertMakingLong(args[1]);
const moduleBaseName = path.basename(modulePath);
const moduleFolder = path.dirname(modulePath);
const unknownModuleErrorRegex =
/([^:]+): cannot open shared object file: No such file or directory/;

function tryImporting(_tmpFolder, previousErrorMessage) {
try {
const res = ancestor.dlopen.apply(process, args);
return res;
} catch (e) {
if (e.message === previousErrorMessage) {
// we already tried to fix this and it didn't work, give up
throw e;
}
if (e.message.match(unknownModuleErrorRegex)) {
// this case triggers on linux, the error message give us a clue on what dynamic linking library
// is missing.
// some modules are packaged with dynamic linking and needs to open other files that should be in
// the same directory, in this case, we write this file in the same /tmp directory and try to
// import the module again

const moduleName = e.message.match(unknownModuleErrorRegex)[1];
const importModulePath = path.join(moduleFolder, moduleName);

if (!fs.existsSync(importModulePath)) {
throw new Error(
`INTERNAL ERROR this file doesn't exist in the virtual file system :${importModulePath}`
);
}
const moduleContent1 = fs.readFileSync(importModulePath);
const tmpModulePath1 = path.join(_tmpFolder, moduleName);

if (insideSnapshot(modulePath)) {
const moduleContent = fs.readFileSync(modulePath);
Expand Down
Empty file.
Empty file.
37 changes: 37 additions & 0 deletions test/test-1103-readdirsync-withfiletypes/main.js
@@ -0,0 +1,37 @@
#!/usr/bin/env node

'use strict';

const path = require('path');
const assert = require('assert');
const utils = require('../utils.js');

assert(!module.parent);
assert(__dirname === process.cwd());

const target = process.argv[2] || 'host';
const input = './read.js';
const output = './run-time/test-output.exe';

utils.mkdirp.sync(path.dirname(output));
utils.pkg.sync([
'--target',
target,
'--assets',
'files/*.js',
'--output',
output,
'.',
]);

let left, right;
left = utils.spawn.sync('node', [path.basename(input)], {
cwd: path.dirname(input),
});

right = utils.spawn.sync(output, [], {
cwd: path.dirname(input),
});

assert.strictEqual(left, right);
utils.vacuum.sync(path.dirname(output));
6 changes: 6 additions & 0 deletions test/test-1103-readdirsync-withfiletypes/package.json
@@ -0,0 +1,6 @@
{
"bin": "read.js",
"pkg": {
"scripts": "files/*.js"
}
}
29 changes: 29 additions & 0 deletions test/test-1103-readdirsync-withfiletypes/read.js
@@ -0,0 +1,29 @@
'use strict';

const fs = require('fs');
const path = require('path');

console.log('Starting sync read');

console.log(
serializeFiles(
fs.readdirSync(path.join(__dirname, 'files'), { withFileTypes: true })
)
);

console.log('Finishing sync read');

console.log('Starting async read');

fs.readdir(
path.join(__dirname, 'files'),
{ withFileTypes: true },
(_err, files) => {
console.log(serializeFiles(files));
console.log('Finishing async read');
}
);

function serializeFiles(files) {
return files.map((file) => `name: ${file.name}, isFile: ${file.isFile()}`);
}