Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Parser } from 'acorn';
import bindings from 'bindings';
import { isIdentifierRead, isLoop, isVarLoop } from './utils/ast-helpers';
import { glob } from 'glob';
import { fsIgnoreEnoent } from './utils/fs-ignore-enoent';
import { getPackageBase } from './utils/get-package-base';
import { pregyp, nbind } from './utils/binary-locators';
import {
Expand Down Expand Up @@ -347,6 +348,7 @@ export default async function analyze(
ignore: assetDirPath + '/**/node_modules/**/*',
dot: true,
nodir: true,
fs: fsIgnoreEnoent,
});
files
.filter(
Expand Down Expand Up @@ -571,6 +573,7 @@ export default async function analyze(
mark: true,
ignore: wildcardDirPath + '/**/node_modules/**/*',
nodir: true,
fs: fsIgnoreEnoent,
});
files
.filter(
Expand Down
82 changes: 82 additions & 0 deletions src/utils/fs-ignore-enoent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import * as nativeFs from 'fs';

function createMockStats() {
return {
isDirectory: () => false,
isFile: () => false,
isSymbolicLink: () => false,
isBlockDevice: () => false,
isCharacterDevice: () => false,
isFIFO: () => false,
isSocket: () => false,
};
}

export const fsIgnoreEnoent = {
...nativeFs,
Comment thread
Harshalj2108 marked this conversation as resolved.
statSync: (path: any, ...args: any[]) => {
try {
return nativeFs.statSync(path, ...args as any);
} catch (err: any) {
if (err.code === 'ENOENT') {
return createMockStats() as any;
}
throw err;
}
},
lstatSync: (path: any, ...args: any[]) => {
try {
return nativeFs.lstatSync(path, ...args as any);
} catch (err: any) {
if (err.code === 'ENOENT') {
return createMockStats() as any;
}
throw err;
}
},
stat: (path: any, ...args: any[]) => {
const cb = args[args.length - 1];
if (typeof cb !== 'function') return (nativeFs.stat as any)(path, ...args);
args.pop();
return nativeFs.stat(path, ...args, (err, stats) => {
if (err && err.code === 'ENOENT') {
return cb(null, createMockStats());
}
return cb(err, stats);
});
},
lstat: (path: any, ...args: any[]) => {
const cb = args[args.length - 1];
if (typeof cb !== 'function') return (nativeFs.lstat as any)(path, ...args);
args.pop();
return nativeFs.lstat(path, ...args, (err, stats) => {
if (err && err.code === 'ENOENT') {
return cb(null, createMockStats());
}
return cb(err, stats);
});
},
promises: {
...nativeFs.promises,
stat: async (path: any, opts?: any) => {
try {
return await nativeFs.promises.stat(path, opts);
} catch (err: any) {
if (err.code === 'ENOENT') {
return createMockStats() as any;
}
throw err;
}
},
lstat: async (path: any, opts?: any) => {
try {
return await nativeFs.promises.lstat(path, opts);
} catch (err: any) {
if (err.code === 'ENOENT') {
return createMockStats() as any;
}
throw err;
}
},
},
};
2 changes: 2 additions & 0 deletions src/utils/sharedlib-emit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os from 'os';
import path from 'path';
import { glob } from 'glob';
import { fsIgnoreEnoent } from './fs-ignore-enoent';
import { getPackageBase } from './get-package-base';
import { Job } from '../node-file-trace';

Expand Down Expand Up @@ -28,6 +29,7 @@ export async function sharedLibEmit(p: string, job: Job) {
ignore:
pkgPath.replaceAll(path.sep, path.posix.sep) + '/**/node_modules/**/*',
dot: true,
fs: fsIgnoreEnoent,
},
);
await Promise.all(files.map((file) => job.emitFile(file, 'sharedlib', p)));
Expand Down
1 change: 1 addition & 0 deletions test/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const skipOnWindows = [
'yarn-workspaces-base-root',
'yarn-workspace-esm',
'asset-symlink',
'asset-symlink-broken',
'require-symlink',
];
const skipOnMac = [];
Expand Down
5 changes: 5 additions & 0 deletions test/unit/asset-symlink-broken/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

const path = require('path');
// This will trigger glob traversal in the directory
const dir = path.join(__dirname, './');
console.log(dir);
5 changes: 5 additions & 0 deletions test/unit/asset-symlink-broken/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

[
"package.json",
"test/unit/asset-symlink-broken/input.js"
]