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

Exception in HostFunction: No such file or directory (os error 2) #10

Closed
dwightwatson opened this issue May 25, 2024 · 6 comments · May be fixed by #22
Closed

Exception in HostFunction: No such file or directory (os error 2) #10

dwightwatson opened this issue May 25, 2024 · 6 comments · May be fixed by #22

Comments

@dwightwatson
Copy link
Contributor

Trying to get this going in an Expo project and not having much luck so far.

  • Installed into a new-ish Expo project
  • Added the plugin to app.json
  • Added schema.primsa to the root directory
  • Ran npx prisma@latest generate
  • Called runMigrations from the root component
export const runMigrations = async () => {
  try {
    await baseClient.$applyPendingMigrations();
  } catch (e) {
    console.error(`failed to apply migrations: ${e}`);

    throw new Error(
      "Applying migrations failed, your app is now in an inconsistent state. We cannot guarantee safety, it is now your responsability to reset the database or tell the user to re-install the app",
    );
  }
};

The error thrown is Exception in HostFunction: No such file or directory (os error 2)

I am newer to Prisma so did some doc-diving and thought perhaps I needed to run npx prisma migrate dev which generated a migrations directory, app.db and app.db-journal in my root directory, but this wasn't mentioned in the docs and also didn't solve the problem.

@sorenbs
Copy link
Member

sorenbs commented May 25, 2024

Hi Dwight 👋

It sounds like you might have the wrong version of Prisma running. Did you edit the schema.prisma file to enable the preview feature as documented in the readme?

generator client {
  provider = "prisma-client-js"
  previewFeatures = ["reactNative"] // <- this line is important !!!
}

You can simply add that line and re-run the generate command:

npx prisma@latest generate

@dwightwatson
Copy link
Contributor Author

dwightwatson commented May 26, 2024

Hey Søren,

Thanks for that - I re-ran the installation command from the docs which looks like it should have installed the latest versions of each package. Here are the versions shown from package-lock.json:

"@prisma/client": "^5.14.0",
"@prisma/react-native": "^5.14.0",
"react-native-quick-base64": "^2.1.2",

I copied the schema.prisma from the docs and just changed the model.

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["reactNative"]
}

datasource db {
  provider = "sqlite"
  url      = "file:./app.db"
}

// Your data model

model Category {
  id           Int     @id @default(autoincrement())
  name         String
  description  String?
  defaultOrder String
  order        Int
  small        String?
  large        String?
}

And here is the output from the generate command:

✗ npx prisma@latest generate
Need to install the following packages:
[email protected]
Ok to proceed? (y) y

Environment variables loaded from .env
Prisma schema loaded from schema.prisma

✔ Generated Prisma Client (v5.14.0) to ./node_modules/@prisma/client in 48ms

Start using Prisma Client in Node.js (See: https://pris.ly/d/client)
```
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
```
or start using Prisma Client at the edge (See: https://pris.ly/d/accelerate)
```
import { PrismaClient } from '@prisma/client/edge'
const prisma = new PrismaClient()
```

See other ways of importing Prisma Client: http://pris.ly/d/importing-client

┌─────────────────────────────────────────────────────────────┐
│  Deploying your app to serverless or edge functions?        │
│  Try Prisma Accelerate for connection pooling and caching.  │
│  https://pris.ly/cli/--accelerate                           │
└─────────────────────────────────────────────────────────────┘

Are there any clues in there? (Do I need to run npx prisma migrate dev in the context of a RN app or is that not relevant?)

@sorenbs
Copy link
Member

sorenbs commented Jun 3, 2024

Okay, I think I understand what is happening.

You must run npx expo prebuild --clean once after adding the expo extension. Can you try to run it once and see if that resolves the issue for you? The extension takes care of copying the database and migrations files to the simulator, so when it is not set up correctly, that results in the No such file or directory error you are seeing.

I have updated the Readme to makes this more clear.

Thank you for reporting this!

@dwightwatson
Copy link
Contributor Author

I had been using EAS build - but yes, after running npx prisma migrate dev and then eas build --profile=development this appears to work now. Thanks for that!

It does feel a little icky to have the app.db present in the repo as conceptually it feels like that should live inside the app only. Do you think this will always be a requirement or something that could be handled automatically eventually?

@sorenbs
Copy link
Member

sorenbs commented Jun 4, 2024

Excellent!

Having the db in the repo isn't strictly required, and I agree it's a bit weird. I think we should have an easy way to connect a db viewer to the db running on a device/simulator during development, and then do away with that db in the repo.

Do you have a preference between a standard tool such as SQLite Viewer for VS Code vs Prisma Studio?

@dwightwatson
Copy link
Contributor Author

Honestly - I'm brand new to Prisma, so I don't have any knowledge/preference on the matter.

Being able to connect directly to the database running on a device or simulator sounds like an incredible tool in debugging and understanding what's going on.

I'll close this issue now anyway as the original problem is now resolved.

hassankhan added a commit to twodoorsdev/react-native-prisma that referenced this issue Jun 12, 2024
…tform

Fixes prisma#10 for Mac Catalyst platform, its likely the same fix will work for macOS in the future too.

Also fixes an issue when using `NSURL.absoluteString` on a path contains spaces, the spaces would
get replaced by `%20`.
hassankhan pushed a commit to twodoorsdev/react-native-prisma that referenced this issue Jun 12, 2024
… platform

Fixes prisma#10 for Mac Catalyst platform, its likely the same fix will work for macOS in the future too.

Also fixes an issue when using `NSURL.absoluteString` on a path contains spaces, the spaces would
get replaced by `%20`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants