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

Create the directory before creating the file #39

Closed
marsrobertson opened this issue Jun 12, 2024 · 4 comments
Closed

Create the directory before creating the file #39

marsrobertson opened this issue Jun 12, 2024 · 4 comments

Comments

@marsrobertson
Copy link
Contributor

marsrobertson commented Jun 12, 2024

Error I was getting:

Log file not found, creating /Users/m/Library/Application Support/KOII-Desktop-Node/namespace/AK2P1L8NWGwWarbHeM7tX2mr4hJA7ZVXGSSSz5PWHBHv/task.log
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: ENOENT: no such file or directory, open '/Users/m/Library/Application Support/KOII-Desktop-Node/namespace/AK2P1L8NWGwWarbHeM7tX2mr4hJA7ZVXGSSSz5PWHBHv/task.log'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/m/Library/Application Support/KOII-Desktop-Node/namespace/AK2P1L8NWGwWarbHeM7tX2mr4hJA7ZVXGSSSz5PWHBHv/task.log'
}

Node.js v18.20.3
[nodemon] app crashed - waiting for file changes before starting..

Here:

// Ensure the log file exists, or create it if it doesn't
try {
await fs.promises.access(desktopNodeLogPath, fs.constants.F_OK);
} catch (err) {
console.log(`Log file not found, creating ${desktopNodeLogPath}`);
await fs.promises.writeFile(desktopNodeLogPath, '', { flag: 'a' }); // 'a' flag ensures the file is created if it doesn't exist and not overwritten if it exists
}

Need to add:

    // Extract the directory path from the full log file path
    const dirPath = path.dirname(desktopNodeLogPath);

    // Check if the directory exists, create it if it doesn't
    try {
      await fs.promises.access(dirPath, fs.constants.F_OK);
    } catch (dirErr) {
      console.log(`Directory not found, creating ${dirPath}`);
      await fs.promises.mkdir(dirPath, { recursive: true });
    }

And on top of the file: const path = require('path');

@marsrobertson
Copy link
Contributor Author

Same issue persists in other prod-debug.js files:

// Ensure the log file exists, or create it if it doesn't
try {
await fs.promises.access(desktopNodeLogPath, fs.constants.F_OK);
} catch (err) {
console.log(`Log file not found, creating ${desktopNodeLogPath}`);
await fs.promises.writeFile(desktopNodeLogPath, '', { flag: 'a' }); // 'a' flag ensures the file is created if it doesn't exist and not overwritten if it exists
}

Presumably all of them needs updating.

@marsrobertson
Copy link
Contributor Author

Some of it has been fixed here: https://github.com/labrocadabro/ezsandbox/pull/1/files

@labrocadabro
Copy link
Contributor

I've made a merge request to the task template (which is where this needs to be changed). But instead of creating the directory, prod-debug will provide a more informative message and exit.

This is because there are two reasons this error usually occurs - one is running prod-debug before adding the task to the desktop node. This fix sort of deals with that but it's wonky - when I tested this and added the task after running prod-debug, I couldn't see the task until I restarted the node.

But the second reason it occurs is that the developer is using the wrong task ID. Creating a directory in that situation wouldn't help them diagnose the problem.

@marsrobertson
Copy link
Contributor Author

This is because there are two reasons this error usually occurs - one is running prod-debug before adding the task to the desktop node.

I thought that's the default way... Run prod-debug first to build / compile in order obtain the task ID.

Bottom line: that should be smooth, simple, intuitive, just works :)

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

2 participants