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

Deno compile crash when try chdir #26175

Open
jlucaso1 opened this issue Oct 12, 2024 · 9 comments · May be fixed by #26203
Open

Deno compile crash when try chdir #26175

jlucaso1 opened this issue Oct 12, 2024 · 9 comments · May be fixed by #26203
Assignees
Labels
bug Something isn't working correctly compile related to the `deno compile` feature

Comments

@jlucaso1
Copy link

jlucaso1 commented Oct 12, 2024

Version: Deno 2.0.0

I've tried to compile a nextjs standalone compiled project and got this error when try to execute the compiled denojs version of this project:

error: Uncaught (in promise) NotSupported: The operation is not supported
    at Process.chdir (ext:deno_fs/30_fs.js:155:3)

Minimal repro:

import { fileURLToPath } from 'node:url'
import process from 'node:process'

const __dirname = fileURLToPath(new URL('.', import.meta.url))

process.chdir(__dirname)

deno compile -A main.js
./main

@marvinhagemeister marvinhagemeister added bug Something isn't working correctly compile related to the `deno compile` feature labels Oct 12, 2024
@MohammadSu1
Copy link
Contributor

@jlucaso1 I tried reproducing the issue using the same code you attached, but I added a log to see the output. As you can see in the attached pictures, it runs without any problem on my side. PS: I'm using Windows
next_js_code
next_js_terminal

@jlucaso1
Copy link
Author

jlucaso1 commented Oct 12, 2024

@MohammadSu1 i tested early in linux. Curious that works fine in windows 🤔 . Can you try in wsl?

@littledivy
Copy link
Member

littledivy commented Oct 13, 2024

This happens because import.meta.url points to a non-existent temp directory for compiled executables.

This seems to work:

$ deno compile main.js
$ mkdir /tmp/deno-compile-chdir-deno-compile/chdir-deno-compile # replace with tmp dir for your executable
$ ./chdir-deno-compile

littledivy added a commit to littledivy/deno that referenced this issue Oct 13, 2024
@littledivy littledivy self-assigned this Oct 13, 2024
@hedefalk
Copy link

I'm trying to poc using deno to create a standalone next.js executable with deno compile and I'm getting the same issue:

I'm for now just using the latest next.js scaffold and set the output to "standalone". Then after renaming the server.js to server.cjs I run:

✗ deno compile --allow-read --allow-net .next/standalone/server.cjs
Check file:///Users/viktor/dev/next-deno/deno-next/.next/standalone/server.cjs
Compile file:///Users/viktor/dev/next-deno/deno-next/.next/standalone/server.cjs to server

when running I get:

:fish: ./server
:white_check_mark: Granted all env access.
error: Uncaught (in promise) NotSupported: chdir '/var/folders/sj/sw26n18x7ts3jlc0gnlkwry00000gn/T/deno-compile-server/standalone': chdir '/var/folders/sj/sw26n18x7ts3jlc0gnlkwry00000gn/T/deno-compile-server/standalone'
    at Process.chdir (ext:deno_fs/30_fs.js:155:3)
    at Object.<anonymous> (file:///var/folders/sj/sw26n18x7ts3jlc0gnlkwry00000gn/T/deno-compile-server/standalone/server.cjs:6:9)
    at Object.<anonymous> (file:///var/folders/sj/sw26n18x7ts3jlc0gnlkwry00000gn/T/deno-compile-server/standalone/server.cjs:39:4)
    at Module._compile (node:module:745:34)
    at Object.loadCjs [as .cjs] (node:module:774:10)
    at Module.load (node:module:662:32)
    at Function.Module._load (node:module:534:12)
    at file:///var/folders/sj/sw26n18x7ts3jlc0gnlkwry00000gn/T/deno-compile-server/standalone/server.cjs:5:32


@hedefalk
Copy link

hedefalk commented Dec 20, 2024

The next standalone init script looks like so:

const path = require('path')

const dir = path.join(__dirname)

process.env.NODE_ENV = 'production'
process.chdir(__dirname)

const currentPort = parseInt(process.env.PORT, 10) || 3000
const hostname = process.env.HOSTNAME || '0.0.0.0'
……

So its failing on the process.chdir(__dirname) call. I've tried to change this to Deno.chdir too and I've used --allow-all and so forth but nothing seems to allow this cwd. If one just comments it out next fails, because it really is made to run from that directory:

✗ sudo ./server2
   ▲ Next.js 15.1.2
   - Local:        http://localhost:3000
   - Network:      http://0.0.0.0:3000

 ✓ Starting...
Error: Could not find a production build in the './.next' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
    at Object.runMicrotasks (ext:core/01_core.js:683:26)
    at Array.processTicksAndRejections (ext:deno_node/_next_tick.ts:59:10)
    at eventLoopTick (ext:core/01_core.js:172:29)

I don't understand the above entirely, but it seems that the next bootstrapping really works on a file system level, depending on the fact that the files are organized under a ".next" folder just beside the main entrypoint. So I'm wondering how just creating a missing temp folder would work? And how deno compile can track the dependencies if they are just file system, not imports/require from the entrypoint?

@hedefalk
Copy link

Oh, some progress. The way next does is a great fit for the include-flag of course. Some progress:

 deno compile --allow-all --unstable-ffi --output server2 --include ./.next/standalone/.next .next/standalone/server.cjs

 ./server2
   ▲ Next.js 15.1.2
   - Local:        http://localhost:3000
   - Network:      http://0.0.0.0:3000

 ✓ Starting...
 ✓ Ready in 34ms
Failed to update prerender cache for /index NotSupported: mkdir '/var/folders/sj/sw26n18x7ts3jlc0gnlkwry00000gn/T/deno-compile-server2/standalone/.next/server/app': mkdir '/var/folders/sj/sw26n18x7ts3jlc0gnlkwry00000gn/T/deno-compile-server2/standalone/.next/server/app'
    at Object.mkdir (ext:deno_fs/30_fs.js:199:9)
    at Object.mkdir (ext:deno_node/_fs/_fs_mkdir.ts:23:8)
    at <unknown> (ext:deno_node/internal/util.mjs:97:15)
    at Object.mkdir (ext:deno_node/internal/util.mjs:82:12)
    at eventLoopTick (ext:core/01_core.js:214:9) {
  name: "NotSupported"

so now it starts but fails on creating some prerender cache folder under the temp directory…

@hedefalk
Copy link

And also

TypeError: Cannot read properties of null (reading 'toUTCString')
    at Deno.stat.then.denoErrorToNodeError.syscall (ext:deno_node/_fs/_fs_stat.ts:149:32)
    at eventLoopTick (ext:core/01_core.js:175:7)

Sorry if I have derailed this issue to be specifically about running next.js under deno since that's not the title…

@ClementDreptin
Copy link

@hedefalk Have you been able to compile a Next.js server in standalone mode in the end?

@hedefalk
Copy link

hedefalk commented Feb 8, 2025

@hedefalk Have you been able to compile a Next.js server in standalone mode in the end?

no, not into something working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working correctly compile related to the `deno compile` feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants