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

[Bug]: @actual-app/api does not work when bundled #3384

Open
2 tasks done
rodrigost23 opened this issue Sep 6, 2024 · 3 comments
Open
2 tasks done

[Bug]: @actual-app/api does not work when bundled #3384

rodrigost23 opened this issue Sep 6, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@rodrigost23
Copy link

Verified issue does not already exist?

  • I have searched and found no existing issue
  • I will be providing steps how to reproduce the bug (in most cases this will also mean uploading a demo budget file)

What happened?

I was trying to use the API in a Next.js environment. But it bundles the dependencies into chunks, and when @actual/api uses "__dirname" to find the root path, it ends up in my disk root, thus not able to find the migrations folder.

It does not make sense to find a folder using __dirname as in:

let rootPath = path.join(__dirname, '..', '..', '..', '..');
if (__filename.match('bundle')) {
// The file name is not our filename and indicates that we're in the
// bundled form. Because of this, the root path is different.
rootPath = path.join(__dirname, '..');
}

There should be a way to set this rootPath manually, maybe instead of setting the data path, as everything would be under the root path, or at least setting the migrations path.

Where are you hosting Actual?

Locally via Yarn

What browsers are you seeing the problem on?

Firefox

Operating System

Windows 11

@rodrigost23 rodrigost23 added the bug Something isn't working label Sep 6, 2024
@rodrigost23
Copy link
Author

Workaround for Next.js:

Edit next.config.mjs:

import CopyPlugin from 'copy-webpack-plugin';
import path from 'path';

/** @type {import('next').NextConfig} */
const nextConfig = {
  webpack: (config) => {
    // Change rootPath for @actual-app/api to find resources
    config.module.rules.push({
      test: /@actual-app.*\.js$/,
      loader: 'string-replace-loader',
      options: {
        search: 'let rootPath = .*?;',
        replace: `let rootPath = "${path.resolve('.next/@actual-app').replaceAll('\\', '\\\\')}"`,
        flags: 'g',
      },
    });
    // Copy @actual-app/api resources
    config.plugins.push(
      new CopyPlugin({
        patterns: [
          {
            from: 'node_modules/@actual-app/api/dist/migrations',
            to: '@actual-app/migrations',
          },
          {
            from: 'node_modules/@actual-app/api/dist/default-db.sqlite',
            to: '@actual-app/default-db.sqlite',
          },
        ],
      }),
    );
    return config;
  },
};

export default nextConfig;

@latetedemelon
Copy link

latetedemelon commented Sep 6, 2024

@rodrigost23 I built a new script with the latest api (6.10.0) and actual version (24.9.0) and I'm seeing a consistent "Database is out of sync with migrations" error. This is both with "old" data and a brand new budget. Could it be related to your issue? If not I'll break this out.

Database is out of sync with migrations: {
  appliedIds: [
    1548957970627, 1550601598648, 1555786194328,
    1561751833510, 1567699552727, 1582384163573,
    1597756566448, 1608652596043, 1608652596044,
    1612625548236, 1614782639336, 1615745967948,
    1616167010796, 1618975177358, 1632571489012,
    1679728867040, 1681115033845, 1682974838138,
    1685007876842, 1686139660866, 1688749527273,
    1688841238000, 1691233396000, 1694438752000,
    1697046240000, 1704572023730, 1704572023731,
    1707267033000, 1712784523000, 1716359441000,
    1720310586000, 1720664867241, 1720665000000,
    1722717601000, 1722804019000
  ],
  available: [
    '1548957970627_remove-db-version.sql',
    '1550601598648_payees.sql',
    '1555786194328_remove_category_group_unique.sql',
    '1561751833510_indexes.sql',
    '1567699552727_budget.sql',
    '1582384163573_cleared.sql',
    '1597756566448_rules.sql',
    '1608652596043_parent_field.sql',
    '1608652596044_trans_views.sql',
    '1612625548236_optimize.sql',
    '1614782639336_trans_views2.sql',
    '1615745967948_meta.sql',
    '1616167010796_accounts_order.sql',
    '1618975177358_schedules.sql',
    '1632571489012_remove_cache.js',
    '1679728867040_rules_conditions.sql',
    '1681115033845_add_schedule_name.sql',
    '1682974838138_remove_payee_rules.sql',
    '1685007876842_add_category_hidden.sql',
    '1686139660866_remove_account_type.sql',
    '1688749527273_transaction_filters.sql',
    '1688841238000_add_account_type.sql'
  ]
}
Error updating Error: out-of-sync-migrations

@rodrigost23
Copy link
Author

Yes, I was having the same error before I used the custom webpack configuration. The problem was that the Actual API was trying to find the migrations folder that is inside the library itself, but it uses a path relative to __dirname, which is not reliable, and isn't customizable.

The workaround was to set all let rootPath = in the library's code to a folder containing the migrations folder copied from the lib.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants
@rodrigost23 @latetedemelon and others