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

bun build --compile has different behavior with bun run #11442

Open
oopsmonk opened this issue May 29, 2024 · 6 comments
Open

bun build --compile has different behavior with bun run #11442

oopsmonk opened this issue May 29, 2024 · 6 comments
Labels
bug Something isn't working bundler Something to do with the bundler

Comments

@oopsmonk
Copy link

What version of Bun is running?

1.1.10

What platform is your computer?

Linux 5.15.0-107-generic x86_64 x86_64

What steps can reproduce the bug?

Create a bun project via bun init
read an array from a file and print out the elements

index.ts

import list from "./list.json";
console.log("Hello via Bun! v"   Bun.version);
for (const item of list) {
  console.log(item);
}

list.json

["山姆", "Mark", "包Sir"]

build and run the app with bun build --compile

$ bun build --compile ./index.ts --outfile ./bin/app
   [5ms]  bundle  2 modules
 [101ms] compile  ./bin/app
$ ./bin/app
Hello via Bun! v1.1.10
å±±å§
Mark
å
Sir

What is the expected behavior?

the output should be the same as bun run ./index.ts

$ bun run ./index.ts 
Hello via Bun! v1.1.10
山姆
Mark
包Sir

What do you see instead?

got garbled characters

$ ./bin/app
Hello via Bun! v1.1.10
å±±å§
Mark
å
Sir

Additional information

No response

@oopsmonk oopsmonk added the bug Something isn't working label May 29, 2024
@oopsmonk oopsmonk changed the title bun build --compile has difference behavior with bun run bun build --compile has different behavior with bun run May 29, 2024
@robobun robobun changed the title bun build --compile has different behavior with bun run bun build --compile has different behavior with bun run May 29, 2024
@BANG88
Copy link

BANG88 commented Aug 31, 2024

‌‌‌‌HELLO, I also encountered the same problem. It took me a long time to find out that it was an issue with bun. Has this problem been resolved?

@BANG88
Copy link

BANG88 commented Aug 31, 2024

There is no garbled text when running, but after building, it becomes garbled.

The garbled part in the code is a JSON file.

// zh-CN.json
"bot": {
    "setup": {
      "token_missing": "BOT_TOKEN 未在环境变量中设置",
      "setup_complete": "Bot 设置完成"
    }
  }
// import zhCN from "./zh-CN.json";

  • bun version 1.1.26
"scripts": {
    "dev": "bun run --hot src/index.ts",
    "build": "bun build ./src/ --minify --outfile ./dist/index.js --target bun",
    "start": "bun run ./dist/index.js"
  }

@BANG88
Copy link

BANG88 commented Aug 31, 2024

The program has been written and is ready for release, but the issue of garbled text cannot be resolved. I'm really sorry if I'm bothering you. I have nowhere else to find the answer and hope to get your help.

@Jarred-Sumner

😢

@Jarred-Sumner
Copy link
Collaborator

@BANG88 this is an encoding issue where we are incorrectly treating JSON as latin1 when it should be utf8, or vice versa

to workaround this issue, you could try using a macro

import {text} from './text' with {type: "macro"};

const bot = JSON.parse(text("./zh-CN.json"));

text.js

export function text(path) {
   return require("fs").readFileSync(path, "utf-8");
}

@BANG88
Copy link

BANG88 commented Aug 31, 2024

Thank you for replying so quickly. I just created a repro demo.

https://github.com/BANG88/bun-chinese-garbled

@BANG88
Copy link

BANG88 commented Aug 31, 2024

‌‌‌‌‌I temporarily changed the JSON file to a zh-CN.ts file export, and it looks normal now.

export default {
  bot: {
    setup: {
      token_missing: "BOT_TOKEN 未在环境变量中设置",
      setup_complete: "Bot 设置完成",
    },
  }
}

😆 🚀

Thank you again.

@paperdave paperdave added the bundler Something to do with the bundler label Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bundler Something to do with the bundler
Projects
None yet
Development

No branches or pull requests

4 participants