Skip to content

Commit bfde44e

Browse files
authored
fix(image-optimization): Workaround for broken symlink dereferencing in Node 22.17.0 and 22.17.1 (#934)
* fix(image-optimization): Workaround for broken symlink dereferencing in Node 22.17.0 and 22.17.1 * changeset * review
1 parent 3b23495 commit bfde44e

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

.changeset/hip-lies-matter.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
fix: Workaround for broken symlink dereferencing in Node 22.17.0 and 22.17.1
6+
7+
The `dereference: true` option in `fs.cpSync()` is broken on version 22.17.0 and 22.17.1. This fix will do it manually for the binaries in `node_modules/.bin`.
8+
9+
Issue in Node: https://github.com/nodejs/node/issues/59168

packages/open-next/src/build/installDeps.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,37 @@ export function installDependencies(
4848
fs.cpSync(
4949
path.join(tempInstallDir, "node_modules"),
5050
path.join(outputDir, "node_modules"),
51-
5251
{ recursive: true, force: true, dereference: true },
5352
);
5453

54+
// This is a workaround for Node `22.17.0` and `22.17.1`
55+
// https://github.com/nodejs/node/issues/59168
56+
const nodeVersion = process.version;
57+
if (nodeVersion === "v22.17.0" || nodeVersion === "v22.17.1") {
58+
const tempBinDir = path.join(tempInstallDir, "node_modules", ".bin");
59+
const outputBinDir = path.join(outputDir, "node_modules", ".bin");
60+
61+
for (const fileName of fs.readdirSync(tempBinDir)) {
62+
const symlinkPath = path.join(tempBinDir, fileName);
63+
const stat = fs.lstatSync(symlinkPath);
64+
65+
if (stat.isSymbolicLink()) {
66+
const linkTarget = fs.readlinkSync(symlinkPath);
67+
const realFilePath = path.resolve(tempBinDir, linkTarget);
68+
69+
const outputFilePath = path.join(outputBinDir, fileName);
70+
71+
if (fs.existsSync(outputFilePath)) {
72+
fs.unlinkSync(outputFilePath);
73+
}
74+
75+
fs.copyFileSync(realFilePath, outputFilePath);
76+
fs.chmodSync(outputFilePath, "755");
77+
logger.debug(`Replaced symlink ${fileName} with actual file`);
78+
}
79+
}
80+
}
81+
5582
// Cleanup tempDir
5683
fs.rmSync(tempInstallDir, { recursive: true, force: true });
5784
logger.info(`Dependencies installed for ${name}`);

0 commit comments

Comments
 (0)