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

[PDE-4679] fix(cli): better handling of broken symlinks while copying files to temp directory during build process #737

Merged
merged 5 commits into from
Feb 1, 2024
Merged
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
29 changes: 24 additions & 5 deletions packages/cli/src/utils/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const os = require('os');
const path = require('path');

const fse = require('fs-extra');
const colors = require('colors/safe');

const fixHome = (dir) => {
const home = process.env.HOME || process.env.USERPROFILE;
Expand Down Expand Up @@ -127,16 +128,34 @@ const copyDir = async (src, dst, options) => {

const promises = files.map(async (file) => {
const srcItem = path.resolve(src, file);

let srcStat;
try {
srcStat = fse.statSync(srcItem);
} catch (err) {
// If the file is a symlink and the target doesn't exist, skip it.
if (fse.lstatSync(srcItem).isSymbolicLink()) {
console.warn(
colors.yellow(
`\n! Warning: symlink "${srcItem}" points to a non-existent file. Skipping!\n`
)
);
return null;
}

// otherwise, rethrow the error
throw err;
}

const srcIsFile = srcStat.isFile();

const dstItem = path.resolve(dst, file);
const stat = fse.statSync(srcItem);
const isFile = stat.isFile();
const dstExists = fileExistsSync(dstItem);

if (!options.filter(srcItem)) {
return null;
}

if (isFile) {
if (srcIsFile) {
if (dstExists) {
if (!options.clobber) {
options.onSkip(dstItem);
Expand All @@ -145,7 +164,7 @@ const copyDir = async (src, dst, options) => {
fse.removeSync(dstItem);
}

await copyFile(srcItem, dstItem, stat.mode);
await copyFile(srcItem, dstItem, srcStat.mode);
options.onCopy(dstItem);
} else {
let shouldCopyRecursively = true;
Expand Down
Loading