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

cwd ignored when copying files #107

Closed
tinfoil-knight opened this issue Feb 19, 2023 · 6 comments
Closed

cwd ignored when copying files #107

tinfoil-knight opened this issue Feb 19, 2023 · 6 comments

Comments

@tinfoil-knight
Copy link

tinfoil-knight commented Feb 19, 2023

I recently migrated to the newer v9.0.1 & updated some code as per: https://github.com/sindresorhus/cpy/releases/tag/v9.0.0

fixtures/directory/
└── test
    └── index.js

I'm trying to copy the test directory to some other temporary directory but only the index.js file gets copied directly without being under the test directory.

I've created a minimal reproduction here: https://github.com/KK-Learning-Gym/cpy-path-err.

Relevant lines are here: https://github.com/KK-Learning-Gym/cpy-path-err/blob/2140c4814dbf905d2bb6ed03354323fe22a69c12/index.mjs#L12-L20

I was trying to debug this a little bit. It seems like the cwd isn't considered under the if (path.isAbsolute(destination)) here:

cpy/index.js

Lines 96 to 98 in 15f9557

if (path.isAbsolute(destination)) {
return path.join(destination, entry.name);
}

@mfukala
Copy link

mfukala commented Feb 20, 2023

Files from source directory are copied but to themselves (into their locations in source directory) instead of into dest directory.

I had to work it around by prefixing patterns with the source directory like this:

await cpy([
    '*',
    'ignore*'
], dest, {
    cwd: source,
})

to

await cpy([
    `${source}/*`,
    `!${source}/ignore*`
], dest,)

@danielweck
Copy link

I am experiencing the same bug with cpy-cli v5 but only in some cases ... very strange bug, yet 100% reproducible (I am trying to find out why ... seems related to CWD pointing to a subfolder inside the current folder, otherwise if it's a parent folder or the local root folder, then it works)

@danielweck
Copy link

Related issue? #113

Seems to be hinting at the same code:

cpy/index.js

Lines 96 to 98 in 15f9557

if (path.isAbsolute(destination)) {
return path.join(destination, entry.name);
}

@danielweck
Copy link

danielweck commented Jul 6, 2023

Ah, I debugged cpy and I can see incorrect behaviour at this line:

cpy/index.js

Line 113 in 38c5d85

return path.join(options.cwd, destination, path.relative(options.cwd, entry.path));

I managed to create a minimal repro using the low-level NodeJS path API via the command line:

Assuming my CWD is /absolute/folder/:

node -e "const cwd='./'; const destination='../parent_folder/'; const entry='/absolute/folder/subfolder/file.txt'; const relative='subfolder/file.txt'; const path = require('path');console.log(path.join(cwd, destination, relative));"

=>
../parent_folder/subfolder/file.txt

Now assuming my CWD is /absolute/:

node -e "const cwd='./folder'; const destination='../parent_folder/'; const entry='/absolute/folder/subfolder/file.txt'; const relative='folder/subfolder/file.txt'; const path = require('path');console.log(path.join(cwd, destination, relative));"

parent_folder/folder/subfolder/file.txt

-(notice the missing ../ prefix)- CLARIFICATION: the missing prefix is correct, but the consumer of the returned relative path from the call to preprocessDestinationPath() incorrectly resolves the absolute path (it generates the files hierarchy inside the /absolute/folder/ subfolder instead of my CWD which is /absolute/)

UPDATE:

cpy/index.js

Line 270 in 38c5d85

await copyFile(entry.path, to, {...options, onProgress: fileProgressHandler});

options.cwd is not my actual CWD, but the flag --cwd ... this causes the relative path sub_folder/inner/file.txt to resolve against an incorrect location.

In concrete terms, this fails in my project:

cpy ./data/ ../target/ --cwd=./sub/ (./sub/ contains data/)

... whereas this succeeds:

cpy ./data/ ../parent/inner/" --cwd=./ (./ contains data/)

cpy ./data/ ../another_parent" --cwd=./parent (./parent contains data/)

@danielweck
Copy link

danielweck commented Jul 6, 2023

Ah, it's a regression / breaking change in cp-file!

https://github.com/sindresorhus/cp-file/blob/v10.0.0/index.js#L77-L79

https://github.com/sindresorhus/cp-file/blob/v10.0.0/index.js#L104-L111

vs.

https://github.com/sindresorhus/cp-file/blob/v9.1.0/index.js#L57-L60

https://github.com/sindresorhus/cp-file/blob/v9.1.0/index.js#L90-L93

Note the added code:

if (options.cwd) {
  ({sourcePath, destinationPath} = resolvePath(options.cwd, sourcePath, destinationPath));
}

See PR:

sindresorhus/copy-file#46

@danielweck
Copy link

danielweck commented Jul 6, 2023

My workaround / temporary fix is: add cwd: null immediately after the ...options spread in the copyFile() call:

await copyFile(entry.path, to, {...options, cwd: null, onProgress: fileProgressHandler});

For now I just use a postinstall script in my package.json to monkey-patch cpy:

"postinstall": "node -e \"const fs = require('fs');let s = fs.readFileSync('node_modules/cpy/index.js', { encoding: 'utf8' });s = s.replace(/options, onProgress:/, 'options, cwd: null, onProgress:');fs.writeFileSync('node_modules/cpy/index.js', s, { encoding: 'utf8' });\"",

@tinfoil-knight tinfoil-knight closed this as not planned Won't fix, can't repro, duplicate, stale Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants