oven-sh / bun

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

Incorrect MikroORM version mismatch error on v0.8.0: require inconsistency with v0.7.3 #4296

Closed TomBebb closed 1 year ago

TomBebb commented 1 year ago

What version of Bun is running?

0.8.0

What platform is your computer?

Linux 5.15.90.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

  1. Install Mikro ORM in your bun project:

dependencies should be:

    "@mikro-orm/core": "^5.7.12",
    "@mikro-orm/mysql": "^5.7.12",
  1. Setup a MySQL database somewhere with a 'demo' table with a single column ID, e.g.:
    CREATE TABLE demo (
    id int autoincrement
    );
  2. Create a script:
import { Entity, PrimaryKey } from "@mikro-orm/core"
import { UserRole } from "@mypbxtools/common"
import { MikroORM } from "@mikro-orm/mysql"

@Entity({ tableName: "demo" })
export class Demo {
  @PrimaryKey({ type: "int", autoincrement: true })
  id!: number
}

export const entities = [Demo]
const url = new URL(process.env.DATABASE_URL)

const db = await MikroORM.init<MySqlDriver>({
    allowGlobalContext: true,
    debug: true,
  entities: entities,
  host: url.hostname,
  user: url.username,
  password: url.password,
  port: parseInt(url.port ?? "3306"),
  dbName: url.pathname.replaceAll("/", ""),
})

await db.em.find(Demo)

console.log("Database query succeeded")

Run the script:

bun main.ts

What is the expected behavior?

ORM operation should run properly with no errors, and show the output 'Database query succeeded'

What do you see instead?

Mikro ORM logs:

HTTP error 246 |         const exceptions = new Set(['nestjs', 'sql-highlighter', 'mongo-highlighter']);
247 |         const ormPackages = [...deps].filter(d => d.startsWith('@mikro-orm/') && d !== '@mikro-orm/core' && !exceptions.has(d.substring('@mikro-orm/'.length)));
248 |         for (const ormPackage of ormPackages) {
249 |             const version = await this.getORMPackageVersion(ormPackage);
250 |             if (version != null && version !== coreVersion) {
251 |                 throw new Error(`Bad ${colors_1.colors.cyan(ormPackage)} version ${colors_1.colors.yellow('' + version)}.\n` +
                          ^
error: Bad @mikro-orm/mysql version 5.7.14.
All official @mikro-orm/* packages need to have the exact same version as @mikro-orm/core (undefined).

Additional information

Works fine on Bun v0.7.3

Seems to be related to require behaviour since if I change the getORMPackageVersion method to use async import the demo works fine:

node_modules/@mikro-orm/core/utils./ConfigurationLoader.js
    static async getORMPackageVersion(name) {

        /* istanbul ignore next */
        try {
            const pkg = await import(`${name}/package.json`);
            return pkg?.version;
        }
        catch (e) {
            return undefined;
        }
    }
crishoj commented 1 year ago

As a temporary workaround, you can set MIKRO_ORM_ALLOW_VERSION_MISMATCH.

See https://github.com/mikro-orm/mikro-orm/blob/b03e1656d6bf0a626bdca2f4395ef3a221acfcbf/packages/core/src/utils/ConfigurationLoader.ts#L284

Electroid commented 1 year ago

We recently fixed bun install sometimes downloading the wrong version. Upgrade to the canary Bun with bun upgrade --canary, and the issue should be fixed. (you'll need to re-install the dependency) If you're still experiencing this problem, please re-open this issue and we'll investigate.