oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.54k stars 2.78k forks source link

ESM importing CommonJS edge case (breaks `vite` with `vite-plugin-monaco-editor`) #3881

Open skrashevich opened 1 year ago

skrashevich commented 1 year ago

What version of Bun is running?

0.7.1

What platform is your computer?

Darwin 22.5.0 x86_64 i386

What steps can reproduce the bug?

% bun --bun vite dev

What is the expected behavior?

No response

What do you see instead?

% bun --bun vite dev
failed to load config from <cut>/web/vite.config.ts
error when starting dev server:
undefined
error: "vite" exited with code 1 (SIGHUP)

Additional information

vite.config.ts:

/// <reference types="vitest" />
import path from 'path';
import { defineConfig } from 'vite';
import preact from '@preact/preset-vite';
import monacoEditorPlugin from 'vite-plugin-monaco-editor';

// https://vitejs.dev/config/
export default defineConfig({
  define: {
    'import.meta.vitest': 'undefined',
  },
  plugins: [
    preact(),
    monacoEditorPlugin.default({
      customWorkers: [{ label: 'yaml', entry: 'monaco-yaml/yaml.worker' }],
      languageWorkers: ['editorWorkerService'], // we don't use any of the default languages
    }),
  ],
  test: {
    environment: 'jsdom',
    alias: {
      'testing-library': path.resolve(__dirname, './__test__/testing-library.js'),
    },
    setupFiles: ['./__test__/test-setup.ts'],
    includeSource: ['src/**/*.{js,jsx,ts,tsx}'],
    coverage: {
      reporter: ['text-summary', 'text'],
    },
    mockReset: true,
    restoreMocks: true,
    globals: true,
  },
});
Christopher2K commented 1 year ago

Hi, same issue here! I'm running bun in the official docker image

trnxdev commented 1 year ago

could you also provide package.json?

Christopher2K commented 1 year ago

@TiranexDev Here's mine if that can help

{
  "name": "@project/web",
  "version": "0.0.0",
  "description": "",
  "scripts": {
    "start": "vite",
    "dev": "vite",
    "build": "vite build",
    "serve": "vite preview",
    "typecheck": "tsc --noEmit"
  },
  "license": "MIT",
  "devDependencies": {
    "solid-devtools": "~0.27.6",
    "typescript": "~5.1.6",
    "vite": "~4.3.9",
    "vite-plugin-solid": "~2.7.0"
  },
  "dependencies": {
    "@project/internal": "workspace:*",
    "solid-js": "~1.7.8"
  }
}

Funny thing is that on my machine (Darwin 22.6.0 arm64 arm) it works. Inside the container, it doesn't

trnxdev commented 1 year ago

How did you install bun in container? Also is it a Docker container? If so then it's not recommended to install bun using curl, https://bun.sh/install lists a tab for Docker

skrashevich commented 1 year ago

How did you install bun in container?

The problem reproduces the same way in both the docker container and macOS

could you also provide package.json?

for example this and this

skrashevich commented 1 year ago

Funny thing is that on my machine (Darwin 22.6.0 arm64 arm) it works. Inside the container, it doesn't

Have you tried running vite dev inside a same docker container but on arm64 architecture?

skrashevich commented 1 year ago

replacing monacoEditorPlugin.default({ with monacoEditorPlugin({ fixes this problem in latest canary

skrashevich commented 1 year ago

Upd:

% bun --bun vite dev     

  VITE v4.4.7  ready in 1438 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
error: "vite" exited with code 11 (SIGSEGV)

Upd2: it start work after execute bun --bun vite optimize

paperdave commented 1 year ago

This is caused by incorrect handling of ESM importing CommonJS. (the monaco plugin is a common js module)

image
paperdave commented 1 year ago

i think this is an easy issue. for those looking to contribute, look in Module.ts or ImportMetaObject.ts and see how we handle __esModule, or more specifically how we handle when that isnt used.

skrashevich commented 1 year ago

can you suggest any workaround?

aryzing commented 1 year ago

Seems this issue is still happening. It every so often, when vite performs a hot reload, it crashes with exited with code 11 (SIGSEGV). This is happening with Bun v1.0.7. It fails every 10 - 20 hot reloads.

As a side effect, it messes up the terminal: user input is no longer visible.

github-actions[bot] commented 3 months ago

This issue is stale and may be closed due to inactivity. If you're still running into this, please leave a comment.

skrashevich commented 3 months ago

Wait for #9267

paperdave commented 3 months ago

why was this given the needs repro label? i literally have a repro and explainer given. the issue with this change is it's a breaking change.