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

Importing .json with non-ascii characters within bun build --target=bun does not encode to disk as latin1 #10851

Open
eslym opened this issue May 5, 2024 · 4 comments
Labels
bug Something isn't working transpiler parser || printer

Comments

@eslym
Copy link

eslym commented May 5, 2024

What version of Bun is running?

1.1.7+b0b7db5c0 (issue since v1.1.5)

What platform is your computer?

Linux 5.15.146.1-microsoft-standard-WSL2 x86_64 unknown

What steps can reproduce the bug?

// @bun
console.log("测试");

What is the expected behavior?

p/s: running without // @bun

$ bun test.js
测试

What do you see instead?

$ bun test.js
æµè¯

Additional information

this issue exists since v1.1.5 (i just confirmed the cause recently)

@eslym eslym added the bug Something isn't working label May 5, 2024
@pfgithub
Copy link
Contributor

pfgithub commented May 5, 2024

@bun files must be ascii-only to reduce memory usage. If the file was generated by a bun command, then this is a bug. Bun should escape non-ascii strings when generating a // @bun file.

bun build is working for me on 1.1.7

// a.js
console.log("测试");
bun build --target=bun a.js
// @bun
// a.js
console.log("\u6D4B\u8BD5");

@eslym
Copy link
Author

eslym commented May 5, 2024

yes, it is generated from bun build when import '*.json',

repo having issue: https://github.com/eslym/dcyt-bot-v2

bun build/index.ts

i did tried to build with bun 1.1.4, it still not encoded properly but 1.1.4 able to understand unicode chars in file with @bun

@pfgithub
Copy link
Contributor

pfgithub commented May 5, 2024

Reproduction:

// a.js
import obj from "./a.json";
console.log(obj);
// a.json
{"a": "测试"}

bun build a.js --target=bun

// @bun
// a.json
var a_default = {
  a: "测试"
};

// a.js
console.log(a_default);

bun 1.1.4:

// @bun
// a.json
var a_default = {
  a: "\u6D4B\u8BD5"
};

// a.js
console.log(a_default);

@eslym
Copy link
Author

eslym commented May 6, 2024

there are some inaccurate part in my findings, thanks for pointing it out.

i think i can use 1.1.4 as builder and use 1.1.7 as runtime in my dockerfile for now, temporary workaround

FROM oven/bun:1.1.4-alpine AS builder

COPY . /home/bun/app

WORKDIR /home/bun/app

RUN bun install --ignore-scripts --no-progress && bun build/index.ts

FROM oven/bun:1.1.7-distroless

COPY --from=builder /home/bun/app/dist /home/bun/app

WORKDIR /home/bun/app

CMD ["index.js"]

@Electroid Electroid added the transpiler parser || printer label May 6, 2024
@Jarred-Sumner Jarred-Sumner changed the title @bun breaks string with unicode characters Importing .json with non-ascii characters within bun build --target=bun does not encode to disk as latin1 May 13, 2024
@robobun robobun changed the title Importing .json with non-ascii characters within bun build --target=bun does not encode to disk as latin1 Importing .json with non-ascii characters within bun build --target=bun does not encode to disk as latin1 May 13, 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 transpiler parser || printer
Projects
None yet
Development

No branches or pull requests

3 participants