Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

fs.copyFile and snapshot filesystem #420

Closed
jsonmaur opened this issue May 14, 2018 · 25 comments · Fixed by #1484
Closed

fs.copyFile and snapshot filesystem #420

jsonmaur opened this issue May 14, 2018 · 25 comments · Fixed by #1484

Comments

@jsonmaur
Copy link

Trying to copy a file from the snapshot filesystem to the real filesystem results in the following error:
ENOENT: no such file or directory. Seems to happen in all versions after 4.2.4.

Example code that should work:

fs.copyFile(
  path.resolve(__dirname, 'some_file.json'),
  path.resolve(process.cwd(), 'some_new_file.json')
)
@jsonmaur
Copy link
Author

Using ncp as a temporary workaround since it doesn't rely on the fs.copyFile function.

@jdziat
Copy link

jdziat commented Jun 14, 2018

Another option that works is to use:

const fileBuffer = fs.readFileSync(path.resolve(__dirname, 'some_file.json'))
 
fs.writeFileSync(self.dynLibraryPath, fileBuffer)

@AxelTerizaki
Copy link

I can confirm your little trick worked out for me o/

@sqliang
Copy link

sqliang commented Apr 3, 2019

Trying to copy a file from the snapshot filesystem to the real filesystem results in the following error:
ENOENT: no such file or directory. Seems to happen in all versions after 4.2.4.

Example code that should work:

fs.copyFile(
  path.resolve(__dirname, 'some_file.json'),
  path.resolve(process.cwd(), 'some_new_file.json')
)

Maybe pkg does not kidnap function fs.copyFile. I meet the same issue, and I resolve it by replace copyFile with readFile & writeFile:

// previously:
function copyFile(sourcePath, targetPath) {
    generatedFilePaths.push(targetPath);
    fs.copyFileSync(sourcePath, targetPath);
}
// now:
function copyFile(sourcePath, targetPath) {
    generatedFilePaths.push(targetPath);
    const sourceData = fs.readFileSync(sourcePath, ENCODING);
    fs.writeFileSync(targetPath, sourceData, ENCODING);
}

@paulocoghi
Copy link

What can we do when using fs-extra?

I get the same error when calling a fs.copy, but I would not like to manually rewrite it (to be able to apply those workarounds), since there is a lot of things made on fs-extra.

@jdziat
Copy link

jdziat commented Apr 18, 2019

You need to not use the copy method from the fs-extra package. It must do something with path that pkg intercepts/changes.

@paulocoghi
Copy link

It seems to be a regression bug, since it worked fine until 4.2.6.

@jingsam
Copy link

jingsam commented Dec 24, 2019

This issue is still not solved. Any progress on this issue? @igorklopov

@jingsam
Copy link

jingsam commented Jul 2, 2020

Hi, @igorklopov thanks for your great work. However, this issue is indeed a pain in our application and still not fixed for two years. Do you have any plans to get this issue to be solved, or suggest some work rounds. Many thanks and wish you a good day.

@medikoo
Copy link

medikoo commented Jul 2, 2020

We workaround this issue by loading the following patch at beginning of the main module: https://github.com/serverless/serverless/blob/94ff3b22ab13afc60fb4e672520b4db527ee0432/lib/utils/standalone-patch.js

@ryanaltair
Copy link

Another option that works is to use:

const fileBuffer = fs.readFileSync(path.resolve(__dirname, 'some_file.json'))
 
fs.writeFileSync(self.dynLibraryPath, fileBuffer)

this work with pkg 4.4.9
with node14

@github-actions
Copy link

This issue is stale because it has been open 90 days with no activity. Remove the stale label or comment or this will be closed in 5 days. To ignore this issue entirely you can add the no-stale label

@github-actions github-actions bot added the Stale label Apr 20, 2021
@medikoo
Copy link

medikoo commented Apr 20, 2021

.

@fbmiranda7
Copy link

bump, cant copy file from snapshot

@dukei
Copy link

dukei commented Jun 27, 2021

It is really annoying bug. Why is not it getting fixed?

@github-actions
Copy link

This issue is stale because it has been open 90 days with no activity. Remove the stale label or comment or this will be closed in 5 days. To ignore this issue entirely you can add the no-stale label

@github-actions github-actions bot added the Stale label Sep 26, 2021
@jingsam
Copy link

jingsam commented Sep 26, 2021

bump

@github-actions github-actions bot removed the Stale label Sep 27, 2021
@github-actions
Copy link

This issue is stale because it has been open 90 days with no activity. Remove the stale label or comment or this will be closed in 5 days. To ignore this issue entirely you can add the no-stale label

@github-actions github-actions bot added the Stale label Dec 26, 2021
@paulocoghi
Copy link

.

@robertsLando
Copy link
Contributor

Hi everyone, I use pkg like you for my projects and when I find a bug I try to fix it my self, fixing this is not so difficult as it just require to add a patch like the one specified by @medikoo in prelude/bootstrap.js.

Please if you use this project help to keep it maintained by contributing to it.

Thanks

@robertsLando
Copy link
Contributor

PR ready to fix this

@jdziat
Copy link

jdziat commented Jan 20, 2022

@robertsLando I like it, i think the only thing is that it may lead people to not understand context when working with this package. If they inadvertently forget to include a file in the snapshot and it loads a file from the filesystem instead it could cause some issues or in a worst case scenario a vulnerability. I think that's why this hasn't been patched/implemented.

@robertsLando
Copy link
Contributor

@jdziat IMO the patch should be integrated to bootstrap as lot users are having this issue

@jdziat
Copy link

jdziat commented Jan 20, 2022

@robertsLando Not disagreeing but you may want to make it possible to turn this off via a flag.

@yunylz
Copy link

yunylz commented Jul 17, 2023

bump

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.