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

When a migration fails, error is not correctly printed out. Displaying "no such file or directory" instead #1332

Closed
AssisrMatheus opened this issue Jul 11, 2024 · 1 comment

Comments

@AssisrMatheus
Copy link
Contributor

AssisrMatheus commented Jul 11, 2024

Expected Behavior

If there's a validation issue on a migration that is scheduled to run. The according validation errors should be shown, example:
image

Actual Behavior

Instead, a misleading, incorrect error is showing up instead:
image

Error: ENOENT: no such file or directory, open 'file:///Users/assisrmatheus/dev/perimetre/optrust-app/contentful/migrations/2024-07-11-09-24_blockCarouselItemLabel.js'
    at Object.readFileSync (node:fs:453:20)
    at renderStepsErrors (/Users/assisrmatheus/dev/perimetre/optrust-app/node_modules/contentful-migration/src/bin/lib/steps-errors.ts:37:29)
    at run (/Users/assisrmatheus/dev/perimetre/optrust-app/node_modules/contentful-migration/src/bin/cli.ts:153:24)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at migrate (/Users/assisrmatheus/dev/perimetre/optrust-app/contentful/migrate.ts:97:7) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'file:///Users/assisrmatheus/dev/perimetre/optrust-app/contentful/migrations/2024-07-11-09-24_blockCarouselItemLabel.js'
}

I am not sure that this is mac only, but since it's the current OS I'm using. Might as well be the case. But can't discard it's also not happening on other OSs as well

This is how migrations are being ran:

const { runMigration } = await import('contentful-migration');
for (const migration of migrationsToRun) {
  const migrationModule = await import(migration.path);
  const migrationFunction = migrationModule.default;

  console.log(`Migration: ${migration.name}`);
  await runMigration({
    spaceId: process.env.CONTENTFUL_SPACE_ID,
    environmentId: process.env.CONTENTFUL_ENVIRONMENT,
    accessToken: process.env.CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN,
    yes: true,
    migrationFunction
  });
  console.log(`Migration ${migration.name} completed`);
}

Possible Solution

The fix for the CLI is on pr #1333.

As a user, you can edit the file at /node_modules/contentful-migration/built/bin/lib/steps-errors.js and add the following code:

// {...}
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
+ const url = __importStar(require("url"));
const cardinal = __importStar(require("cardinal"));
// {...}
const errorsByFile = _.groupBy(errors, (error) => {
        const intent = error.details.intent;
        return intent.toRaw().meta.callsite.file;
    });
    const messages = [];
    for (const file of Object.keys(errorsByFile)) {
+        const filepath = file.startsWith(`file://`) ? url.fileURLToPath(file) : file;
+        const fileContents = fs.readFileSync(filepath, 'utf-8');
        const highlightedCode = cardinal.highlight(fileContents, { linenos: true });
        const lines = highlightedCode.split('\n');
+        const fileErrorsMessage = (0, chalk_1.default) `{red Errors in ${filepath}}\n\n`;
        const errorMessages = errorsByFile[file]
            .map((error) => {
            const intent = error.details.intent;
            const callsite = intent.toRaw().meta.callsite;
// {...}

Steps to Reproduce

  1. Create a migration that has validation errors, with invalid actions
  2. Execute the migration by providing it as a migration function

Environment

  • Node Version: v20.11.1 - Using ES Modules
  • Package Manager Version: 10.2.4
  • Operating System: macOS Sonoma 14.5 - Darwin Matheuss-MBP 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 arm64
  • Package Version: 4.20.2
@AssisrMatheus
Copy link
Contributor Author

Fixed with merge of #1333

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

No branches or pull requests

1 participant