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

fix: astrodb vite plugin throws in vitest and cannot seed database #11435

Merged
merged 6 commits into from
Oct 21, 2024

Conversation

haivuw
Copy link
Contributor

@haivuw haivuw commented Jul 9, 2024

Changes

Fixes #11414

import.meta.env is undefined in the virtual module which make it throw and the database is not seeded.

Testing

Tested by linking local @astrojs/db in my other project. Db was seeded ok.

Docs

just a minor bug fix

Copy link

changeset-bot bot commented Jul 9, 2024

🦋 Changeset detected

Latest commit: 905562c

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@haivuw
Copy link
Contributor Author

haivuw commented Jul 9, 2024

I couldn't run all tests #11441. Though @astrojs/db:test alone passed.

@marpe
Copy link

marpe commented Oct 12, 2024

Just ran into this issue, would be nice to have this merged.

@matthewp
Copy link
Contributor

@haivuw can you update this PR

@haivuw
Copy link
Contributor Author

haivuw commented Oct 19, 2024

@matthewp I resolved the conflicts but this test failed on Windows:
https://github.com/withastro/astro/blob/ff522b96a01391a29b44f820dfcc2a2176d871e7/packages/db/test/local-prod.test.js#L37C3-L37C4

The path to local db is malformed: /D:/a/astro/astro/packages/db/test ixtures�asicsdistastro.db

✖ failing tests:

test at file:\D:\a\astro\astro\packages\db\test\local-prod.test.js:50:3
✖ Can render page
  'test did not finish before its parent and was cancelled'

test at file:\D:\a\astro\astro\packages\db\test\local-prod.test.js:37:2
✖ build (not remote) with DATABASE_FILE env (relative file path) (683.227ms)
  [astro:db] Could not load astro:db (imported by test/fixtures/local-prod/src/pages/index.astro): Failed to connect to database: `Unable to open connection to local database /D:/a/astro/astro/packages/db/testixtures�asicsdistastro.db: 14`
  Error: Failed to connect to database: `Unable to open connection to local database /D:/a/astro/astro/packages/db/testixtures�asicsdistastro.db: 14`
      at new Database (D:\a\astro\astro\node_modules\.pnpm\libsql@0.4.5\node_modules\libsql\index.js:83:17)
      at _createClient (file:///D:/a/astro/astro/node_modules/.pnpm/@libsql+client@0.14.0/node_modules/@libsql/client/lib-esm/sqlite3.js:47:16)
      at _createClient (file:///D:/a/astro/astro/node_modules/.pnpm/@libsql+client@0.14.0/node_modules/@libsql/client/lib-esm/node.js:21:16)
      at Module.createClient (file:///D:/a/astro/astro/node_modules/.pnpm/@libsql+client@0.14.0/node_modules/@libsql/client/lib-esm/node.js:11:12)
      at Module.createLocalDatabaseClient (D:/a/astro/astro/packages/db/dist/runtime/db-client.js:25:40)
      at eval (astro:db:seed:8:34)
      at async instantiateModule (file:///D:/a/astro/astro/node_modules/.pnpm/vite@5.4.9_@types+node@18.19.50_sass@1.79.5/node_modules/vite/dist/node/chunks/dep-Cyk9bIUq.js:52961:5) {
    code: 'PLUGIN_ERROR',
    pluginCode: '',
    plugin: 'astro:db',
    hook: 'load',

I don't have the capacity to make sure all tests pass right now. So this is as far as I can do.

@@ -124,12 +124,14 @@ export function getLocalVirtualModContents({
tables: DBTables;
root: URL;
}) {
const { ASTRO_DATABASE_FILE } = getAstroEnv();
const envDbUrl = ASTRO_DATABASE_FILE ? `'${ASTRO_DATABASE_FILE}'` : undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to stringify the string, because it's already a string. We already to such here:

const { ASTRO_DATABASE_FILE } = getAstroEnv();
const dbUrl = normalizeDatabaseUrl(ASTRO_DATABASE_FILE, new URL(DB_PATH, root).href);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be

const dbUrl = normalizeDatabaseUrl(file://local.db, ${JSON.stringify(dbUrl)});

without stringification.

But I just realized I could have used JSON.stringify, so I'll update.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The failing test is definitely weird though, I wonder what's causing the failure

Copy link
Contributor Author

@haivuw haivuw Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the test passed before because ASTRO_DATABASE_URL was not passed to the plugin at all.

And this line of the test generates a malformed path on Windows:

const prodDbPath = relative(process.cwd(), fileURLToPath(absoluteFileUrl));

which is /D:/a/astro/astro/packages/db/test ixtures�asicsdistastro.db

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hopefully someone with Windows machine can fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait it passed now 💀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what magic JSON.stringify does here.

.changeset/lemon-beans-admire.md Outdated Show resolved Hide resolved
haivuw and others added 2 commits October 21, 2024 17:05
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
@ematipico ematipico merged commit f32a7a8 into withastro:main Oct 21, 2024
13 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AstroDB] getViteConfig throws in Vitest - Cannot read properties of undefined (reading 'ASTRO_DATABASE_FILE')
4 participants