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

Can't use fs with relative paths in svelte.config.js since the postinstall lifecycle hook has a different $PWD #5769

Closed
ghost opened this issue Jul 31, 2022 · 3 comments · Fixed by #5771

Comments

@ghost
Copy link

ghost commented Jul 31, 2022

Describe the bug

The most recent update of SvelteKit can not be installed on my project because of my use of fs.readFileSync and relative paths inside of svelte.config.js.
It seems like the added lifecycle hook executes the code of svelte.config.js inside of node_modules/@sveltejs/kit (according to process.cwd()), which of course breaks any relative paths and finally makes fs.readFileSync throw, which in turn makes npm update or npm install fail and not change anything.
I use a JSON file to populate the content of config.kit.prerender.entries, so accessing a file using Nodes fs is crucial.

Reproduction

🎩 Repository for reproduction
I quickly made a skeleton project using npm create svelte@latest and added the following four lines to the top of svelte.config.js:
https://github.com/martinszar/sveltekit-lifecycle-pwd-reproduction/blob/main/svelte.config.js#L1-L4

To observe the bug just clone the repo and run npm install. I additionally added a console.log marked with the 🚨 emoji, which will log process.cwd().

Logs

No response

System Info

System:
    OS: Linux 5.18 Fedora Linux 36 (Workstation Edition)
    CPU: (4) x64 Intel(R) Core(TM) i5-7500 CPU @ 3.40GHz
    Memory: 10.09 GB / 15.49 GB
    Container: Yes
    Shell: 3.5.0 - /usr/bin/fish
  Binaries:
    Node: 16.14.0 - /usr/bin/node
    npm: 8.13.1 - /usr/local/bin/npm
  Browsers:
    Firefox: 102.0

Severity

blocking an upgrade

Additional Information

No response

@Conduitry
Copy link
Member

Here

const cwd = event === 'postinstall' ? process.env.INIT_CWD ?? '' : process.cwd();
we've got some logic for looking for the svelte.config.js file in a different place depending on whether we're in a postinstall event or not. Presumably we could process.chdir() here rather than simply loading the config file from the appropriate directory.

@Conduitry
Copy link
Member

Conduitry commented Jul 31, 2022

That doesn't appear to quite work. I'm guessing the CWD needs to be changed back afterwards.
edit: Maybe I was just running into something weird with linking packages?

Also, for the record, you repro is trying to load "./.svelte.config.js" rather than "./svelte.config.js".

@Conduitry
Copy link
Member

Looks like the fix worked. Should be good now with next.401.

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

Successfully merging a pull request may close this issue.

1 participant