diff --git a/.changeset/hip-lies-matter.md b/.changeset/hip-lies-matter.md new file mode 100644 index 000000000..33a3bcdd8 --- /dev/null +++ b/.changeset/hip-lies-matter.md @@ -0,0 +1,9 @@ +--- +"@opennextjs/aws": patch +--- + +fix: Workaround for broken symlink dereferencing in Node 22.17.0 and 22.17.1 + +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`. + +Issue in Node: https://github.com/nodejs/node/issues/59168 \ No newline at end of file diff --git a/packages/open-next/src/build/installDeps.ts b/packages/open-next/src/build/installDeps.ts index bd475527e..40db056ed 100644 --- a/packages/open-next/src/build/installDeps.ts +++ b/packages/open-next/src/build/installDeps.ts @@ -48,10 +48,37 @@ export function installDependencies( fs.cpSync( path.join(tempInstallDir, "node_modules"), path.join(outputDir, "node_modules"), - { recursive: true, force: true, dereference: true }, ); + // This is a workaround for Node `22.17.0` and `22.17.1` + // https://github.com/nodejs/node/issues/59168 + const nodeVersion = process.version; + if (nodeVersion === "v22.17.0" || nodeVersion === "v22.17.1") { + const tempBinDir = path.join(tempInstallDir, "node_modules", ".bin"); + const outputBinDir = path.join(outputDir, "node_modules", ".bin"); + + for (const fileName of fs.readdirSync(tempBinDir)) { + const symlinkPath = path.join(tempBinDir, fileName); + const stat = fs.lstatSync(symlinkPath); + + if (stat.isSymbolicLink()) { + const linkTarget = fs.readlinkSync(symlinkPath); + const realFilePath = path.resolve(tempBinDir, linkTarget); + + const outputFilePath = path.join(outputBinDir, fileName); + + if (fs.existsSync(outputFilePath)) { + fs.unlinkSync(outputFilePath); + } + + fs.copyFileSync(realFilePath, outputFilePath); + fs.chmodSync(outputFilePath, "755"); + logger.debug(`Replaced symlink ${fileName} with actual file`); + } + } + } + // Cleanup tempDir fs.rmSync(tempInstallDir, { recursive: true, force: true }); logger.info(`Dependencies installed for ${name}`);