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

Glob Pattern Entities Not Resolved #9004

Open
Adrastopoulos opened this issue May 15, 2022 · 13 comments
Open

Glob Pattern Entities Not Resolved #9004

Adrastopoulos opened this issue May 15, 2022 · 13 comments

Comments

@Adrastopoulos
Copy link

Issue Description

The Directory Exported Classes Loader (DirectoryExportedClassesLoader.js) normalizes the path provided to the entities property on a DataSourceOptions object. This normalized path changes all forward slashes to back slashes - however, glob is only compatible with forward slashes. Removing the pathNormalize function fixed my issue.

Expected Behavior

All classes found using provided glob pattern...

Actual Behavior

 No classes were found using the provided glob pattern...

Steps to Reproduce

  1. Replicate Code Below
const databaseOptions: DataSourceOptions = {
	type: 'postgres',
	host: 'localhost',
	port: 5432,
	username: 'postgres',
	password: DB_PASSWORD,
	entities: [path.join(__dirname, '/entities/*.js').replace(/\\/g, '/')],
	database: 'postgres',
	logging: ['info'],
};

const dataSource = await new DataSource(databaseOptions).initialize();

console.log(dataSource.entityMetadatas); // []

My Environment

Dependency Version
Operating System Windows
Node.js version 8.3.0
Typescript version 4.6.4
TypeORM version 0.3.6

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
@avin
Copy link

avin commented May 15, 2022

I am experiencing the same problem on Windows, but there are no issues in the Unix environment with the same configuration.

@avin
Copy link

avin commented May 15, 2022

I have found the solution. The bug is in the 'glob' package. To make it work with TypeORM, you need to downgrade the 'glob' dependency to version 7.2.0. (how to change it)

proof isaacs/node-glob#468 (comment)

This was referenced May 15, 2022
@Adrastopoulos
Copy link
Author

Alternatively, upgrading the glob dep to 7.2.3 fixed the issue as well. Thanks for the PR.

@szul
Copy link

szul commented May 16, 2022

I spent all weekend trying to figure out why an entire application environment stopped working. Today I was debugging TypeORM all the way down to DirectoryExportedClassesLoader.js and hit the same issue. After trying various glob patterns, I stuck in a full string and used the debug window in VSCode directly on the server. It worked before pathNormalize(), but not after. We are not yet on NPM 8, so I'm going to attempt a different path to downgrade.

@lidroider
Copy link

I have faced same problem on typeorm 0.3.12 and windows 10. But on linux or mac it's working normally

I fixed by overriding glob -> 7.2.3 in package.json like

"overrides": {
  "glob": "7.2.3"
}

@ownik
Copy link

ownik commented Feb 24, 2023

Yep. I also have the same problem on typeorm 0.3.12 and windows 11. Overrides helps me too

@Vilx2
Copy link

Vilx2 commented Feb 27, 2023

+1 to this. Just spent half a day tearing my hairs out. Thanks to @lidroider for the "overrides" trick, I didn't know that.

That said, I think the bug is on TypeORM side of things. Glob/minimatch explicitly state in the READMEs:

Please only use forward-slashes in glob expressions.

Though windows uses either / or \ as its path separator, only / characters are used by this glob implementation. You must use forward-slashes only in glob expressions. Back-slashes in patterns will always be interpreted as escape characters, not path separators.

However TypeORM explicitly calls PathNormalize() which converts all / to ­\. This is wrong.

@ownik
Copy link

ownik commented Mar 1, 2023

I guess it's already resolved in PR #9768 3 weeks ago. But unfortunately, it's not included in 0.3.12

@ownik
Copy link

ownik commented Mar 1, 2023

I checked 0.3.13-dev.58fc088 and it includes fix and works fine.

@akmaldira
Copy link

Thanks you, i stess about this issue in 3 days

@cooljeffro
Copy link

I have faced same problem on typeorm 0.3.12 and windows 10. But on linux or mac it's working normally

I fixed by overriding glob -> 7.2.3 in package.json like

"overrides": {
  "glob": "7.2.3"
}

This also worked for me, but I only overrode typeorm's glob version. I also had to uninstall and reinstall typeorm for the overriden glob version to get installed.

"overrides": {
  "typeorm": {
    "glob": "7.2.3"
  }
}

@foaudkajj
Copy link

Can't believe that I spent hours on this problem.
I replaced the version of the typeorm in the package.json file with "typeorm": "0.3.13-dev.58fc088"

see #9768

NoNamer777 added a commit to NoNamer777/dnd-mapp that referenced this issue Apr 24, 2023
For Windows specifically, it breaks the usage of glob patterns and can't
find the migration files or entities if not overridden. The bug is fixed
already but not included in the latest release yet.
typeorm/typeorm#9004 (comment)
@Samandar02
Copy link

Samandar02 commented Nov 24, 2023

import { join } from "node:path";
import { DataSource } from "typeorm";

export let dataSource = new DataSource({
    type: 'postgres',
    host: 'localhost',
    port: 5432,
    database: 'localdb',
    username: 'admin',
    password: 'admin',
    schema: 'public',
    entities: [join(__dirname, '/entities/*.ts').replace(/\\/g, '/')],
    migrations: [join(__dirname, '/migrations/*.ts').replace(/\\/g, '/')],
})

this snipped code works for me width Windows,

note: datasource.ts file, migrations and entities folder inside of same folder db (see below)

+db
|- datasource.ts
|- entities
|- migrations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants