Skip to content

Commit 1ee8b67

Browse files
committed
workaround process.exit() not exiting in node
1 parent 5c56e07 commit 1ee8b67

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

cmd/esbuild/main.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ func main() {
163163
sendPings := false
164164
isWatch := false
165165
isWatchForever := false
166+
isServe := false
166167

167168
// Do an initial scan over the argument list
168169
argsEnd := 0
@@ -226,7 +227,13 @@ func main() {
226227
isWatch = true
227228
} else if arg == "--watch=forever" {
228229
arg = "--watch"
230+
isWatch = true
229231
isWatchForever = true
232+
} else if arg == "--serve" ||
233+
strings.HasPrefix(arg, "--serve=") ||
234+
strings.HasPrefix(arg, "--servedir=") ||
235+
strings.HasPrefix(arg, "--serve-fallback=") {
236+
isServe = true
230237
}
231238

232239
// Strip any arguments that were handled above
@@ -295,31 +302,30 @@ func main() {
295302
exitCode = cli.Run(osArgs)
296303
}
297304
} else {
298-
isServeOrWatch := false
299-
nonFlagCount := 0
300-
for _, arg := range osArgs {
301-
if !strings.HasPrefix(arg, "-") {
302-
nonFlagCount++
303-
} else if arg == "--watch" ||
304-
arg == "--watch=true" ||
305-
arg == "--serve" ||
306-
strings.HasPrefix(arg, "--serve=") ||
307-
strings.HasPrefix(arg, "--servedir=") ||
308-
strings.HasPrefix(arg, "--serve-fallback=") {
309-
isServeOrWatch = true
310-
}
311-
}
312-
313-
if !isServeOrWatch {
305+
if !isWatch && !isServe {
314306
// If this is not a long-running process and there is at most a single
315307
// entry point, then disable the GC since we're just going to allocate
316308
// a bunch of memory and then exit anyway. This speedup is not
317309
// insignificant. We don't do this when there are multiple entry points
318310
// since otherwise esbuild could unnecessarily use much more memory
319311
// than it might otherwise need to process many entry points.
312+
nonFlagCount := 0
313+
for _, arg := range osArgs {
314+
if !strings.HasPrefix(arg, "-") {
315+
nonFlagCount++
316+
}
317+
}
320318
if nonFlagCount <= 1 {
321319
debug.SetGCPercent(-1)
322320
}
321+
} else if isServe && isServeUnsupported() {
322+
// The development server isn't supported on WebAssembly, so we will
323+
// immediately call "os.Exit(1)" below, which will call "process.exit(1)"
324+
// in node. However, node has a bug/feature where any pending calls to
325+
// "fs.read(process.stdin.fd)" hold up "process.exit()" without seemingly
326+
// any way to stop this from happening. So to avoid this bug/feature,
327+
// we explicitly avoid listening to stdin in this case (when we know
328+
// that we are about to exit due to an invalid flag).
323329
} else if !isStdinTTY && !isWatchForever {
324330
// If stdin isn't a TTY, watch stdin and abort in case it is closed.
325331
// This is necessary when the esbuild binary executable is invoked via
@@ -344,7 +350,7 @@ func main() {
344350
logger.PrintTextWithColor(os.Stderr, options.Color, func(colors logger.Colors) string {
345351
return fmt.Sprintf("%s[watch] stopped automatically because stdin was closed (use \"--watch=forever\" to keep watching even after stdin is closed)%s\n", colors.Dim, colors.Reset)
346352
})
347-
} else if isServeOrWatch {
353+
} else if isServe {
348354
logger.PrintTextWithColor(os.Stderr, options.Color, func(colors logger.Colors) string {
349355
return fmt.Sprintf("%s[serve] stopped automatically because stdin was closed (keep stdin open to continue serving)%s\n", colors.Dim, colors.Reset)
350356
})

cmd/esbuild/main_other.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ func createCpuprofileFile(osArgs []string, cpuprofileFile string) func() {
5555
f.Close()
5656
}
5757
}
58+
59+
func isServeUnsupported() bool {
60+
return false
61+
}

cmd/esbuild/main_wasm.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ func createCpuprofileFile(osArgs []string, cpuprofileFile string) func() {
2323
logger.PrintErrorToStderr(osArgs, "The \"--cpuprofile\" flag is not supported when using WebAssembly")
2424
return nil
2525
}
26+
27+
func isServeUnsupported() bool {
28+
return true
29+
}

0 commit comments

Comments
 (0)