question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dev-time Rendering isolation - Preventing naive file reading from working in dev but not in prod

See original GitHub issue

Describe the problem

I’m not sure if this is a bug so much as a discussion I’d like to have. One basic premise of sveltekit is that we have a local dev experience that matches closely to the production experience.

I find that one area where this promise is broken is when you try to render-on-demand rather than prerender, because it is very common for serverless environments (netlify, vercel et al) to separately bundle and run the serverless function. So, if I have a page that is only rendered on demand, its very easy for me to code a file read that works in dev, but then breaks when done in prod:

import { resolve } from 'path';
import { promises as fs } from 'fs';

const x = await fs.readFile(resolve('./podcasts.yml'), 'utf8')

this code works fine in dev, but then deploy it and…

{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"Error: ENOENT: no such file or directory, open '/var/task/podcasts.yml'","trace":["Runtime.UnhandledPromiseRejection: Error: ENOENT: no such file or directory, open '/var/task/podcasts.yml'"," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:400:28)"," at processPromiseRejections (internal/process/promises.js:245:33)"," at processTicksAndRejections (internal/process/task_queues.js:96:32)"]}

Describe the proposed solution

i think we should try to shift this problem left - if the page is serverless, actually compile the function and make it properly isolated as it would be in prod

Alternatives considered

do nothing, let people fumble around with this new serverless rendering paradigm 😦

for what its worth i’m a veteran Netlify user, spent multiple hours trying to figure out how to get around this and still havent got anything i’m really happy with (apart from brute force copying the file postbuild into the built folder, which really feels super janky and not great)

Importance

would make my life easier

Additional Information

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:3
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
Rich-Harriscommented, Jul 20, 2022

As part of #4296, we’re thinking about how to prevent client-side code from accidentally importing secret environment variables. The current plan is to traverse the module graph from client-side entry points (i.e. pages, plus maybe hooks/client.js in future) in both dev and build and throwing an error if a server-only module is imported. (For now, that just means $app/env/private, but it could include endpoints, or modules with a certain naming convention like utils.server.js or whatever.)

I digress. The point is that if we have the ability to say that some files can’t import certain modules, then it should be possible to say (for example) that if an endpoint imports node:fs or other Node built-ins and isn’t marked as prerenderable (#4093) then it’s an error.

This isn’t a complete solution to the problem of making the dev server environment a high fidelity simulation of the production environment — it doesn’t address platform-specific things like Durable Objects, or differences in the runtimes. But it would at least solve the specific problem that motivated this issue, namely that it’s confusing to be able to use Node built-ins in dev and preview but not in production.

0reactions
Rich-Harriscommented, Apr 7, 2022

If the dev server was started via vc dev or netlify dev etc then the problem becomes more solvable. I’m just not sure what that looks like

Read more comments on GitHub >

github_iconTop Results From Across the Web

Isolated Storage | Microsoft Learn
With isolated storage, your code no longer needs unique paths to specify safe locations in the file system, and data is protected from...
Read more >
Shadow CLJS User's Guide
shadow-cljs provides everything you need to compile your ClojureScript projects with a focus on simplicity and ease of use.
Read more >
relay.dev
Relay's compiler aggregates and optimizes the data requirements for your entire app, so that they can be efficiently fetched in a single GraphQL...
Read more >
Releases - styled-components
$as and $forwardedAs will be removed in the next major version, ... if you get a lot of warnings at dev time from...
Read more >
rushstack/rushstack - Gitter
Hello. I'm trying to setup my packages using Rush, but I'm stuck with the setup: I cannot find a way to locally test...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found